Resize an image and maintain quality?

Posted by JasonS on Stack Overflow See other posts from Stack Overflow or by JasonS
Published on 2011-01-03T14:45:00Z Indexed on 2011/01/03 14:53 UTC
Read the original article Hit count: 202

Filed under:
|

Hi, I have a problem with resizing images.

What happens is that if you upload a file larger than the stated parameters, the image is cropped, then saved at 100% quality.

So if I upload a large jpeg which is 272Kb. The image is cropped by 100 odd pixels. The file size then goes up to 1.2Mb.

We are saving images at a 100% quality. I assume that this is what is causing the problem. The image is exported from Photoshop at 30% quality which reduces the file size. Resaving the image at 100% quality creates the same image but I assume with a lot of redundant file data.

Has anyone encountered this before? Does anyone have a solution?

This is what we are using.

$source_im = imagecreatefromjpeg ($file);
$dest_im = imagecreatetruecolor ($newsize_x, $newsize_y);
imagecopyresampled (
    $dest_im, $source_im,
    0, 0,
    $offset_x, $offset_y,
    $newsize_x, $newsize_y,
    $sourceWidth, $sourceHeight
);

imagedestroy ($source_im);
if ($greyscale) {
    $dest_im = $this->imageconvertgreyscale ($dest_im);
}
imagejpeg($dest_im, $save_to_file, $quality);
break;

© Stack Overflow or respective owner

Related posts about php

Related posts about image-optimization