ImageCopyResampled - How to avoid black background?

Discussion in 'PHP' started by mapg, Aug 28, 2006.

  1. #1
    Hi All,

    I would like to know how to avoid the default black background color in "ImageCopyResampled" if origin and destination sizes 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);
    ImageCopyResampled($im, $im2, 0, 0, $ox, $oy, $nx, $ny, $nx, $ny);
    ImageFill($im, 0, 0, ImageColorAllocate($im, 191, 225, 216));
    ImageInterlace($im, 1);
    PHP:
    What do you think? I spent two days doing many combinations, and using other options like "ImageAlphaBlending", "ImageColorTransparent". I couldn't get anything. Maybe I didn't use these functions rightly. I guess.

    If I use "ImageFill" or "ImageFilledRectangle" 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" to black before ImageCopyResampled doesn't work because looks like "ImageCopyResampled" puts another "ImageCreateTrueColor" and its opaque black color at top of image.

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

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

    Try by yourself with ImageCopyResampled and retouch the sizes to force images to do not match each other, and you will see a black background around the copied image and at top of all. If you use "ImageFill" after that, you will get problems with darken and lighten images.

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

    Some idea?

    Thank you very much in advance for your help.

    Mapg
     
    mapg, Aug 28, 2006 IP
  2. mapg

    mapg Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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
     
    mapg, Aug 28, 2006 IP