How to construct a π-ece of Cake with PI-H-PI
For celebration of π-Day I wanted to construct a cake.
And the perfect language for this is, of course, πHπ (PHP ;).
As of PHP 4.3 your installation should ship with the GD-Library.
Cause this is, what our cake-πe’ll be made of :).
So let’s begin and create our πcture ^^.
I downloaded two pictures from pixaby.com and resized them
respectively for our goal of an image size of 550×550.
Before we cut our cake, we have to define some numbers:
$pi_work=pi()*pi(); $total=100; $arc=$pi_work*360/$total;
They define our data ($pi_work), the circle in total (100) and the PI-e
we’re cutting out of the cake.
First, you have to create your image and copy the plate-background into it:
$pi_ece_of_cake = imagecreatetruecolor(550, 550); $plate = imagecreatefromjpeg("http://www.timhagn.com/images/silver.jpg"); imagecopy($pi_ece_of_cake, $plate, 0, 0, 0, 0, 550, 550);
Next, we get our topping and define our PI-e:
$strawberry = imagecreatefrompng("http://www.timhagn.com/images/strawberry.png"); $style=IMG_ARC_PIE;
Now let’s define two stripe colors and the starting point of our PI-e,
(and remember, GD’s “imagefilledarc” starts at 3 o’clock, speaking
at an angle of 90 degrees, so we have to subtract our starting point
from that).
$choco = imagecolorallocate($pi_ece_of_cake, 210, 105, 30); $cream = imagecolorallocate($pi_ece_of_cake, 255, 255, 204); $cakecolors=array($choco,$cream); $start=-80;
Our starting point is at 10 degrees, so we have to accommodate for this
in our following “hard work” of implementing the PI-e:
for ($i = 600; $i > 500; $i--) { imagefilledarc($pi_ece_of_cake, 250+40, $i*5/10-40, 500, 250, $start, $start+$arc,$cakecolors[$i/10%2], $style); } imagefilledarc($pi_ece_of_cake, 250+40, 250-40, 500, 250, $start, $start+$arc, $choco, $style);
The Modulo Operator (%2) on our $cakecolors and incrementor creates the
stripes visible (yes, you could program a particular flag, this way ;).
I wanted the PI-e to be slightly off center, so come the “-40” (in pixels).
$start and $start+$arc are the definitions of the PI-e circumference,
referring to our $start variable.
But wait, we wanted to have a topping, didn’t we? Let’s place our strawberry
on top of our chocolate cake with the following line of code (always with
π in mind ;) :
imagecopymerge($pi_ece_of_cake, $strawberry, 250+40+($pi_work*pi()*2), 72+(pi()*2)-15,0,0,55,70,99);
Now let’s flush our images and destroy temporary data:
header('Content-type: image/png'); imagepng($pi_ece_of_cake); imagedestroy($pi_ece_of_cake); imagedestroy($plate);
And now we’re ready and can celebrate π-Day! Happy π-ing!
Here’s the complete code:
<?php $pi_work=pi()*pi(); $total=100; $arc=$pi_work*360/$total; $plate = imagecreatefromjpeg("http://www.timhagn.com/images/silver.jpg"); $pi_ece_of_cake = imagecreatetruecolor(550, 550); imagecopy($pi_ece_of_cake, $plate, 0, 0, 0, 0, 550, 550); $strawberry = imagecreatefrompng("http://www.timhagn.com/images/strawberry.png"); $style=IMG_ARC_PIE; $choco = imagecolorallocate($pi_ece_of_cake, 210, 105, 30); $cream = imagecolorallocate($pi_ece_of_cake, 255, 255, 204); $cakecolors=array($choco,$cream); $start=-80; for ($i = 600; $i > 500; $i--) { imagefilledarc($pi_ece_of_cake, 250+40, $i*5/10-40, 500, 250, $start, $start+$arc,$cakecolors[$i/10%2], $style); } imagefilledarc($pi_ece_of_cake, 250+40, 250-40, 500, 250, $start, $start+$arc, $choco, $style); imagecopymerge($pi_ece_of_cake, $strawberry, 250+40+($pi_work*pi()*2), 72+(pi()*2)-15,0,0,55,70,99); header('Content-type: image/png'); imagepng($pi_ece_of_cake); imagedestroy($pi_ece_of_cake); imagedestroy($plate); ?>