如何使用pngquant在ubuntu服务器上使用php压缩jpeg文件
我正在尝试使用pngquant在上传到我的服务器时压缩图像(jpeg和png)。我从pngquant网站上得到的脚本对于png文件来说效果很好,但对于jpeg文件来说效果不好。我使用的代码(适用于png文件):如何使用pngquant在ubuntu服务器上使用php压缩jpeg文件,php,image,ubuntu,pngquant,Php,Image,Ubuntu,Pngquant,我正在尝试使用pngquant在上传到我的服务器时压缩图像(jpeg和png)。我从pngquant网站上得到的脚本对于png文件来说效果很好,但对于jpeg文件来说效果不好。我使用的代码(适用于png文件): 函数compress\u png($path\u to\u png\u file,$max\u quality=90) { 如果(!file_存在($path_to_png_file)){ 抛出新异常(“文件不存在:$path_to_png_File”); } //保证质量不会比那差。
函数compress\u png($path\u to\u png\u file,$max\u quality=90)
{
如果(!file_存在($path_to_png_file)){
抛出新异常(“文件不存在:$path_to_png_File”);
}
//保证质量不会比那差。
$min_质量=60;
//“-”使其使用stdout,这是保存到$compressed\u png\u内容变量所必需的
// '
pngquant可以用于jpeg文件吗?或者有没有更好的(免费)压缩jpeg文件的工具(比如付费的jpegmini)要在我的Ubuntu服务器上使用PHP.< /P> < p>这样做,我非常确信PngQuANT只适用于PNG文件。在JPGS上有许多类似的实用程序。您可能需要考虑只使用现有脚本IMGopt(Bash shell脚本)。它位于这里: 这个脚本运行良好(JPG和PNG),非常容易使用。它是完全无损的,因此相当安全,但如果您愿意,您应该能够在其中添加pngquant(pngquant是有损的)。您需要先安装一些必需的程序,如果您选择使用它,还需要一个二进制文件(pngout)
我没有编写脚本,只是用了很长时间。据我所知,Pngquant用于以有损方式压缩png格式。对于jpeg,您可以在输出时使用质量因子
function compress_png($path_to_png_file, $max_quality = 90)
{
if (!file_exists($path_to_png_file)) {
throw new Exception("File does not exist: $path_to_png_file");
}
// guarantee that quality won't be worse than that.
$min_quality = 60;
// '-' makes it use stdout, required to save to $compressed_png_content variable
// '<' makes it read from the given file path
// escapeshellarg() makes this safe to use with any path
$compressed_png_content = shell_exec("pngquant --quality=$min_quality-$max_quality - < ".escapeshellarg( $path_to_png_file));
if (!$compressed_png_content) {
throw new Exception("Conversion to compressed PNG failed. Is pngquant 1.8+ installed on the server?");
}
return $compressed_png_content;
}
/****************************************************/
$download_url = "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png";
// maximum execution time in seconds
set_time_limit (24 * 60 * 60);
// folder to save downloaded files to. must end with slash
$destination_folder = '/IMAGES/';
$ImageName = "testing_pngquant_image3.png";
$CompressedImageName = "testing_pngquant_image_compressed3.png";
$url = $download_url;
$newfname = $destination_folder . $ImageName;
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
$path_to_uncompressed_file = $newfname;
$path_to_compressed_file = $destination_folder . $CompressedImageName;
// this will ensure that $path_to_compressed_file points to compressed file
// and avoid re-compressing if it's been done already
if (!file_exists($path_to_compressed_file)) {
file_put_contents($path_to_compressed_file, compress_png($path_to_uncompressed_file));
}
// and now, for example, you can output the compressed file:
header("Content-Type: image/png");
header('Content-Length: '.filesize($path_to_compressed_file));
readfile($path_to_compressed_file);
?>