Yesideez Posted April 19, 2009 Share Posted April 19, 2009 Is it possible to have a function inside a script to generate a thumbnail of an image and display it in the browser without having to use an external script? I've got a script that shows the status of the background image, here's the line of code that is trying to display the image: <tr><td>Image</td><td><a href="<?php echo '../uploads/images/'.$image->name; ?>" rel="lightbox" title="Background Image" <?php echo htmlentities($image->name); ?>><?php imageResize($basedir.'/../uploads/images/'.$image->name,$image->type,false,null,50); ?></a></td></tr> This is the function imageResize(): function imageResize($source,$type,$target=false,$newWidth=null,$newHeight=null) { $result['success']=false; switch ($type) { case 'jpg':header("Content-type: image/jpg");$src=@imagecreatefromjpeg($source);break; case 'png':header("Content-type: image/png");$src=@imagecreatefrompng($source);break; case 'gif':header("Content-type: image/gif");$src=@imagecreatefromgif($source);break; } if (isset($src)) { //DID WE CREATE OUR RESOURCE? list($imgW,$imgH)=getimagesize($source); if ($newWidth==null && $newHeight==null) { //WIDTH & HEIGHT NOT SPECIFIED SO DON'T RESIZE $newWidth=$imgW; $newHeight=$imgH; } else if ($newHeight==null) { //CALCULATE NEW HEIGHT CONSTRAINING THE DIMENSIONS $newHeight=intval(($imgH/$imgW)*$newWidth); } else { //CALCULATE NEW WIDTH CONSTRAINING THE DIMENSIONS $newWidth=intval(($imgW/$imgH)*$newHeight); } $tmp=@imagecreatetruecolor($newWidth,$newHeight); if ($tmp) { //DID WE CREATE OUR IMAGE? if (@imagecopyresampled($tmp,$src,0,0,0,0,$newWidth,$newHeight,$imgW,$imgH)) { //DID WE RESIZE THE IMAGE? if ($target==false) { switch ($type) { case 'jpg':$img=@imagejpeg($tmp);break; case 'png':$img=@imagepng($tmp);break; case 'gif':$img=@imagegif($tmp);break; } } else { switch ($type) { case 'jpg':$img=@imagejpeg($tmp,$target,100);break; case 'png':$img=@imagepng($tmp,$target,0);break; case 'gif':$img=@imagegif($tmp,$target);break; } } if ($img) { //DID WE SAVE OUR NEW IMAGE? $result['width']=$newWidth; $result['height']=$newHeight; $result['name']=$target; @imagedestroy($src); @imagedestroy($tmp); $result['success']=true; } } } } if ($target!=false) { return $result; } } If I omit the header() statements the script works but instead of the image being shown I get the raw content of the file - as expected. How can I send the header() info so it displays? I've searched everywhere for examples and all I can find are examples that work in their own files - unless I'm reading something wrong somewhere. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 19, 2009 Author Share Posted April 19, 2009 Forgot to mention the entire site is contained within an ob_start() at the start of each page. EDIT:: Re-posted the updated code. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 19, 2009 Share Posted April 19, 2009 I've searched everywhere for examples and all I can find are examples that work in their own files - unless I'm reading something wrong somewhere.That's because that is the way it works. Browsers fetch media content on a page separately through the URL of that media content and for a image you need an <img src="URL_that_resutls_in_the_image_being_output_with_header" alt=""> tag in the link you are forming on the page. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 19, 2009 Author Share Posted April 19, 2009 Okies, thanks for the reply. I'll place the function inside a file of its own and call it that way instead. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 19, 2009 Author Share Posted April 19, 2009 Definitely solved - thumbs working just fine Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.