2016-08-23 24 views
0

我正在使用下面的php函数来裁剪图像。当图像被裁剪时,图像背景看起来像模糊。像低音图像一样。它应该是纯白色的背景色:为什么使用PHP裁剪图像时图像背景颜色显示模糊?

模糊图像:

enter image description here

PHP代码为波纹管:

function crop_image ($target, $newcopy, $w, $h, $ext) {  

    $ext = strtolower($ext); 
    if ($ext == "gif"){ 
     $image = imagecreatefromgif($target); 
    } else if($ext =="png"){ 
     $image = imagecreatefrompng($target); 
    } else { 
     $image = imagecreatefromjpeg($target); 
    } 

    $filename = $newcopy; 
    $thumb_width = $w; 
    $thumb_height = $h; 
    $width = imagesx($image); 
    $height = imagesy($image); 
    $original_aspect = $width/$height; 
    $thumb_aspect = $thumb_width/$thumb_height; 
    if ($original_aspect >= $thumb_aspect) 
    {  
     $new_height = $thumb_height; 
     $new_width = $width/($height/$thumb_height); 
    } 
    else 
    {  
     $new_width = $thumb_width; 
     $new_height = $height/($width/$thumb_width); 
    } 
    $thumb = imagecreatetruecolor($thumb_width, $thumb_height);  
    $color = imagecolorallocate($thumb, 255, 255, 255); 
    imagefill($thumb, 0, 0, $color); 

    imagecopyresampled($thumb, 
         $image, 
         0 - ($new_width - $thumb_width)/2, // Center the image horizontally 
         0 - ($new_height - $thumb_height)/2, // Center the image vertically 
         0, 0, 
         $new_width, $new_height, 
         $width, $height); 
    imagejpeg($thumb, $filename, 80); 

} 

回答

2

你喂养的80 $quality值到imagejpegJPEG is a lossy format。这导致像你看到的文物:

质量是可选的,范围从0(最差的质量,较小的文件)到100(最好的质量,最大的文件)。默认值是默认的IJG质量值(约75)。

尝试使用更高的价值:

imagejpeg($thumb, $filename, 90); 

$quality的最大值为100

+0

OH让我做到这一点。 –

+0

你是对的:) –