News:

Wondering if this will always be free?  See why free is better.

Main Menu

A PHP File That Loads Avatar Images

Started by Trenaldi, November 04, 2014, 07:27:40 PM

Previous topic - Next topic

Trenaldi

Hey guys,

I know very little about PHP, but here's what I was wondering was possible and how.

Could one make a php file which you could use to load users forum avatars on external pages? Something I could keep in the root with my forum and then reference in say an image tag on an external page that would grab someone's avatar. As an example:

<img src="http://www.forumdomain.com/forums/Avatar.php?42"/>
And have that URL function as an image both when used in a page or when directly navigated to. Specifically, the avatar of member number 42 in that example where the "?42" is what's appended to the URL.

Thanks in advance.

Kindred

load SSI.php

check the $user_info or the $context arrays...
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

JBlaze

Quote from: Kindred on November 05, 2014, 12:54:31 PM
load SSI.php

check the $user_info or the $context arrays...

That would only return the avatar URL. You would then need to use GD functions to turn the URL into an image resource, find out what format the image is in (jpg, png, gif, etc), and then return a Content-type: image/{format} header and then create the image. It's much more complicated than just grabbing $user_info['avatar']['link']...
Jason Clemons
Former Team Member 2009 - 2012

margarett

Especially because $user_info returns the info about the current logged user, which is not what I understand from the OP.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Trenaldi

Being a bit of a raging idiot, would something like this work?:

<?php
include_once("http://www.forumdomain.com/forums/SSI.php");
$img '$context['user']['avatar']['image']';
string image_type_to_extension int $imagetype [, bool $include_dot FALSE ] );
header('Content-Type: image/$imagetype');
readfile($img);
?>


And I guess if ['user'] is a valid thing that the URL for a given avatar would be
www.forumdomain.com/forums/Avatar.php?user=42
I guess?

I mean, I know that this in particular does not work, but how badly have I missed the mark here? Are we talking "On the right track" or are we talking "I should stick my head in an oven."?

margarett

More or less on the right track ;)

You need to include SSI.php then either call loadMemberContext (or is it Data?...) that will get you all user details, including avatar URL.
Or, if you don't want to have the overhead of getting a bunch of data you don't need, manually fetch the required data from members/attachments table ;)

I'm just writing of the phone, I can check it better when I'm at the computer
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

JBlaze

#6
<?php
require_once('SSI.php');

// get the required member info
$id_member = (int) $_GET['id'];
$member_data ssi_fetchMember($id_member'return');

// find the image type
$image_type exif_imagetype($member_data['avatar']);

// get the image format and make it a string so we can use it for our headers later
if ($image_type IMAGETYPE_GIF)
{
    
$image_format 'gif';
}
else if (
$image_type IMAGETYPE_JPEG)
{
    
$image_format 'jpeg';
}
else if (
$image_type == IMAGETYPE_PNG)
{
    
$image_format 'png';
}
else
{
    
$image_format null;
}

// create the correct image resource
switch ($image_type)
{
    case 
IMAGETYPE_GIF:
        
$im imagecreatefromgif($member_data['avatar']);
        break;
    case 
IMAGETYPE_JPEG:
        
$im imagecreatefromjpeg($member_data['avatar']);
        break;
    case 
IMAGETYPE_PNG:
        
$im imagecreatefrompng($member_data['avatar']);
    default:
        die(
'Image type not supported!');
}

// no image, no workie... error out
if (!$im)
{
    die(
'An error has occurred!');
}

// send our headers
header('Content-type: image/' $image_format);

// send the image to the browser, then destroy it
image' . $image_format($im);
imagedestroy($im);


This is just what I came up with off the top of my head. You'd need to double check the avatar variable to see which one returns the path/url of the avatar image. Also, it could do with some better error handling.

Lastly, I have not tested this, so if it doesn't work, don't blame me. :)
Jason Clemons
Former Team Member 2009 - 2012

Advertisement: