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.

Mapg

    Try:

    $im = ImageCreateTrueColor($nx, $ny);
    ImageColorAllocate($im, 191, 225, 216)

      Unfortunately, doesn't work.

      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.

      Thanks.

      mapg

        Okay, the try this:

        $bg = imagecolorallocate($im, 191, 225, 216);
        imagefilledrectangle($im, 0, 0, $nx, $ny, $bg);

          Hi Installer,

          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.

          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.

          Mapg

            I forgot to do mention of this ...

            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.

            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.

            Thanks.

            Mapg

              After three days working in 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?

              Mapg

                Write a Reply...