An Intro to Using the GD Image Library with PHP - Let's Finish It Up (Page 6 of 6 ) Ok, so we have it all together and have an image created. Now if you just try and stick that code in your page, it will not display correctly. The reason for this is that the browser doesn't know how to interpret the data that is coming at it. Some people solve this by writing the image to a temporary file; I don't like that method. If you write to a temporary file then you have to go back and clean those up. What a hassle. What I do is put all the code into its own PHP file and add a header to it. Then just act as if the PHP file was an image file and use standard HTML to include it. So, here is our finished clock all ready to go into its own PHP file : <?php Header("Content-type: image/png"); $time = strftime("%I:%M:%S", time()); $timearray = explode(':',$time); $hour = (((int)$timearray[0]) * 60) + (int)$timearray[1]; $minute = (int)$timearray[1]; $second = (int)$timearray[2]; if($hour != 0) { $hourdegree = ((360 / (720 / $hour)) - 90) % 360; if($hourdegree < 0) { $hourdegree = 360 + $hourdegree; } } else { $hourdegree = 270; } if($minute != 0) { $minutedegree = ((360 / (60 / $minute)) - 90) % 360; if($minutedegree < 0) { $minutedegree = 360 + $minutedegree; } } else { $minutedegree = 270; } if($second != 0) { $seconddegree = ((360 / (60 / $second)) - 90) % 360; if($seconddegree < 0) { $seconddegree = 360 + $seconddegree; } } else { $seconddegree = 270; }
$image = imagecreate(100,100); $maroon = ImageColorAllocate($image,123,9,60); $white = ImageColorAllocate($image,255,255,255); $black = ImageColorAllocate($image,0,0,0);
ImageFilledRectangle($image,0,0,99,99,$white); ImageFilledEllipse($image,49,49,100,100,$black); ImageFilledEllipse($image,49,49,95,95,$maroon); ImageFilledEllipse($image,49,49,75,75,$white); ImageFilledEllipse($image,49,49,5,5,$maroon); ImageFilledArc($image,49,49,50,50,$hourdegree-4,$hourdegree+4, $maroon,IMG_ARC_PIE); ImageFilledArc($image,49,49,65,65,$minutedegree-3,$minutedegree+3, $maroon,IMG_ARC_PIE); ImageFilledArc($image,49,49,70,70,$seconddegree-2,$seconddegree+2, $black,IMG_ARC_PIE);
ImageColorTransparent($image,$white);
ImageTTFText ($image, 8, 0, 44, 11, $white, "/home/mjw21/www/images/arial.ttf","12"); ImageTTFText ($image, 8, 0, 89, 53, $white, "/home/mjw21/www/images/arial.ttf","3"); ImageTTFText ($image, 8, 0, 47, 96, $white, "/home/mjw21/www/images/arial.ttf","6"); ImageTTFText ($image, 8, 0, 5, 53, $white, "/home/mjw21/www/images/arial.ttf","9");
imagePNG($image); imagedestroy($image); ?> |
Simple huh? There are lots of other functions available in the GD library, so get out there and experiment with them. If you look around my site you will see that the graphics for the code ratings are also created on the fly. The possibilities are endless! | DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|