简体   繁体   中英

PHP GD imagescale returns wrong image size

I'm using the GD library to rescale jpeg images, so that the resulting image has 100px width. I use the following code:

$image =  imagecreatefromjpeg('images/Image1.jpg');
$imageScaled = imagescale($image, 100);
// Save the image as 'simpletext.jpg'
imagejpeg($image, 'simpletext.jpg',90);
// Free up memory
imagedestroy($image);

The code appears to work correctly. For example if I use a 391x291 pixel image, I get a smaller image consistent with the change. The thing is that if I try to get info about the size of the (resized) image via my OS (Windows 8) or via PHP:

 $imageSize = getimagesize('simpletext.jpg');
 echo 'Image size: ' . $imageSize[0] . 'x' . $imageSize[1];

I get that the image dimension still is 391x291 pixel. Why is that?

You are saving the original image instead of the scaled one. Fix your code to do this:

imagejpeg($imageScaled, 'simpletext.jpg',90);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM