1

I am trying to lower the quality of large JPEG pictures (using PHP) and willing to loose some quality. using this code

function compress_image($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);

    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);

    //save it
    imagejpeg($image, $destination_url, $quality);

    //return destination file url
    return $destination_url;
}
$source_photo = $file;
$dest_photo = $newfile;
$d = compress_image($source_photo, $dest_photo, 70);

but the file (output) is actully bigger in size than the original (677kb vs 639kb) but I noticed the DPI when saved on my PC was bumped to 96 DPI ? How can prevent this ? any thoughts .. I am looking to make the file size smaller

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
zefrank
  • 53
  • 2
  • 6
  • Please fix the formatting of the code in your question. – ironchicken Feb 05 '17 at 22:38
  • Have you experimented with different `$quality` values? And would you consider scaling the image to make its dimensions smaller with `imagescale`? – ironchicken Feb 05 '17 at 22:41
  • @ironchicken - yes the file starts becoming smaller than the original when the imagescale drops below 60 – zefrank Feb 06 '17 at 00:00
  • related: http://stackoverflow.com/questions/19153122/image-compression-tools-via-command-line – ZiTAL Feb 06 '17 at 08:31
  • 1
    DPI doesn't have any effect on file size. What counts is width and height (together with colour depth and, of course, compression quality). Whatever, JPEG compression is not magic. To get size gain you need a poorly compressed file as a start. How are your source files? Why hard-coded 70? – Álvaro González Feb 06 '17 at 08:43

0 Answers0