2013-06-25 89 views
-2

我必須用PHP調整圖片大小。 但是...質量非常非常糟糕! (看圖)調整圖片質量而不損失

這裏我的代碼:

function _copyAndResizeImage($id, $wanted_width, $isAdvert=true) { 
// The file 
if ($isAdvert) { 
    $filename = "../upload/images/advert/".$id."/1.jpg"; 
} else { 
    $filename = "../upload/images/user/".$id.".jpg"; 
} 

list($width, $height) = getimagesize($filename); 

if ($width == 0) { 
    $percent=1; 
} else { 
    $percent = $wanted_width/$width; 
} 

// Content type 
header('Content-Type: image/jpeg'); 

// Get new dimensions 
$new_width = $width * $percent; 
$new_height = $height * $percent; 

// Resample 
$image_p = imagecreatetruecolor($new_width, $new_height); 
$image = imagecreatefromjpeg($filename); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// Output 
if ($isAdvert) { 
    imagejpeg($image_p, '../upload/images/advert/'.$id.'/1-'.$wanted_width.'.jpg'); 
} else { 
    imagejpeg($image_p, '../upload/images/user/'.$id.'-'.$wanted_width.'.jpg'); 
} 

} 

enter image description here

你有一個想法? 由於

+3

那麼,這就是擴大一個小圖像將要做的事,沒有辦法解決這個問題。你的原始圖像有多大? –

+0

我沒有得到源代碼,但是,源代碼的大小通常較大 –

+0

請嘗試像[phpThumb](http://phpthumb.sourceforge.net/)這樣的庫,或者在函數imagejpeg中添加圖像質量參數' – bystwn22

回答

0

imagejpeg JAS範圍從0到100的質量的第三可選參數:

布爾imagejpeg(資源$圖像[,字符串$文件名[摘要$質量]])

默認爲75應該很正常。

+0

甚至75%的圖像質量 - 你不會注意到他所看到的損失;對於低分辨率圖像,75和100之間的差別是標稱的。 –

3

由於您正在調整較小的基於光柵的(像素)圖像的大小,因此將其大小調整爲較大時會失去質量。這是預料之中的。如果您不希望發生這種情況,請使用SVG。