GD draw nice shapes!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
artheus
Forum Newbie
Posts: 2
Joined: Wed Mar 18, 2009 4:31 pm

GD draw nice shapes!

Post 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
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: GD draw nice shapes!

Post 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.
artheus
Forum Newbie
Posts: 2
Joined: Wed Mar 18, 2009 4:31 pm

Re: GD draw nice shapes!

Post 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!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: GD draw nice shapes!

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
Post Reply