我目前正在开发基于 Pligg CMS 的网站,它的默认图像上传模块将缩略图附加到用户提供的链接的帖子上,它使用 PHP 的 GD 库进行图像处理。结果的缩略图质量降低了,经过一点网络搜索后,我发现我应该将该imagecopyresized
功能替换为imagecopyresampled
.
主要问题是我是 Web 开发的新手,我不知道从哪里开始。我认为(因此可能是错误的)负责图像处理并需要编辑的代码块如下:
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
while (file_exists("$thumb_dir/$name$i.jpg")) $i++;
$name = "$name$i.jpg";
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
if (!imagejpeg( $tmp_img, "$thumb_dir/$name",$settings['quality'] ))
$error .= "Can't create thumbnail $thumb_dir/$name";
else
$db->query("INSERT INTO ".table_prefix."files
SET file_size='$size',
file_orig_id='$orig_id',
file_user_id={$current_user->user_id},
file_link_id=$link_id,
file_ispicture=1,
file_comment_id='".$db->escape($_POST['comment'])."',
file_real_size='".filesize("$thumb_dir/$name")."',
file_name='".$db->escape($name)."'");
}
return $error;
据我所知,图像首先通过imagecreatruecolor
函数处理成一个新tmp_img
的,然后通过imagecopyresized
函数处理。
由于我没有经验,我无法判断这是否是在不降低其质量的情况下调整 XY 大小图像的正确路径。我应该同时替换imagecreatetruecolor
和imagecopyresized
吗imagecopyresampled
?