简体   繁体   English

imagecopyresampled(jpg)给我黑色背景

[英]imagecopyresampled (jpg) give me black background

I have a problem with imagecopyresampled . 我对imagecopyresampled有问题。 So, I crop a picture (jpg), want to add white background, for the output picture ... and it doesn't work ... 因此,我裁剪了图片(jpg),想为输出图片添加白色背景,但效果不佳...

Here is my code: 这是我的代码:

$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);

So, it work, but, the background is black, and not white ... 因此,它可以工作,但是背景是黑色,而不是白色...
Here is the output picture ... 这是输出图片...
在此处输入图片说明
EDIT: 编辑:
Passed values are (0, 0, 31, -50, 110, 110, 110, 110); 传递的值为(0,0,31,-50,110,110,110,110);
And, if I fill after the imagecopyresampled, it works just sometimes, cause, it adds me domes black borders ... 而且,如果我在对imagecopy重新采样后填充,它有时会起作用,原因是,它为我添加了圆顶黑色边框...

Any ideas? 有任何想法吗? ... ...

Try putting your colour fill after the imagecopyresampled. 图像副本重新采样后,尝试填充颜色。 It's unintuitive, I know, but that's GD for you... 我知道这很不直观,但这就是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);

If that doesn't (appear) to work, check the values you're passing into imagecopyresampled as the co-ords and dimensions - it's possible to make the copied image bigger than the canvas (or itself) which will bring a default black background with it. 如果这样(看起来)不起作用,请检查要传递给imagecopy的值,以坐标和尺寸重新采样-可以使复制的图像大于画布(或本身),这会带来默认的黑色背景用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM