jpegoptim PHP shell_exec不压缩图像


jpegoptim PHP shell_exec not compressing images

我正在运行Linux Centos 6.5并安装了jpegoptim。

为了确认这一点,我运行了 yum 安装 jpegoptim 并得到以下内容:

软件包 jpegoptim-1.4.4-1.e16.x86_64 已安装和最新版本无事可做

当我运行以下内容时,不会发生压缩,但是图像已保存到正确的路径,并且没有出现任何错误。

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);

当我运行以下内容时,我得到一个图像文件,其中没有任何内容保存到正确的路径,并且没有收到任何错误。

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);

有没有人运气从PHP调用和压缩jpegoptim使用shell_exec?

好吧,我找到了我自己问题的答案:

我从我的第一个函数示例中更改了这一行

$compressed_jpg_content = shell_exec("jpegoptim --quality=$min_quality-$max_quality - < ".escapeshellarg($path_to_jpg_file));

到以下内容:

$compressed_jpg_content = shell_exec("jpegoptim --max=75 --strip-all --all-progressive - < ".escapeshellarg($path_to_jpg_file));

这实际上归结为了解 jpegoptim 选项以及如何引用它们。将示例图像从 54,318 字节压缩到 27,999 字节。