我正在使用以下php函数来裁剪图像。裁剪图像时,图像背景看起来模糊。像贝娄图像。它应该是纯白色背景颜色:
模糊图片
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);
}
答案 0 :(得分:2)
您将$quality
值80提供给imagejpeg
。 JPEG is a lossy format。这会导致像你看到的那样的文物:
质量是可选的,范围从0(最差质量,较小文件)到100(最佳质量,最大文件)。默认值是默认的IJG质量值(约75)。
尝试使用更高的值:
imagejpeg($thumb, $filename, 90);
$quality
的最大值为100。