johnhenry Posted January 16, 2012 Share Posted January 16, 2012 I have been trying to get this to work for ages and cannot see what is wrong. Some help would be appreciated. My script is... <?php // Get image from file $filename = 'image-files/my.jpg'; $reduction = .25; // Content type header('Content-type: image/jpeg'); // Get dimensions and change to those wanted list($width, $height) = getimagesize($filename); $new_width = $width * $reduction; $new_height = $height * $reduction; // Resample $resized_image = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Save back to original file imagejpeg($resized_image, $filename); ImageDestroy($resized_image); ImageDestroy($image); ?> It works fine, doing the job I want, which is to resize the image and save it to the original file. However I also get an error message that is the usual vague line .... The image “bla-bla-bla” cannot be displayed, because it contains errors. I guess there is something I haven't managed to get right but I cannot work it out. Can anyone throw some light on the reason for this please? Quote Link to comment https://forums.phpfreaks.com/topic/255127-reducing-image-size-error-message/ Share on other sites More sharing options...
johnhenry Posted January 16, 2012 Author Share Posted January 16, 2012 I have looked at this problem again - (and again) - and I now notice that the error message which I reported as being .... 'The image “bla-bla-bla” cannot be displayed, because it contains errors' ..... is actually pointing to the answer. I put "bla bla bla" in instead of the URL that it gave. The URL it gave was the location and name of the php script and not the image. So it seems that it is trying to print the script to the page. I'm still baffled. Quote Link to comment https://forums.phpfreaks.com/topic/255127-reducing-image-size-error-message/#findComment-1308132 Share on other sites More sharing options...
PFMaBiSmAd Posted January 16, 2012 Share Posted January 16, 2012 When exactly do you get that error message? When you are resizing the image or when you later try to place the resized image on a web page? What is the code you use to invoke the script you posted? What is the code you use to place the resized image onto a web page? Edit: The reason I ask this is because you don't output a content-type header unless you are dynamically outputting the image to the browser, not when you are saving it to a file. Quote Link to comment https://forums.phpfreaks.com/topic/255127-reducing-image-size-error-message/#findComment-1308135 Share on other sites More sharing options...
johnhenry Posted January 16, 2012 Author Share Posted January 16, 2012 I was running the script exactly as is. I was planning to integrate it into a larger script when I got this part working correctly. The error message appears after the new image has been saved back to overwrite the original. I guess you have given me the answer. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/255127-reducing-image-size-error-message/#findComment-1308140 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.