Page 1 of 1
how to make transparent an area as a hole in image ???
Posted: Mon Apr 13, 2009 1:06 am
by lamp4
hello all PHPGDhero
now this is my problem when working with GD in PHP. and i hope you can help me.
first, i have an image with .PNG format, as bellow:
And i want to process image as: delete the rectangle and change it into background. then make this rectangle to transparent:
Result:white rectangle is transparent as a hole can show through

:
I tried many way but still don't success...
Can you help me about code to make this function ???
Thanks very much

Re: how to make transparent an area as a hole in image ???
Posted: Mon Apr 13, 2009 3:06 am
by greyhoundcode
Should be pretty straightforward, allocate your colour then set it to transparent.
Code: Select all
$colour = ImageColorAllocate($img, 255, 255, 255);
ImageColorTransparent($img, $colour);
What have you tried so far?
Re: how to make transparent an area as a hole in image ???
Posted: Mon Apr 13, 2009 4:39 am
by Apollo
I think he doesn't neccesarily have a white area there. TS, I assume you just wants to create a transparent box in the middle of *any* random image?
Code: Select all
<?php
$imgPath = 'before.png';
$img = imagecreatefrompng($imgPath); // load the image
list($width,$height) = getimagesize($imgPath); // get its size
$c = imagecolortransparent($img,imagecolorallocate($img,255,1,254)); // create transparent color, (255,1,254) is a color that won't likely occur in your image
$border = 10;
imagefilledrectangle($img, $border, $border, $width-$border, $height-$border, $c); // draw transparent box
imagepng($img,'after.png'); // save
print('<html><body background="randombackground.jpg"><img src="after.png"></body></html>'); // show result
?>
Re: how to make transparent an area as a hole in image ???
Posted: Mon Apr 13, 2009 5:53 am
by lamp4
thanks you for replying
My purpose is make the rectangle in the image become transparent and can show through.
illustrator for an image after process:
And i want to know how to do it ...???
Re: how to make transparent an area as a hole in image ???
Posted: Mon Apr 13, 2009 7:12 am
by Apollo
lamp4 wrote:And i want to know how to do it ...???
Just like I posted above.
Re: how to make transparent an area as a hole in image ???
Posted: Mon Apr 13, 2009 12:50 pm
by lamp4
That right. Thanks Apollo very much
But now i how 2 problem, and i want you help me more time:
1)
Your code is worked, but align of rectangle is center. i want to control it, can move it around place on image (put rectangle any where i like)
2)
i want to change the conner of rectangle ( the coner is circle ), as bellow:
Thanks very much

Re: how to make transparent an area as a hole in image ???
Posted: Tue Apr 14, 2009 4:00 am
by greyhoundcode
Apollo's already given you some good example code, surely you can look at it and experiment a little in order to figure out positioning?
As far as rounded corners go, there are various ways of achieving this - off the cuff, some judicious use of ImageFilledEllipse() might help.

- Rounded corners with the GD lib
- rounded-corners.png (35.63 KiB) Viewed 11202 times
Re: how to make transparent an area as a hole in image ???
Posted: Tue Apr 14, 2009 4:30 am
by Apollo
lamp4 wrote:1)
Your code is worked, but align of rectangle is center. i want to control it, can move it around place on image (put rectangle any where i like)
Here is where I place the rectangle:
Code: Select all
$border = 10;
imagefilledrectangle($img, $border, $border, $width-$border, $height-$border, $c);
You can easily change this to suit your needs. If you are completely clueless how this works, see the
imagefilledrectangle function and experiment yourself.