vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   How would I show avatars? (https://vborg.vbsupport.ru/showthread.php?t=136687)

AutomatikStudio 01-16-2007 06:00 PM

How would I show avatars?
 
I'm tying in to vB's user system for a site I'm working on and I'd like to display user avatars throughout the site on lots of non-vb pages. I'm having trouble getting the images to display though.

Here's the query I'm making in my avatar.php file that should display a user avatar:

PHP Code:

$sql mysql_query("SELECT filedata, dateline, filename
            FROM vb_customavatar
            WHERE userid = '1'"
);

    
$row=mysql_fetch_array($sql);

    
header("Content-Length: ".strlen($row[filedata]));
    
header('Content-transfer-encoding: binary');
    
header("Content-Disposition: inline; filename=\"".$row['filename']."\";");
    
header("Content-Type: image/jpeg");
    echo 
$row['filedata']; 

The query works fine, but it's just plain not showing the image.

Am I missing something?

ElfMage 01-16-2007 07:13 PM

Note that the userid is numeric, you don't need the quotes (it should work with the quotes, but you are making MySQL do an extra conversion for each row.)

Try the following code, I haven't tried but it should work.

PHP Code:

$sql mysql_query("SELECT filedata, dateline, filename
            FROM vb_customavatar
            WHERE userid = 1"
); 

$row=mysql_fetch_array($sql); 

header('Cache-control: max-age=31536000');
header('Expires: ' gmdate('D, d M Y H:i:s', (TIMENOW 31536000)) . ' GMT');
header('Content-disposition: inline; filename=' $row['filename']);
header('Content-transfer-encoding: binary');
header('Content-Length: ' strlen($row['filedata']));
header('Last-Modified: ' gmdate('D, d M Y H:i:s'$row['dateline']) . ' GMT');
$extension trim(substr(strrchr(strtolower($row['filename']), '.'), 1));
if (
$extension == 'jpg' OR $extension == 'jpeg')
    
header('Content-type: image/jpeg');
else if (
$extension == 'png')
    
header('Content-type: image/png');
else
    
header('Content-type: image/gif');

echo 
$row['filedata']; 


AutomatikStudio 01-16-2007 07:21 PM

Still no go. I really have no idea why. Maybe I need some tips on debugging this? I've got error reporting set to E_ALL and it's not giving me anything related to this.

ElfMage 01-16-2007 07:51 PM

I just tried it, and it works. You can see it working here: http://www.nuhit.com/test/getavatar.php?userid=86

Here is my full code: (you would need to change the location of your forums in the config_filename variable).

PHP Code:


$config_filename 
dirname(__FILE__) . "/../forums/includes/config.php";

require_once 
$config_filename;
define'TABLE_PREFIX'$config['Database']['tableprefix'] );

$userid $_REQUEST['userid'];

$conn mysql_connect$config['MasterServer']['servername'], $config['MasterServer']['username'], $config['MasterServer']['password'] );
if (!
$conn)
    die (
"Could not connect to server.");

if (!
mysql_select_db$config['Database']['dbname'], $conn ))
    die (
"Database not found.");

$sql "SELECT filedata, dateline, filename 
            FROM " 
TABLE_PREFIX "customavatar
            WHERE userid = 
$userid";
$results mysql_query($sql$conn); 
if (!
$results)
    die (
"Error running query.");

$row=mysql_fetch_array($results); 

header('Cache-control: max-age=31536000');
header('Expires: ' gmdate('D, d M Y H:i:s', (TIMENOW 31536000)) . ' GMT');
header('Content-disposition: inline; filename=' $row['filename']);
header('Content-transfer-encoding: binary');
header('Content-Length: ' strlen($row['filedata']));
header('Last-Modified: ' gmdate('D, d M Y H:i:s'$row['dateline']) . ' GMT');
$extension trim(substr(strrchr(strtolower($row['filename']), '.'), 1));
if (
$extension == 'jpg' OR $extension == 'jpeg')
    
header('Content-type: image/jpeg');
else if (
$extension == 'png')
    
header('Content-type: image/png');
else
    
header('Content-type: image/gif');

echo 
$row['filedata']; 


AutomatikStudio 01-16-2007 08:24 PM

That's so odd. That very thing doesn't work for me. It's not throwing any errors and if I remove the header() stuff it displays the contents (letters/numbers) of the 'filedata' field.

Could it be something with my version of PHP/MySQL?

I'm running PHP 5.1.6 and MySQL 4.1.2-standard.

ElfMage 01-16-2007 08:34 PM

I am using PHP 5.1 with MySQL 5.

However, if the avatars are shown fine in your forum, then they should show using this script. I "adapted" the code found in vBulletin's image.php.

Which brings me to the next point, why don't you use image.php directly?:

http://www.nuhit.com/forums/image.php?userid=86

AutomatikStudio 01-16-2007 09:01 PM

Well, I'm ultimately wanting to resize the avatars to various sizes depending on their locations throughout the site...so I'm wanting to pass the image through my GD resizing stuff.

ElfMage 01-16-2007 09:09 PM

Oh, I see.

You could save the data returned by the query into a local file in your server and compare it with the file returned by image.php, they should be binarily the same.

AutomatikStudio 01-16-2007 09:53 PM

Quote:

Originally Posted by ElfMage (Post 1160940)
Oh, I see.

You could save the data returned by the query into a local file in your server and compare it with the file returned by image.php, they should be binarily the same.

Ah. Any tips on doing that?

ElfMage 01-16-2007 09:58 PM

You could do this:

$filename = 'my_' . $row['filename'];
$fp = fopen( $filename, 'wb' );
if ($fp)
{
fwrite( $fp, $row['filedata'] );
fclose( $fp );
}


All times are GMT. The time now is 09:23 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01822 seconds
  • Memory Usage 1,771KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete