2016-07-09 5 views
0

Ich versuche, pngquant verwenden, um Bilder (JPEG und PNG) beim Hochladen auf meinen Server zu komprimieren. Das Skript, das ich von der pngquant-Website bekommen habe, funktioniert gut für PNG-Dateien, aber nicht für JPEG-Dateien. Ich verwende diesen Code (die für PNG-Dateien funktioniert):Wie pngquant zum Komprimieren von JPEG-Dateien auf Ubuntu-Server mit Hilfe von PHP

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); 
?> 

Kann überhaupt für JPEG-Dateien verwendet werden pngquant? Oder gibt es ein besseres (kostenloses) Werkzeug zum Komprimieren von JPEG-Dateien (wie jpegmini, das bezahlt wird), um dies auf meinem Ubuntu-Server mit PHP zu tun.

Antwort

0

Ich bin mir ziemlich sicher, dass Pngquant nur für PNG-Dateien ist. Es gibt eine Reihe ähnlicher Dienstprogramme für JPGs. Vielleicht möchten Sie nur das vorhandene Skript imgopt (bash shell script) verwenden. Es befindet sich hier: imgopt

Dieses Skript funktioniert gut (JPG und PNG) und ist sehr einfach zu bedienen. Es ist völlig verlustfrei, also ist es ziemlich sicher, aber Sie sollten in der Lage sein, pngquant dort hineinzufügen, wenn Sie wollen (pngquant ist verlustbehaftet). Es gibt einige erforderliche Programme, die Sie zuerst installieren müssen, und eine Binärdatei, wenn Sie sie verwenden möchten (pngout).

Ich habe das Skript nicht geschrieben, ich habe es nur für eine ganze Weile benutzt.

0

Wie ich weiß, Pngquant wird verwendet, um Png-Format auf eine verlustreiche Weise zu komprimieren. Für JPEG können Sie den Qualitätsfaktor verwenden, wenn Sie es ausgeben.