Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2004
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy ImageCopyResampled - How to avoid default black background?

    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:
    PHP Code:
    $im ImageCreateTrueColor($nx$ny);
    ImageInterlace($im1);
    ImageCopyResampled($im$im200$ox$oy$nx$ny$nx$ny);
    ImageFill($im00ImageColorAllocate($im191225216)); 
    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
    Last edited by mapg; 08-29-2006 at 01:33 AM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Did you try using Imagecreate instead of Imagecreatetruecolor?

  • #3
    New Coder
    Join Date
    Jul 2004
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    Thanks.

    Mapg

  • #4
    New Coder
    Join Date
    Jul 2004
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    You could use imagefilledrectangle to create a white rectange as the background.

  • #6
    New Coder
    Join Date
    Jul 2004
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Mapg

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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?

  • #8
    New Coder
    Join Date
    Jul 2004
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Mapg

  • #9
    New Coder
    Join Date
    Jul 2004
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    Mapg

  • #10
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •