Page 1 of 1

Using libGD to output an image in another page

Posted: Tue Jul 07, 2009 4:04 am
by unknown365
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi! I have a PHP function which I use to resize the images that I get from the database.
These are the functions inside my getImage.php:

Code: Select all

function resize($image){
    $background = imagecreatetruecolor(116,84);
    $source = imagecreatefromjpeg($image);
    $width= imagesx($source);
    $height = imagesy($source);
    header("Content-type: image/jpeg");
 
    if($width>$height){
        imagecopyresampled($background,$source,0,0,0,0,116,84,$width,$height);
        imagejpeg($background);
        imagedestroy($background);
    }
    else{
        imagefill($background,0,0,imagecolorallocate($background, 255, 255, 80));
        imagecopyresampled($background,$source,20,0,0,0,75,84,$width,$height);
        imagejpeg($background);
        imagedestroy($background);
    }
}
 
 
function getImage($id){
   $query="SELECT i.filepath FROM users u, items i
               WHERE u.userid=i.userid AND u.userid=$id";
   $res = mysql_query($query);
 
   while ($obj = mysql_fetch_object($res)) {
      $file = $obj->filepath;
      return resize($file);
   }
}
Basically, I don't have any problems with resizing the image. My problem is that whenever I try to call this function to output in another page either by using <img src="<?=getImage($id)?>" border="0"> OR <img src="getImage.php" border="0">, the image does not show itself. It's as if I didn't call any image. Please help!

By the way, I really need the getImage.php to output the image to another page.


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Using libGD to output an image in another page

Posted: Tue Jul 07, 2009 4:34 am
by onion2k
Is that all the code? I don't see any header() calls, so you're not sending the Content-Type header. You need:

Code: Select all

header("Content-type: image/jpeg");

Re: Using libGD to output an image in another page

Posted: Tue Jul 07, 2009 7:41 pm
by unknown365
The header is there above the if and else statement

Re: Using libGD to output an image in another page

Posted: Tue Jul 07, 2009 10:54 pm
by Skara
This is what you want, not the other one:

Code: Select all

<img src="getImage.php" alt="" />
However, I see no code that's actually run in getImage.php. You're only declaring functions. On top of that, since whenever the code is run the output is always an image... I'm assuming you're using functions like this so that you can use the code for more than one purpose...? The whole thing's a little wonky to me.
You're also needlessly duplicating code.
On top of all that, you're looping through and parsing multiple images.

Code: Select all

function getImage($id){
   $query="SELECT i.filepath FROM users u, items i
              WHERE u.userid=i.userid AND u.userid=$id";
   $res = mysql_query($query);
 
   while ($obj = mysql_fetch_object($res)) {
      $file = $obj->filepath;
      return resize($file); //this gets run over and over.  you can only output a single image.  in other words, you can only output a single file--the file that this is.
   }
}

Code: Select all

function display($image) {
    header("Content-type: image/jpeg");
    imagejpeg($image);
    imagedestroy($image);
}
function resize($image){
    $background = imagecreatetruecolor(116,84);
    $source = imagecreatefromjpeg($image);
    $width= imagesx($source);
    $height = imagesy($source);
 
    if($width>$height){
        imagecopyresampled($background,$source,0,0,0,0,116,84,$width,$height);
    }
    else{
        imagefill($background,0,0,imagecolorallocate($background, 255, 255, 80));
        imagecopyresampled($background,$source,20,0,0,0,75,84,$width,$height);
    }
    return $background;
}
 
function getImage($id) {
   //I'm assuming you're only retrieving a single image with this query, so I'm adding a LIMIT
   $query="SELECT i.filepath FROM users u, items i WHERE u.userid=i.userid AND u.userid='$id' LIMIT 1;"; //quote your variables
   $res = mysql_query($query);
 
   $obj = mysql_fetch_object($res);
   $file = $obj->filepath; //why using an object instead of an array?
   return $file;
}
 
//now to actually _run_ the code:
//get the filename
$filename = getImage(intval($_GET['id']));
//create the image
$image = resize($filename);
//output the image to the screen
display($image);
 

Code: Select all

<!-- Adding the &.jpg to the end makes some browsers happier about treating a php file as a jpg -->
<img src="showimage.php?id=5&.jpg" alt="" />

Re: Using libGD to output an image in another page

Posted: Thu Jul 09, 2009 12:18 am
by unknown365
I was finally able to solve it yesterday. I did not have any mistakes with the getImage.php. My mistake was I forgot to put a connection to the database. How stupid..thanks for your help guys:D