2013-06-25 88 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。