View Full Version : ImageCopyResampled - How to avoid default black background?

08-28-2006, 08:30 AM
Hi All,

I would like to know how to avoid the default black background color in ImageCopyResampled if origin and destination do not fit.

ImageCopyResampled puts always a black background and if I try to fill it with another color and with ImageFill function, ... I get problems: Some images with a deep dark colors predominance or even with a deep light color predominance too, are filled with my color, as you know, applied with ImageFill.

Here is some example code:

$im = ImageCreateTrueColor($nx, $ny);
ImageInterlace($im, 1);
ImageCopyResampled($im, $im2, 0, 0, $ox, $oy, $nx, $ny, $nx, $ny);
ImageFill($im, 0, 0, ImageColorAllocate($im, 191, 225, 216));What do you think? I spent two days doing many combinations, and using other options like ImageColorClosest, ImageAlphaBlending, ImageColorTransparent. I couldn't get anything. Maybe I didn't use these functions rightly. I guess.

If I use ImageFill before ImageCopyResampled, I get the same result, ImageCopyResampled puts its black background, if on contrary, as I mentioned, there are problems with darken and lighten images.

I know that ImageCreateTrueColor default color is black too, but setting another color with ImageFill or ImageColorTransparent before ImageCopyResampled doesn't work because looks like ImageCopyResampled puts another ImageCreateTrueColor and its black color at top of image.

ImageInterlace is not relevant for the problem, I have researched this too.

I am desperate, indeed.


08-28-2006, 03:27 PM
Did you try using Imagecreate instead of Imagecreatetruecolor?

08-28-2006, 10:41 PM
I got the same bad result with Imagecreate, but even worst because images had lower quality than Imagecreatetruecolor.

Try by yourself with ImageCopyResampled and retouch the sizes to force images do not fit, and you will always see a black background around the copied image.

How to replace it by your own color? That's the question.

Any idea?



08-29-2006, 12:19 AM
Let me explain you what I want to do a little bit more.

I have developed an automatic system to crop images for a gallery. Works OK.

The question is that when images are portraits or have a human as target, I put 15 pixels down the original imagen taken by ImageCopyResampled (this has a good reason), so when original imagen has enough space in its top zone, final image fits well and ImageCopyResampled doesn't put any black background.

... But not on the contrary case, because ImageCopyResampled try to fill the whole width and height pixels with something, and that something is a black color.

As it is impossible to know when a image is a portrait or has a human as protagonist, (the gallery is a mixture of subjects), I try to put the same color that website background has, which is a light color.

Realize how bad looks the gallery with 15 pixels of black in some images over a light background, because of this I am worry with this matter.

Sometimes I think that internally, ImageCopyResampled call another ImageCreateTrueColor (which has a black background color by default) to put the original image over the final image and we cannot do anything to solve it apart to take GD source, modify the default color used by ImageCopyResampled and compile GD again. Bad stuff.

Maybe PHP team should add a default background color option in ImageCopyResampled command line.

Some idea?

Thank you very much for your help.


08-29-2006, 12:22 AM
You could use imagefilledrectangle (http://uk.php.net/manual/en/function.imagefilledrectangle.php) to create a white rectange as the background.

08-29-2006, 12:53 AM
I tried that before. Obviously If I put ImageFilledRectangle after ImageCopyResampled, fill out all image with that color. :-) I even tried that :-)

If I put it before ImageCopyResampled, this function continues putting its black background color over the final image in the zones that are not matching.

Thank you for your help


08-29-2006, 01:17 AM
I mentioned the Imagecreate because that fixed a problem I was having with a black background, but it was a transparency issue with gifs, so I'm afraid it's not the same issue you're having.

I'm sorry I don't know what's going on with your issue. :(

Are you trying to keep all the images in the gallery the same dimmensions, is that why you want margins? I just don't quite understand where the black blackground is showing up-- you're not filling in the entire image with the image?

08-29-2006, 04:15 AM
For now, the only solution I found is to give always enough size in the source image to avoid that ImageCopyResampled puts its black color around destination image. But sometimes this is not possible.

Yes, I am cropping all images to the same width and height dimensions, so the problem is whether some of these sources have a width or height smaller than final cropped image. In this case "ImageCopyResampled" puts black pixels around to fill the dimension of cropped image, and I cannot find a way to replace these black pixels with other color.

Some idea?

Thank you indeed.


08-29-2006, 11:36 PM
After three days working with this issue, I think, there is no solution because "ImageCopyResampled" is like "ImageCreateFromJPEG", namely, it inicializes a new "ImageCreateTrueColor" internally for the resampling process, and we cannot manilpulate it.

The only solution should be a new ...

ImageCopyResampled(resource img_dst, resource img_org, int Xdst, int Ydst, int Xorg, int Yorg, int width_dst, int height_dst, int width_org, int height_org, int col) <-- Default Background color for internal call to ImageCreateTrueColor... from PHP developer team.

What do you think?


08-30-2006, 02:25 AM
I agree that would be a very nice addition to the function.

I guess your other option would be to draw fillrectangles in the color of your choice on the margins that are coming up black. If you know the size of the original image, and you know the size of the image you're resampling to, you should be able to come up with the size of the margin area.

A royal pain in the tookus if you ask me!