mahalleday Posted April 18, 2011 Share Posted April 18, 2011 I have written a php script to dynamically resize an image an covert it's file type to jpeg. <?php //include_once('ezuploader.php'); header('Content-Type: image/jpeg'); //get stuff $image = $_GET['image']; $size = $_GET['size']; $quality = $_GET['quality']; //make thumbnail method function makeThumb($image,$max_size,$quality){ if(file_exists($image)) { //get image extension $parts = explode('.',$image); $ext = $parts[1]; //get the width and height of origonal image list($ow, $oh) = getimagesize($image); //check if image is larger the $max_size if($ow > $max_size || $oh > $max_size) { //image dim(s) are larger therfore resize $ratio = $ow/$oh; //if width > height if($ow > $oh) { $nw = $max_size; $nh = $nw/$ratio; } //hegith > width else if($oh > $ow) { $nh = $max_size; $nw = $nh * $ratio; } //width and height are equal else { $oh = $max_size; $nh = $max_size; } } //the image is already an appropriate size else { $nw = $ow; $nh = $oh; }//end of image size check //decide which type of image is being sesized and convert to .jpeg switch($ext) { case "jpg": $new_image = imagecreatefromjpeg($image); break; case "jpeg": $new_image = imagecreatefromjpeg($image); break; case "png": $new_image = imagecreatefrompng($image); break; case "gif": $new_image = imagecreatefromgif($image); break; } // create blank image with new width and height $blank_image = imagecreatetruecolor($nw, $nh); //copy old image into new image with new dimensions imagecopyresampled($blank_image, $new_image, 0, 0, 0, 0, $nw, $nh ,$ow ,$oh); // Output imagejpeg($blank_image,NULL,$quality); //free memory imagedestroy($blank_image); } else { echo 'no files exists dumby...'; } }//end //make thumb makeThumb($image,$size,$quality); ?> This is how I intend to use it... <p><img src="resizer.php?image=image_to_be_resized.png&quality=75&size=150"/></p> However I get this error when I run it.... The image “http://mysite.com/resizer.php?image=localhost/wb/modules/adbaker/images/image_to_be_resized.png&quality=75&size=150” cannot be displayed because it contains errors. I worked when I was just passing jpeg files through and I see no reason why the conversion shouldn't work. any help as always is much appreciated. Mike. Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/ Share on other sites More sharing options...
dcro2 Posted April 18, 2011 Share Posted April 18, 2011 Remove the header() call and visit the page manually so you can see if any errors are being generated by PHP that would corrupt the image. Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1202801 Share on other sites More sharing options...
QuickOldCar Posted April 18, 2011 Share Posted April 18, 2011 you have to also set the header to switch mime types as well can't name the file a .png when your header mime type is reading as image/jpeg Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1202814 Share on other sites More sharing options...
mahalleday Posted April 18, 2011 Author Share Posted April 18, 2011 I'll try the header switch, I thought that might be it. Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1202821 Share on other sites More sharing options...
mahalleday Posted April 18, 2011 Author Share Posted April 18, 2011 Still having issues even with the switch for differnt mime types. The crazy thing is if i print out the $header and $mime variables to the screen they are correct for the type of image being resized. Here is the code again with the changes I have made.... <?php //get stuff $image = $_GET['image']; $size = $_GET['size']; $quality = $_GET['quality']; //make thumbnail method function makeThumb($image,$max_size,$quality){ //get image info list($ow, $oh, $type) = getimagesize($image); $mime = image_type_to_mime_type($type); //output header based on image type switch($mime) { case "image/jpg": $header = 'Content-Type: image/jpeg'; break; case "image/jpeg": $header = 'Content-Type: image/jpeg'; break; case "image/png": $header = 'Content-Type: image/png'; break; case "image/gif": $header = 'Content-Type: image/gif'; break; } header($header); //check if image is larger the $max_size if($ow > $max_size || $oh > $max_size) { //image dim(s) are larger therfore resize $ratio = $ow/$oh; //if width > height if($ow > $oh) { $nw = $max_size; $nh = $nw/$ratio; } //hegith > width else if($oh > $ow) { $nh = $max_size; $nw = $nh * $ratio; } //width and height are equal else { $oh = $max_size; $nh = $max_size; } } //the image is already an appropriate size else { $nw = $ow; $nh = $oh; }//end of image size check //decide which type of image is being sesized and convert to .jpeg switch($mime) { case "image/jpg": $new_image = imagecreatefromjpeg($image); break; case "image/jpeg": $new_image = imagecreatefromjpeg($image); break; case "image/png": $new_image = imagecreatefrompng($image); break; case "image/gif": $new_image = imagecreatefromgif($image); break; } // create blank image with new width and height $blank_image = imagecreatetruecolor($nw, $nh); //copy old image into new image with new dimensions imagecopyresampled($blank_image, $new_image, 0, 0, 0, 0, $nw, $nh ,$ow ,$oh); // Output resized image switch($mime) { case "image/jpg": imagejpeg($blank_image,NULL,$quality); break; case "image/jpeg": imagejpeg($blank_image,NULL,$quality); break; case "image/png": imagepng($blank_image); break; case "image/gif": imagegif($blank_image); break; } //free memory imagedestroy($blank_image); }//end //make thumb makeThumb($image,$size,$quality); ?> Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1202841 Share on other sites More sharing options...
PFMaBiSmAd Posted April 18, 2011 Share Posted April 18, 2011 If your goal was to convert the images to jpeg, you didn't need to make those changes to your code. You were using imagejpeg() to output the image, along with a matching 'Content-Type: image/jpeg' header. You need to troubleshoot what your code is doing. What is an actual URL (hide your actual domain name) you are using, because the error message implies you are supplying the following path to the image localhost/wb/modules/adbaker/images/image_to_be_resized.png and that is likely invalid unless you have created a folder named localhost/wb/....... relative to the page where the resizer.php script is located. Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1202845 Share on other sites More sharing options...
mahalleday Posted April 19, 2011 Author Share Posted April 19, 2011 The resize script is located in the same root directory the images folder is ie mysite.com/resizer.php and mysite.com/images. I have changed the path the be a relative one ie resizer.php?image=images/image.png. With this script I posted above both pngs and jpgs work fine the only issue is with gif images. I have added a check to see if the image I am trying to resize exists and that returns true like it should I just get the "The image “images/image.gif” cannot be displayed because it contains errors" error. Mike. Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1203558 Share on other sites More sharing options...
PFMaBiSmAd Posted April 19, 2011 Share Posted April 19, 2011 What GD Version and/or is GIF Read Support present? var_dump(gd_info()); Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1203564 Share on other sites More sharing options...
QuickOldCar Posted April 19, 2011 Share Posted April 19, 2011 If still having issues and need a decent thumbnailer use this. http://phpthumb.sourceforge.net/ And here's a direct download link. http://downloads.sourceforge.net/project/phpthumb/phpThumb%28%29/1.7.9/phpThumb_1.7.9.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fphpthumb%2Ffiles%2FphpThumb%2528%2529%2F&ts=1303233583&use_mirror=cdnetworks-us-1 I used to use it before I wrote my own. Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1203565 Share on other sites More sharing options...
mahalleday Posted April 20, 2011 Author Share Posted April 20, 2011 What GD Version and/or is GIF Read Support present? var_dump(gd_info()); Here is my GD info GD Support enabled GD Version bundled (2.0.34 compatible) FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.3.9 GIF Read Support enabled GIF Create Support enabled JPEG Support enabled libJPEG Version 6b PNG Support enabled libPNG Version 1.2.37 WBMP Support enabled XBM Support enabled I thought it might have something to do with my php settings but things look ok to me. I will try out the link suggested for phpThumb. In the end though I would prefer to write my own. Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1203841 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2011 Share Posted April 20, 2011 You do have GIF support. So, you need to debug why your code is failing to operate on a gif image. Perform the following steps - 1) Add the following two lines of php code, immediately after the first <?php tag in your resizer.php code - ini_set("display_errors", "1"); error_reporting(-1); 2) Comment out the Content-type: header() statement, so that you will see any php error messages and the raw image data. 3) Browse directly to the resizer.php?.... URL that you are putting into the <img src="..." attribute so that you can see the result of any output from the code. Are there any php errors reported? Also, posting your current code for resizer.php would help. Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1203941 Share on other sites More sharing options...
mahalleday Posted April 20, 2011 Author Share Posted April 20, 2011 Thanks I'll try that and see what the errors say if any are present. the code above is my most current code. Works for jpgs and pngs. Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1204053 Share on other sites More sharing options...
mahalleday Posted April 21, 2011 Author Share Posted April 21, 2011 I found the error. Would you believe it was just a typo?!? thanks for everyone's help on this. Here is the fully working code for others to make use of. <?php //get stuff $image = $_GET['image']; $max_size = $_GET['size']; $quality = $_GET['quality']; // if(file_exists($image)) { //get image info list($ow, $oh, $type) = getimagesize($image); $mime = image_type_to_mime_type($type); //output header based on image type switch($mime) { case "image/jpg": $header = 'Content-Type: image/jpeg'; break; case "image/jpeg": $header = 'Content-Type: image/jpeg'; break; case "image/png": $header = 'Content-Type: image/png'; break; case "image/gif": $header = 'Content-Type: image/gif'; break; } //header($header); //check if image is larger the $max_size if($ow > $max_size || $oh > $max_size) { //image dim(s) are larger therfore resize $ratio = $ow/$oh; //if width > height if($ow > $oh) { $nw = $max_size; $nh = $nw/$ratio; } //hegith > width else if($oh > $ow) { $nh = $max_size; $nw = $nh * $ratio; } //width and height are equal else { $nw = $max_size; $nh = $max_size; } } //the image is already an appropriate size else { $nw = $ow; $nh = $oh; }//end of image size check //decide which type of image is being sesized and convert to .jpeg switch($mime) { case "image/jpg": $new_image = imagecreatefromjpeg($image); break; case "image/jpeg": $new_image = imagecreatefromjpeg($image); break; case "image/png": $new_image = imagecreatefrompng($image); break; case "image/gif": $new_image = imagecreatefromgif($image); break; } // create blank image with new width and height $blank_image = imagecreatetruecolor($nw, $nh); //copy old image into new image with new dimensions imagecopyresampled($blank_image, $new_image, 0, 0, 0, 0, $nw, $nh ,$ow ,$oh); // Output resized image switch($mime) { case "image/jpg": imagejpeg($blank_image,NULL,$quality); break; case "image/jpeg": imagejpeg($blank_image,NULL,$quality); break; case "image/png": imagepng($blank_image); break; case "image/gif": imagegif($blank_image); break; } //free memory imagedestroy($blank_image); } else { echo "<p>File does not exist...</p>"; } ?> Thanks again. Mike. Quote Link to comment https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/#findComment-1204305 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.