2016-07-09 5 views
0

내 서버에 업로드하는 동안 pngquant를 사용하여 이미지 (jpeg 및 png)를 압축하려고합니다. pngquant 웹 사이트에서 얻은 스크립트는 png 파일에는 잘 작동하지만 jpeg 파일에는 적합하지 않습니다. 이 코드는 png 파일에서 사용됩니다.PHP를 사용하여 우분투 서버에서 jpeg 파일을 압축하는 데 pngquant를 사용하는 방법

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를 사용할 수 있습니까? 아니면 PHP를 사용하여 내 우분투 서버 에서이 일을 (jpegmini 유료)와 같은 jpeg 파일을 압축하기위한 더 나은 (무료) 도구입니다.

답변

0

저는 pngquant가 PNG 파일에만 사용됩니다. JPG에 사용할 수있는 유사한 유틸리티가 많이 있습니다. 기존 스크립트 인 imgopt (bash 쉘 스크립트) 만 사용하는 것이 좋습니다. 다음 위치에 있습니다 : imgopt

이 스크립트는 (JPG 및 PNG 모두) 잘 작동하며 사용하기가 쉽습니다. 그것은 완전히 무손실이므로 매우 안전합니다. 그러나 원하는 경우 pngquant를 추가 할 수 있어야합니다 (pngquant는 손실입니다). 먼저 설치해야하는 몇 가지 필수 프로그램과 사용하기로 한 경우 하나의 이진 파일 (pngout)이 있습니다.

나는 스크립트를 작성하지 않았지만 방금 사용했습니다.

0

Pngquant는 손실 된 방식으로 png 형식을 압축하는 데 사용됩니다. jpeg의 경우 출력 할 때 품질 요소를 사용할 수 있습니다.