PDA

View Full Version : GD & PHP: returning image source with a function


tatlar
02-26-2005, 12:51 AM
Hi there,

I am trying to dynamically generate a resized image with a custom function using the GD library.

What I have is the following:


echo "<img src=\"" . return_websize_image( $orig_image_file, 600, 450, "JPG" ) . "\" alt=\"Resized image\" />" ;


The function return_websize_image(); looks like the following:

<?php
function return_websize_image( $imgsource, $width, $height, $return_image_type ) {

list( $in_x, $in_y, $type, $attr ) = getimagesize( $imgsource ) ; // get image parameters

ob_start() ;

switch ( $return_image_type ) { // type of webimage wanted

case "JPG":
header( "Content-type: image/jpg" ) ;
$in_image = @imagecreatefromJPEG( $imgsource ) ;
$out_image = @imagecreatetruecolor( $width, $height ) ;
imagecopyresampled( $out_image, $in_image, 0, 0, 0, 0, $width, $height, $in_x, $in_y ) ;
imagejpeg( $out_image ) ;
break ;

case "GIF":
header( "Content-type: image/gif" ) ;
$in_image = @imagecreatefromgif( $imgsource ) ;
$out_image = @imagecreatetruecolor( $width, $height ) ;
imagecopyresampled( $out_image, $in_image, 0, 0, 0, 0, $width, $height, $in_x, $in_y ) ;
imagegif( $out_image ) ;
break ;

case "PNG":
header( "Content-type: image/png" ) ;
$in_image = @imagecreatefrompng( $imgsource ) ;
$out_image = @imagecreatetruecolor( $width, $height ) ;
imagecopyresampled( $out_image, $in_image, 0, 0, 0, 0, $width, $height, $in_x, $in_y ) ;
imagepng( $out_image ) ;
break ;
}

$buffer = ob_get_contents();
ob_end_clean();
return $buffer ;
}
?>


However, I keep getting a headers error (from my error log):


[error] PHP Warning: Cannot modify header information - headers already sent by index.php on line 5


I had thought by using ob_start I would guarantee that the headers are sent to the buffer and not the parent page. Any idea what I am doing wrong? I searched the web to see anyone has tried something similar but most people just source another php script like:


<img src="image.php?source=someParameter" />


I am trying to avoid having an extra script to do the job that I am pretty sure a function can do. Can anyone help me?

Thanks in advance!

darksidepuffin
02-26-2005, 07:47 AM
Your problem..I believe...is you need to buffer ALL output:


<?
ob_start();//line ONE(or two because of the php tag) of your file

/* ...................*/

ob_end_flush();//LAST Line..Very Last Line..or 2nd last due to php tag
?>


This includes whitespace coming before the opening php tag..and any other pathogens therein.

See if that helps.

scoutt
03-01-2005, 07:41 PM
you do it this way

<img src="image.php?source=someParameter" />

and image.php contains the function. that is the best way as you get the image header too. if not you hav eto mess with the header function and that could get messy

tatlar
03-01-2005, 08:13 PM
hmmmm.

I was hoping to get around using another script. I am trying to modularize my code and so any extra scripts are not all that welcome.

demonicpuffin - I have not yet had a chance to try your suggestion - when I do I will post the results.

cheers.