Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/255127-reducing-image-size-error-message/
Share on other sites

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.