gpt4 book ai didi

php imagecopyresampled 质量差

转载 作者:可可西里 更新时间:2023-10-31 22:07:40 25 4
gpt4 key购买 nike

我有一个 php 脚本,它可以保存原始图像,然后调整它的大小 - 一个缩略图和一个较大的图像供网络查看。这很好用,除了一些图像质量很差。它似乎是用一个非常低的颜色托盘保存的。您可以在 http://kalpaitch.com/index.php?filter=white 查看结果- 单击标题为“白色白色白色”的第一个缩略图

下面是用于图像重采样的代码:

function resizeImg($name, $extension, $size1, $size2) {
if (preg_match('/jpg|jpeg|JPG|JPEG/',$extension)){
$image = imagecreatefromjpeg($name);
}
if (preg_match('/gif|GIF/',$extension)){
$image = imagecreatefromgif($name);
}

$old_width = imageSX($image);
$old_height = imageSY($image);
$old_aspect_ratio = $old_width/$old_height;

if($size2 == 0){
$new_aspect_ratio = $old_aspect_ratio;
if($old_width > $old_height){
$new_width = $size1;
$new_height = $new_width / $old_aspect_ratio;
} else {
$new_height = $size1;
$new_width = $new_height * $old_aspect_ratio;
}
} elseif($size2 > 0){
$new_aspect_ratio = $size1/$size2;
//for landscape potographs
if($old_aspect_ratio >= $new_aspect_ratio) {
$x1 = round(($old_width - ($old_width * ($new_aspect_ratio/$old_aspect_ratio)))/2);
$old_width = round($old_width * ($new_aspect_ratio/$old_aspect_ratio));
$y1 = 0;
$new_width = $size1;
$new_height = $size2;
//for portrait photographs
} else{
$x1 = 0;
$y1 = 0;
$old_height = round($old_width/$new_aspect_ratio);
$new_width = $size1;
$new_height = $size2;
}
}

$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, $x1, $y1, $new_width, $new_height, $old_width, $old_height);

return $new_image;

非常感谢

附言[从服务器中删除的照片]

这是上传代码的其余部分:

// Move the original to the right place
$result = @move_uploaded_file($image['tmp_name'], $origlocation);

// Resize the image and save the thumbnail
$new_image = resizeImg($origlocation, $extension, 500, 0);

if (preg_match("/gif/",$extension)){
imagegif($new_image, $normallocation);
} else {
imagejpeg($new_image, $normallocation);
}

// Resize the image and save the thumbnail
$new_image = resizeImg($origlocation, $extension, 190, 120);

if (preg_match("/gif/",$extension)){
imagegif($new_image, $thumblocation);
} else {
imagejpeg($new_image, $thumblocation);
}

最佳答案

质量下降不是因为 imagecopyresampled(),而是因为 JPEG 压缩。不幸的是,GD 的压缩算法无法与 Photoshop 的相提并论——事实上,几乎没有。但是您可以改进结果:GD 默认的 JPG 压缩级别是 75 of 100。

您可以使用第三个参数将质量提高到 imagejpeg() (我假设您将其用于最终输出):

imagejpeg  ( $new_image, null, 99);

在 90-100 范围内玩。图像的文件大小将比原始文件大 - 这将是您支付的价格。但应该可以达到相当的质量。

或者,正如 John Himmelman 在评论中所说,尝试使用 imagepng() 以获得更好的质量 - 当然,代价是文件大小明显变大。

关于php imagecopyresampled 质量差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2345605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com