Hướng dẫn dùng png comression trong PHP

I have this piece of code for uploading images. It makes thumbnails with PNG extension with level 9 compression, but the images do not look good. I want just -50% or little more compression of PNG with transparency.

$path_thumbs = "../pictures/thumbs/";
$path_big = "../pictures/";
$img_thumb_width = 140; // 
$extlimit = "yes"; 
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");       
$file_type = $_FILES['image']['type'];
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];

if(!is_uploaded_file($file_tmp)){
    echo "choose file for upload!. 
--return"; exit(); } $ext = strrchr($file_name,'.'); $ext = strtolower($ext); if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { echo "dissallowed!
--return"; exit(); } $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; $rand_name = md5(time()); $rand_name= rand(0,10000); $ThumbWidth = $img_thumb_width; if($file_size){ if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ $new_img = imagecreatefromjpeg($file_tmp); }elseif($file_type == "image/x-png" || $file_type == "image/png"){ $new_img = imagecreatefrompng($file_tmp); }elseif($file_type == "image/gif"){ $new_img = imagecreatefromgif($file_tmp); } list($width, $height) = getimagesize($file_tmp); $imgratio = $width/$height; if ($imgratio>1){ $newwidth = $ThumbWidth; $newheight = $ThumbWidth/$imgratio; }else{ $newheight = $ThumbWidth; $newwidth = $ThumbWidth*$imgratio; } if (@function_exists(imagecreatetruecolor)){ $resized_img = imagecreatetruecolor($newwidth, $newheight); }else{ die("Error: Please make sure you have GD library ver 2+"); } imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); ImagePng ($resized_img, "$path_thumbs/$rand_name.$file_ext,9"); ImageDestroy ($resized_img); ImageDestroy ($new_img); } move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");

Hàm imagejpeg là nơi bạn chỉ định chất lượng. Nếu bạn đã đặt giá trị đó thành một giá trị thích hợp thì bạn không thể làm gì khác.

Tốc độ trang có thể coi tất cả hình ảnh trên một kích thước nhất định là "cần nén", có lẽ chỉ cần đảm bảo tất cả chúng đều nhỏ vừa phải (về chiều cao / chiều rộng) và được nén.

Bạn có thể tìm thêm về tốc độ trang và các đề xuất nén trên tài liệu về tốc độ trang http://code.google.com/speed/page-speed/docs/payload.html#CompressImages trong đó mô tả một số kỹ thuật / công cụ để nén thích hợp.

Tôi cũng vừa đọc những điều sau:

Một số công cụ có sẵn để thực hiện nén thêm, không mất dữ liệu trên các tệp JPEG và PNG mà không ảnh hưởng đến chất lượng hình ảnh. Đối với JPEG, chúng tôi đề xuất jpegtran hoặc jpegoptim (chỉ khả dụng trên Linux; chạy với tùy chọn --strip-all). Đối với PNG, chúng tôi đề xuất OptiPNG hoặc PNGOUT .

Vì vậy, có lẽ (nếu bạn thực sự muốn tuân theo các đề xuất của Google), bạn có thể sử dụng PHP execđể chạy một trong những công cụ đó trên các tệp khi chúng được tải lên.


Để nén bằng php, bạn làm như sau (có vẻ như bạn đang làm điều này):

Trong trường hợp $source_urllà hình ảnh, $destination_urllà nơi để lưu và $qualitylà một con số từ 1 đến 100 lựa chọn bao nhiêu nén jpeg để sử dụng.

function compressImage($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);

    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);

    //save file
    imagejpeg($image, $destination_url, $quality);

    //return destination file
    return $destination_url;
}

17 hữu ích 1 bình luận chia sẻ


Đây là một tập lệnh thu nhỏ rất tốt =) Đây là một ví dụ:

$ path = Đường dẫn đến thư mục chứa ảnh gốc. $ name = Tên tệp của tệp bạn muốn tạo hình thu nhỏ. $ thumbpath = Đường dẫn đến thư mục bạn muốn lưu hình thu nhỏ vào. $ maxwidth = chiều rộng tối đa của hình thu nhỏ trong PX, ví dụ. 100 (sẽ là 100px).

createThumbnail($path, $name, $thumbpath, $maxwidth);

10 hữu ích 0 bình luận chia sẻ