简体   繁体   中英

imagecopyresampled (jpg) give me black background

I have a problem with imagecopyresampled . So, I crop a picture (jpg), want to add white background, for the output picture ... and it doesn't work ...

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);
And, if I fill after the imagecopyresampled, it works just sometimes, cause, it adds me domes black borders ...

Any ideas? ...

Try putting your colour fill after the imagecopyresampled. It's unintuitive, I know, but that's GD for you...

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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