2016-07-09 8 views
0

サーバーにアップロード中にpngquantを使用してイメージ(jpegとpng)を圧縮しようとしています。私がpngquantウェブサイトから得たスクリプトは、pngファイルでは正常に動作していますが、jpegファイルではうまく動作していません。私はこのコードを使用しています(これはpngファイルで動作します):pngquantを使ってubuntuサーバー上の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); 
?> 

jpegファイルにpngquantを使用することはできますか?または、私のubuntuサーバーでphpを使ってこれを行うためにjpegファイル(jpegminiなど)を圧縮するための、より良い(無料の)ツールがあります。

答えて

0

私はpngquantがPNGファイルのためだけであることを確信しています。 JPGで使用するための同様のユーティリティがいくつかあります。既存のスクリプトimgopt(bashシェルスクリプト)を使用するだけでよいでしょう。これは次の場所にあります:imgopt

このスクリプトはうまくいきます(JPGとPNGの両方)、使いやすいです。それは完全にロスレスですので、かなり安全ですが、必要に応じてpngquantを追加することができます(pngquantは損失です)。最初にインストールする必要があるいくつかの必要なプログラムと、それを使用するように選択した場合は1つのバイナリ(pngout)があります。

私はスクリプトを書いていませんでしたが、私はかなり長い間使ってきました。

0

私が知っているように、Pngquantは、損失の多い方法でpng形式を圧縮するために使用されます。 jpegの場合は、出力するときに品質係数を使用することができます。

関連する問題