:) 我在另一篇文章中发现了这一行代码,它使用 pngquant 成功压缩了图像。问题是,它输出具有不同名称的优化图像(显然是为了保留原始图像)。
我试图找到一种方法:
a) 添加最低质量参数 60 b) 使用 if/else 语句允许用户选择覆盖现有文件或输出新的优化图像(用户指定的名称)
谢谢你!ntlri - 不要长时间阅读它
<?php system('pngquant --quality=85 image.png'); ?>
所以我尝试过的是以下..由于某种原因,单引号需要是双引号才能正确解析变量..
<?php
$min_quality = 60; $max_quality = 85;
$keep_original = 'dont_keep';
if ($keep_original == 'keep') {
$image_name = 'image.png';
$path_to_image = 'images/' . $image_name;
$new_file = 'image2.png';
$path_to_new_image = 'images/' . $new_file;
// don't know how to output to specified $new_file name
system("pngquant --quality=$min_quality-$max_quality $path_to_image");
} else {
$image_name = 'image.png';
$path_to_image = 'images/' . $image_name;
// don't know if you can overwrite file by same name as additional parameter
system("pngquant --quality=$min_quality-$max_quality $path_to_image");
// dont't know how you get the name of the new optimised image
$optimised_image = 'images/' . $whatever_the_optimised_image_is_called;
rename($optimised_image, $image_name);
unlink($optimised_image);
}
?>