0

我目前正在开发基于 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 大小图像的正确路径。我应该同时替换imagecreatetruecolorimagecopyresizedimagecopyresampled

4

1 回答 1

1

imagecopyresized并且imagecopyresampled具有相同的参数要求,因此您可以简单地更改以下行中的函数名称:

imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

其他一切都应该保持不变。

请注意,您的代码容易受到SQL 注入的攻击。阅读并开始使用准备好的语句

于 2016-05-22T20:55:29.243 回答