【发布时间】:2012-04-10 00:08:13
【问题描述】:
我正在尝试修改此脚本:http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
我想要(使用裁剪选项时)白色背景而不是现在的黑色。我的情况是这样的:
当我调整图像大小时,底部出现黑色边框 (1px),这是我不想要的。我希望这是白色的。
我查看了这个线程并尝试在脚本上实现它:How do I fill white background while resize image
但它似乎不起作用。这是我调整大小类的代码:
public function resizeImage($newWidth, $newHeight, $option="auto")
{
// *** Get optimal width and height - based on $option
$optionArray = $this->getDimensions($newWidth, $newHeight, $option);
$optimalWidth = $optionArray['optimalWidth'];
$optimalHeight = $optionArray['optimalHeight'];
// *** Resample - create image canvas of x, y size
$this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
// MY EDIT STARTS HERE
$backgroundColor = imagecolorallocate($this->imageResized, 255, 255, 255);
imagefill($this->imageResized, 0, 0, $backgroundColor);
// AND STOPS HERE
imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
// *** if option is 'crop', then crop too
if ($option == 'crop') {
$this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);
}
}
我做错了什么,我应该改变什么?
【问题讨论】:
-
未设置 CSS 边框。这只是一些图像得到这个黑色底部边框,而其他图像则没有。这是 - 我认为 - 因为他们(没有底部黑色边框)有另一个宽度/高度比例,然后他们得到底部边框。