Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-28-2006, 08:30 AM   PM User | #1
mapg
New Coder

 
Join Date: Jul 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
mapg is an unknown quantity at this point
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 12:33 AM..
mapg is offline   Reply With Quote
Old 08-28-2006, 03:27 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Did you try using Imagecreate instead of Imagecreatetruecolor?
Fumigator is offline   Reply With Quote
Old 08-28-2006, 10:41 PM   PM User | #3
mapg
New Coder

 
Join Date: Jul 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
mapg is an unknown quantity at this point
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
mapg is offline   Reply With Quote
Old 08-29-2006, 12:19 AM   PM User | #4
mapg
New Coder

 
Join Date: Jul 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
mapg is an unknown quantity at this point
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
mapg is offline   Reply With Quote
Old 08-29-2006, 12:22 AM   PM User | #5
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
You could use imagefilledrectangle to create a white rectange as the background.
Mwnciau is offline   Reply With Quote
Old 08-29-2006, 12:53 AM   PM User | #6
mapg
New Coder

 
Join Date: Jul 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
mapg is an unknown quantity at this point
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
mapg is offline   Reply With Quote
Old 08-29-2006, 01:17 AM   PM User | #7
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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?
Fumigator is offline   Reply With Quote
Old 08-29-2006, 04:15 AM   PM User | #8
mapg
New Coder

 
Join Date: Jul 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
mapg is an unknown quantity at this point
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
mapg is offline   Reply With Quote
Old 08-29-2006, 11:36 PM   PM User | #9
mapg
New Coder

 
Join Date: Jul 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
mapg is an unknown quantity at this point
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 ...

Quote:
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
mapg is offline   Reply With Quote
Old 08-30-2006, 02:25 AM   PM User | #10
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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!
Fumigator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:55 PM.


Advertisement
Log in to turn off these ads.