On Sun, 2010-05-09 at 14:21 +0200, Giorgio wrote:

> Thankyou Ashley,
> 
> as I only need 5 colors for each image, my idea was to create a single pixel
> image for each color and save it on the hd (so that i don't have to create
> it every time).
> 
> I've finally used this code:
> 
> imagecopymerge($base, $img1, 0, 0, 1, 1, 100, 500, 75);
> imagecopymerge($base, $img2, 100, 0, 1, 1, 100, 500, 75);
> imagecopymerge($base, $img3, 200, 0, 1, 1, 100, 500, 75);
> imagecopymerge($base, $img4, 300, 0, 1, 1, 100, 500, 75);
> imagecopymerge($base, $img5, 400, 0, 1, 1, 100, 500, 75);
> 
> ($imgX are 1*1 images. I have a function that checks if they exists)
> 
> You can see the output here: http://aagmns47.facebook.joyent.us/sdna/.
> 
> I think that copying a 1*1 image (and as said i'll cache all 1*1 colors on
> the hd) on a background is less resource-expensive than creating a coloured
> rectangle every time, isn't it?
> 
> Giorgio
> 
> 
> 2010/5/9 Ashley Sheridan <a...@ashleysheridan.co.uk>
> 
> >  On Sat, 2010-05-08 at 18:48 +0200, Giorgio wrote:
> >
> > Hi,
> >
> > i've just started using GD libraries. My purpose is to create something like
> > a flag.
> >
> > So that i have a 600*600 px background image, and a 1*1 px image for each
> > colour I need.
> >
> > Ok now let's say i want to colour a third of the background image. I can use
> > this code:
> >
> > imagecopymerge($base, $img1, 0, 0, 0, 0, 200, 600, 85);
> >
> > It will simply make red first 200 px (on x axys) of the bg image. And this
> > work.
> >
> > Now the problem is: how can i add another colour on pixels from 201 (201
> > right? not 200?) to 400?
> >
> > Thankyou
> >
> >
> >
> > Erm, why don't you use something like imagerectangle() ? It's far easier
> > and less resource intensive than having loads of single-pixel images for
> > each colour you want to use.
> >
> >   Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> >
> 
> 


No, you use imagerectangle() directly on the final image, no need to use
imagecopymerge() at all!

$colour1 = imagecolorallocate($base, 255, 105, 180);
$colour2 = imagecolorallocate($base, 255, 255, 255);
$colour3 = imagecolorallocate($base, 132, 135, 28);
$colour4 = imagecolorallocate($base, 88, 105, 180);
$colour5 = imagecolorallocate($base, 0, 0, 0);

imagerectangle($base, 0, 0, 100, 500, $colour1);
imagerectangle($base, 100, 0, 200, 500, $colour2);
imagerectangle($base, 200, 0, 300, 500, $colour3);
imagerectangle($base, 300, 0, 400, 500, $colour4);
imagerectangle($base, 400, 0, 500, 500, $colour5);

It looks like more code, but you'll really appreciate it if you get a
lot of visitors who are triggering these images to be created
dynamically. Copying a single-pixel image and resizing it into a much
larger area is always going to be more costly than using the native
imagerectangle() function.

Also, the above way allows you to create the colours dynamically for the
image from within your script, and not have to create a new 1x1 pixel
image each time!

Thanks,
Ash
http://www.ashleysheridan.co.uk


Reply via email to