重新调整PHP-GD图像大小会降低图像质量
我已经编写了这段代码,如果找不到请求的图像,它将动态地重新调整图像大小,然后存储它 唯一的问题是输出的jpg图像质量低 我想知道是否有什么我需要改变,以提高图像质量重新调整PHP-GD图像大小会降低图像质量,php,php-gd,Php,Php Gd,我已经编写了这段代码,如果找不到请求的图像,它将动态地重新调整图像大小,然后存储它 唯一的问题是输出的jpg图像质量低 我想知道是否有什么我需要改变,以提高图像质量 if (isset($_GET['size'])) { $size = $_GET['size']; } if (isset($_GET['name'])) { $filename = $_GET['name']; } $filePath = "files/catalog/" . urldecode($filen
if (isset($_GET['size'])) {
$size = $_GET['size'];
}
if (isset($_GET['name'])) {
$filename = $_GET['name'];
}
$filePath = "files/catalog/" . urldecode($filename);
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filePath);
if ($_GET["name"] && !$size) {
$newwidth = $width * $percent;
$newheight = $height * $percent;
} else if ($_GET["name"] && $size) {
switch ($size) {
case "thumbs":
$newwidth = 192;
$newheight = 248;
break;
case "large":
$newwidth = 425;
$newheight = 550;
break;
}
}
$resizedFileName = $filename;
$resizedFileName = str_replace(".jpg", "", $resizedFileName) . ".jpg";
$resizedFilePath = "files/catalog/" . urldecode($resizedFileName);
if (!file_exists($resizedFilePath)) {
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filePath);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb, $resizedFilePath);
//file_put_contents($, $binarydata);
$imageContent = file_get_contents($resizedFilePath);
echo $imageContent;
} else {
$imageContent = file_get_contents($resizedFilePath);
echo $imageContent;
}
必须使用imagecopyresized()而不是imagecopyresized(),并且函数有第三个可选参数,可以指定质量。可能是因为使用错误,函数中的第二个变量表示质量百分比 你想要的。通过丢弃不必要的像素来调整大小。resampled()将平均计算结果,并生成更平滑的结果