0

The following works very well for png files, but for jpeg/jpg the compression doesn't work.

function compressImage($source, $destination, $quality) {
    $info = getimagesize($source);
    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source);
    imagejpeg($image, $destination, $quality);
    return $destination;
}

How do I get it to compress jpeg?

Joshua
  • 79
  • 1
  • 8

2 Answers2

0

In order to manipulate JPEG files with GD, your version of PHP needs to be compiled with libjpeg support built in. You can check this on phpinfo. Look for the following two:

  • JPEG Support enabled
  • libJPEG Version 6b (or different)

If you don't have those, ask your host.

Another issue that might come from this is that your mime type for jpeg images might come up as unknown. Double-check the library version - if it is there, comment back and I'll see if I can find a problem in your code by more than library introspection.

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66
0

The issue was with PHP.

Do:

yum install gd gd-devel

Joshua
  • 79
  • 1
  • 8