imagecopyresampled(jpg)给我黑色背景

时间:2014-02-09 14:01:34

标签: php gd

我对imagecopyresampled有疑问。所以,我裁剪了一张图片(jpg),想要为输出图片添加白色背景......它不起作用......

这是我的代码:

$this->imageResized = imagecreatetruecolor($sizes['width'], $sizes['height']);
$white = imagecolorallocate($this->imageResized, 255, 255, 255);
imagefill($this->imageResized, 0, 0, $white);
imagecopyresampled($this->imageResized, $this->image, 0, 0, (($selected_x * $ratio) - ($blanc_x * $ratio)), (($selected_y * $ratio) - ($blanc_y * $ratio)), $sizes['width'], $sizes['height'], ($selected_width * $ratio), ($selected_height * $ratio));
imagejpeg($this->imageResized, $savePath, $imageQuality);

所以,它有效,但是,背景是黑色的,而不是白色...... 这是输出图片...
enter image description here
编辑:
传递值为(0,0,31,-50,110,110,110,110);
并且,如果我在imagecopyresampled之后填写,它有时会起作用,因为它会增加我的圆顶黑色边框......

有什么想法吗? ...

1 个答案:

答案 0 :(得分:1)

尝试在imagecopyresampled后放置颜色填充。我知道这不直观,但那是你的GD ......

$this->imageResized = imagecreatetruecolor($sizes['width'], $sizes['height']);

imagecopyresampled($this->imageResized, $this->image, 0, 0, (($selected_x * $ratio) - ($blanc_x * $ratio)), (($selected_y * $ratio) - ($blanc_y * $ratio)), $sizes['width'], $sizes['height'], ($selected_width * $ratio), ($selected_height * $ratio));

$white = imagecolorallocate($this->imageResized, 255, 255, 255);
imagefill($this->imageResized, 0, 0, $white);

imagejpeg($this->imageResized, $savePath, $imageQuality);

如果没有(显示)起作用,请检查您传入imagecopyresampled的值作为co-ords和dimension - 可以使复制的图像大于画布(或其自身),这将带来默认的黑色背景。