我正在运行Linux Centos 6.5并安装了jpegoptim。
确认这个我运行yum install 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);
有没有人有运气和压缩jpegoptim运气从PHP使用shell_exec?
答案 0 :(得分:0)
我找到了自己问题的答案:
我从第一个函数示例
更改了这一行$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字节。