我有一个PHP脚本,保存原始图像,然后调整大小 - 一个缩略图和一个较大的图像,以供网页浏览。除了一些图像的质量很差外,这种方法效果很好。它似乎用非常低的颜色托盘保存。您可以在http://kalpaitch.com/index.php?filter=white看到的结果 - 点击标题为“白衣白”的第一个缩略图php imagecopyresampled质量差
下面是用于图像重采样代码:
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);
}
你能展示一个事前后的例子吗? – 2010-02-27 00:00:10
我不是100%清楚,但它听起来像你得到的一些图像是一定的大小,你的脚本实际上调整它大于它最初?如果你这样做,质量将会很糟糕。 – Jage 2010-02-27 00:02:56
如果只给出一个尺寸,则脚本不会按比例调整图像大小。如果给出两种尺寸,它将调整尺寸并裁剪其余部分而不改变宽高比。 重采样的图像总是比原始图像的分辨率低得多。上面的照片不是原始的,它们都是相同的分辨率,但之前用photoshop保存,而后一个用我的脚本保存。 – kalpaitch 2010-02-27 00:10:31