Log in

View Full Version : How would I show avatars?


AutomatikStudio
01-16-2007, 06:00 PM
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:

$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.


$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).



$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
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 );
}

AutomatikStudio
01-17-2007, 03:32 PM
Thanks for the tip. I tried your initial code with another vB installation of mine that is on an actual live server...and it works fine. Right now, where I'm having problems, is on my local development server (OS X) that has PHP/MySQL installed. So it's gotta be some sort of PHP setting...I just have no idea what.

Any ideas what it could be?

ElfMage
01-17-2007, 03:40 PM
Sorry, haven't worked ever on OS X.

I work on Windows workstations :eek: and Linux servers.

In theory there should be no difference whatsoever, but I wouldn't know..

AutomatikStudio
01-17-2007, 03:51 PM
Sorry, haven't worked ever on OS X.

I work on Windows workstations :eek: and Linux servers.

In theory there should be no difference whatsoever, but I wouldn't know..

Well it's not an OS issue...it's some sort of PHP/MySQL setting issue.

Oh well.