PHP-使用pngquant进行图像压缩的简单脚本
:)我在另一篇文章中发现了这1行代码,它使用pngquant成功地压缩了图像。问题是,它以不同的名称输出优化后的图像(显然是为了保留原始图像) 我试图找到一种方法: a) 添加最小质量参数60 b) 使用if/else语句允许用户选择覆盖现有文件或输出新的优化图像(用户指定的名称) 谢谢大家!!ntlri-不要看太久PHP-使用pngquant进行图像压缩的简单脚本,php,image,optimization,compression,pngquant,Php,Image,Optimization,Compression,Pngquant,:)我在另一篇文章中发现了这1行代码,它使用pngquant成功地压缩了图像。问题是,它以不同的名称输出优化后的图像(显然是为了保留原始图像) 我试图找到一种方法: a) 添加最小质量参数60 b) 使用if/else语句允许用户选择覆盖现有文件或输出新的优化图像(用户指定的名称) 谢谢大家!!ntlri-不要看太久 <?php system('pngquant --quality=85 image.png'); ?> 因此,我尝试了以下几点。。出于某种原因,单引号必须是双引号
<?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);
}
?>
以下文件中的:
输出文件名与输入名称相同,只是\n\n
以\“-fs8.png\”、\“-or8.png\”或您的自定义扩展名结尾
因此,对于这个问题:
要选择新名称,请假设您正在压缩imagename.png
:
--ext=_x.png
这将创建名为name\u x.png的新图像
因此,您的$new_文件
只是一个后缀
$new_file = '_x.png'; // to choose new file name name_x.png
//不知道是否可以用与其他文件相同的名称覆盖该文件
参数
如程序文档中所述,新文件名的后缀为-fs8.png
或-or8.png
,因此您可以重命名将使用此后缀生成的文件,或,只需将--ext
选项设置为:.png
,这将附加到原始文件中
--ext=.png
有关更多详细信息,请查看我与开发pngquant的chappie pornel交谈过。其实比我以前写的要简单得多
!!重要信息-使用escapeshellarg()非常重要,否则人们可以通过上传带有特殊文件名的文件来接管您的服务器
$image_name = 'image.png';
$target_file = 'images/' . $image_name;
$existing_image = 'image.png'; // image already on server if applicable
$keep = 'keep';
$target_escaped = escapeshellarg($target_file);
if ($keep == 'keep') {
// process/change output file to image_compressed.png keeping both images
system("pngquant --force --quality=70 $target_escaped --ext=_compressed.png");
$remove_ext = substr($newFileName, 0 , (strrpos($newFileName, ".")));
// $new_image is just the name (image_compressed.png) if you need it
$new_image = $remove_ext . '_compressed.png';
// remove old file if different name
if ($existing_image != $newFileName) {
$removeOld = '../images/' . $existing_image;
unlink($removeOld);
} // comment out if you want to keep existing file
} else {
// overwrite if file has the same name
system("pngquant --force --quality=70 $target_escaped --ext=.png");
// remove old file if different name
if ($existing_image != $newFileName) {
$removeOld = '../images/' . $existing_image;
unlink($removeOld);
}
$new_image = $newFileName;
}
很抱歉选择了我的答案,下次我肯定会阅读存储库,但您编写的解决方案——正如我认为和从程序文档中理解的那样——将始终覆盖在原始图像上
$image_name = 'image.png';
$target_file = 'images/' . $image_name;
$existing_image = 'image.png'; // image already on server if applicable
$keep = 'keep';
$target_escaped = escapeshellarg($target_file);
if ($keep == 'keep') {
// process/change output file to image_compressed.png keeping both images
system("pngquant --force --quality=70 $target_escaped --ext=_compressed.png");
$remove_ext = substr($newFileName, 0 , (strrpos($newFileName, ".")));
// $new_image is just the name (image_compressed.png) if you need it
$new_image = $remove_ext . '_compressed.png';
// remove old file if different name
if ($existing_image != $newFileName) {
$removeOld = '../images/' . $existing_image;
unlink($removeOld);
} // comment out if you want to keep existing file
} else {
// overwrite if file has the same name
system("pngquant --force --quality=70 $target_escaped --ext=.png");
// remove old file if different name
if ($existing_image != $newFileName) {
$removeOld = '../images/' . $existing_image;
unlink($removeOld);
}
$new_image = $newFileName;
}