Page 1 of 1

GD draw nice shapes!

Posted: Wed Mar 18, 2009 4:41 pm
by artheus
Hi!

I've been looking around the web for some tutorials on how to make nice shapes in PHP. But the shapes I get from the imagefilledpolygon(), etc. Is not really what I would call "nice".. The edges are ugly, and not smooth as I would like them.

The shape I would like to create should look something like this :

Image

The reason I want to make theese with php is to get the corners in random angles.. And I want to have text in there.. But thats not my problem..

If you look at this picture created with php :

Image

You should see what I mean with ugly edges. Not really smooth..

So could someone help me with how I should make these shapes the best way.. And maby how I could make the edges smooth?

Thanks!
/Artheus

Re: GD draw nice shapes!

Posted: Wed Mar 18, 2009 4:49 pm
by php_east
GD is primitive and raw. What i would suggest is you use a graphic template of some sort and use GD copy/cut image capabilities to sort of cut and paste peices together to make/produce really nice backgrounds from the originals/templates. Not sure is anyone had done this, but it would definitely be the way to go.

Re: GD draw nice shapes!

Posted: Thu Mar 19, 2009 6:50 am
by artheus
I found out how to do this!

I use a 12 point polygon, and ellipses for rounded corners.
And to "antialias" this I make the picture 10 times bigger than what it's supposed to be and shrink it using the imagecopyresampled().

code:

Code: Select all

 
<?php
// set up array of points for polygon
$values = array(
            220,  720,  // Point 1 (x, y)
            220,  620, // Point 2 (x, y)
            120,  620,  // Point 3 (x, y)
            190, 170,  // Point 4 (x, y)
            290,  170,  // Point 5 (x, y)
            290,  70,  // Point 6 (x, y)
        4760,  130,  // Point 7 (x, y)
            4760,  230, // Point 8 (x, y)
            4860,  230,  // Point 9 (x, y)
            4880, 650,  // Point 10 (x, y)
            4780,  650,  // Point 11 (x, y)
            4780,  750   // Point 12 (x, y)
            );
 
// create image
$image = imagecreatetruecolor(5000, 800);
$im = imagecreatetruecolor(500, 80);
 
// allocate colors
$bg   = imagecolorallocate($image, 255, 255, 255);
$blue = imagecolorallocate($image, 232, 142, 217);
 
// fill the background
imagefilledrectangle($image, 0, 0, 5000, 800, $bg);
 
// draw a polygon
imagefilledpolygon($image, $values, 12, $blue);
imagefilledellipse($image, 220, 620, 200, 200, $blue);
imagefilledellipse($image, 290,  170, 200, 200, $blue);
imagefilledellipse($image, 4760, 230, 200, 200, $blue);
imagefilledellipse($image, 4780, 650, 200, 200, $blue);
 
imagecopyresampled($im, $image, 0,0,0,0,500,80,5000,800);
// flush image
header('Content-type: image/png');
imagepng($im);
 
imagedestroy($im);
?> 
 
Hope someone can use this :D

Cheers!

Re: GD draw nice shapes!

Posted: Thu Mar 19, 2009 9:06 am
by VladSun