我必须用 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');
}
}
你有想法吗?谢谢