I am running Linux Centos 6.5 and have installed jpegoptim.
to confirm this I ran yum install jpegoptim and got the following:
Package jpegoptim-1.4.4-1.e16.x86_64 already installed and latest version Nothing to do
When I run the following no compression happens, but the image is saved to the correct path and I get no error's.
function compress_jpg($path_to_jpg_file, $max_quality = 90)
{
if(!file_exists($path_to_jpg_file)){throw new Exception("File does not exist: $path_to_jpg_file");}
$min_quality = 60;
$compressed_jpg_content = shell_exec("jpegoptim --quality=$min_quality-$max_quality - < ".escapeshellarg($path_to_jpg_file));
if(!$compressed_jpg_content){throw new Exception("Conversion to compressed JPG failed. Is jpegoptim installed on the server?");}
return $compressed_jpg_content;
}
$read_from_path = "image-old/cleveland-corner.jpg";
$save_to_path = "image-new/compressed-cleveland-corner.jpg";
$compressed_jpg_content = compress_jpg($read_from_path);
file_put_contents($save_to_path, $compressed_jpg_content);
When I run the following I get a image file with nothing in it saved to the correct path and I get no error's.
function compress_jpg($path_to_jpg_file)
{
$command = 'jpegoptim '.$path_to_jpg_file;
shell_exec($command);
return $compressed_jpg_content;
}
$read_from_path = "image-old/cleveland-corner.jpg";
$save_to_path = "image-new/compressed-cleveland-corner.jpg";
$compressed_jpg_content = compress_jpg($read_from_path);
file_put_contents($save_to_path, $compressed_jpg_content);
Has anyone had any luck calling and compressing jpegoptim use shell_exec from PHP?
Well I found the answer to my own question:
I change this line from my first function example
To the following:
It really came down to knowing the jpegoptim options and how to refer to them. Compressed the sample image from 54,318 bytes down to 27,999 bytes.