Create image with PHP / GD library, And Adding Fonts (Tutorial Contest)

chiragchirag MemberNAT Warrior
GD Library is one of the cool features started from PHP 3.0 , It`s helps create image on the fly , It just needs PHP and GD library Installed on the server.
~ It`s Just Creation of images from PHP ~

Using this we are creating a simple PNG image without any source image . just pure PHP :) .
< ?php

header("Content-type: image/png");
$string = $_GET;
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
header(?Content-type: image/png?); - This defines Image Extention, as here it is PNG
$string = $_GET[?text?]; - This is to get the text from the Input page
$im = imagecreatefrompng(?images/button1.png?); - This is the location of the source image.
$orange = imagecolorallocate($im, 220, 210, 60); - This defines the colour.
$px = (imagesx($im) - 7.5 * strlen($string)) / 2; - This figures out the Size.
imagestring($im, 3, $px, 9, $string, $orange); - This Combines all the the above defined .
imagepng($im); - Now it Creates the Image
imagedestroy($im); - Once the image is created it is only shown for that session time after that it is deleted
This example would be called from a page with a tag like: < img src=?button.php?text=text?/>. The above (say) button.php script then takes this ?text? string and overlays it on top of a base image which in this case is ?images/button1.png? and outputs the resulting image. This is a very convenient way to avoid having to draw new button images every time you want to change the text of a button. With this method they are dynamically generated.
To Add Fonts,
Add some fonts in the same directory of the script , and then add $font= ?somefont.ttf? in the script file. this has to be called in the URL i.e button.php?text=text&font=arial.ttf and then add it with the imagecreate line.
Sign In or Register to comment.