how to make transparent an area as a hole in image ???

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
lamp4
Forum Newbie
Posts: 5
Joined: Mon Apr 13, 2009 12:36 am

how to make transparent an area as a hole in image ???

Post by lamp4 »

hello all PHPGDhero :wink:
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:
Image


And i want to process image as: delete the rectangle and change it into background. then make this rectangle to transparent:
Image


Result:white rectangle is transparent as a hole can show through :o :
Image



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

Thanks very much :)
Last edited by lamp4 on Mon Apr 13, 2009 3:30 am, edited 1 time in total.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: how to make transparent an area as a hole in image ???

Post 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?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: how to make transparent an area as a hole in image ???

Post 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
?>
lamp4
Forum Newbie
Posts: 5
Joined: Mon Apr 13, 2009 12:36 am

Re: how to make transparent an area as a hole in image ???

Post by lamp4 »

thanks you for replying :o

My purpose is make the rectangle in the image become transparent and can show through.

illustrator for an image after process:
Image

And i want to know how to do it ...???
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: how to make transparent an area as a hole in image ???

Post by Apollo »

lamp4 wrote:And i want to know how to do it ...???
Just like I posted above.
lamp4
Forum Newbie
Posts: 5
Joined: Mon Apr 13, 2009 12:36 am

Re: how to make transparent an area as a hole in image ???

Post 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:
Image

Thanks very much :)
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: how to make transparent an area as a hole in image ???

Post 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 with the GD lib
rounded-corners.png (35.63 KiB) Viewed 11203 times
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: how to make transparent an area as a hole in image ???

Post 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.
Post Reply