我有一个重新采样脚本,将重采样的图像集中在方形画布上。 对于在白色背景上垂直居中的水平方向图像,沿着重采样图像的BOTTOM边缘添加一条线。 对于水平居中的垂直方向图像,该线条出现在重采样图像的右边缘。
$thumb = imagecreatetruecolor($th_width, $th_height);
imagecopyresampled($thumb, $source, $th_x, $th_y, 0, 0, $th_width, $th_height, $src_width, $src_height);
$bgcolor = imagecolorallocate($thumb, 255, 255, 255);
imagefill($thumb, 0, 0, $bgcolor);
无论背景填充如何,线都在那里,它只显示在白色上。
是什么原因引起的?没有多少调整参数值会消除它(它们只是在背景上偏移重新采样的图像或扭曲它)。
答案 0 :(得分:0)
我正在为此问题提供自己的解决方法。 看起来图像边界伪像是'imagecopyresampled()'和中心偏移的COMBINATION结果。如果我只是将图像重新采样为THEN中心并填充图像,则避免使用边框。这是解决方法:
1)按原样重新采样图像(相同的宽高比),然后保存以保留更改:
$thumb = imagecreatetruecolor($res_width, $res_height);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $res_width, $res_height, $src_width, $src_height);
// SAVE new image - set quality to lossless since reusing it:
imagejpeg($thumb, "resampled/output_temp.jpg", 100);
2)检索临时图像:
$file = "resampled/output_temp.jpg";
$image = file_get_contents($file);
$source = imagecreatefromstring($image);
// Get the RESAMPLED image dimensions
list($src_width, $src_height) = getimagesize($file);
3)现在申请CENTERING和FILL:
$thumb = imagecreatetruecolor($th_width, $th_height);
// COPY the image
imagecopy($thumb, $source, $th_x, $th_y, 0, 0, $src_width, $src_height);
$bgcolor = imagecolorallocate($thumb, 255, 255, 255);
// Replace default black background - this must be placed AFTER imagecopy()
imagefill($thumb, 0, 0, $bgcolor);
// Save image (uses default quality setting 75)
imagejpeg($thumb, "resampled/" . $newfile);
// Optionally free up memory:
imagedestroy($thumb);
结果是干净的图像。 为了在垂直方向图像居中时修正背景上不完整的图像填充(),请参阅此帖子: imagecopyresampled() results in split color background imagefill()