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!
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!