PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Oct 19, 2018 9:36 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Mon Apr 13, 2009 1:06 am 
Offline
Forum Newbie

Joined: Mon Apr 13, 2009 12:36 am
Posts: 5
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.

Top
 Profile  
 
PostPosted: Mon Apr 13, 2009 3:06 am 
Offline
Forum Regular
User avatar

Joined: Mon Feb 11, 2008 5:22 am
Posts: 613
Should be pretty straightforward, allocate your colour then set it to transparent.

Syntax: [ Download ] [ Hide ]
$colour = ImageColorAllocate($img, 255, 255, 255);
ImageColorTransparent($img, $colour);

What have you tried so far?


Top
 Profile  
 
PostPosted: Mon Apr 13, 2009 4:39 am 
Offline
Forum Regular
User avatar

Joined: Wed Apr 30, 2008 2:34 am
Posts: 794
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?
Syntax: [ Download ] [ Hide ]
<?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
?>


Top
 Profile  
 
PostPosted: Mon Apr 13, 2009 5:53 am 
Offline
Forum Newbie

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


Top
 Profile  
 
PostPosted: Mon Apr 13, 2009 7:12 am 
Offline
Forum Regular
User avatar

Joined: Wed Apr 30, 2008 2:34 am
Posts: 794
lamp4 wrote:
And i want to know how to do it ...???

Just like I posted above.


Top
 Profile  
 
PostPosted: Mon Apr 13, 2009 12:50 pm 
Offline
Forum Newbie

Joined: Mon Apr 13, 2009 12:36 am
Posts: 5
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 :)


Top
 Profile  
 
PostPosted: Tue Apr 14, 2009 4:00 am 
Offline
Forum Regular
User avatar

Joined: Mon Feb 11, 2008 5:22 am
Posts: 613
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.

Attachment:
File comment: Rounded corners with the GD lib
rounded-corners.png
rounded-corners.png [ 35.63 KiB | Viewed 6979 times ]


Top
 Profile  
 
PostPosted: Tue Apr 14, 2009 4:30 am 
Offline
Forum Regular
User avatar

Joined: Wed Apr 30, 2008 2:34 am
Posts: 794
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:
Syntax: [ Download ] [ Hide ]
$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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group