How to make an image with php
By liviu - Posted on May 22nd, 2009
Tagged:
This is a nice example of how to make an image with php using the GD extension.
It was taken from
| Programming PHP, 2nd Edition | ||
| By Rasmus Lerdorf, Peter MacIntyre, Kevin Tatroe |
"
With PHP, you can easily create and manipulate images using the GD extension. Example 1-5 provides a text-entry field that lets the user specify the text for a button. It takes an empty button image file, and on it centers the text passed as the GET parameter "message." The result is then sent back to the browser as a PNG image.
"
<?php
if (isset($_GET['message'])) {
// load font and image, calculate width of text
$font = 'times';
$size = 12;
$im = ImageCreateFromPNG('button.png');
$tsize = imagettfbbox($size,0,$font,$_GET['message']);
// center
$dx = abs($tsize[2]-$tsize[0]);
$dy = abs($tsize[5]-$tsize[3]);
$x = ( imagesx($im) - $dx ) / 2;
$y = ( imagesy($im) - $dy ) / 2 + $dy;
// draw text
$black = ImageColorAllocate($im,0,0,0);
ImageTTFText($im, $size, 0, $x, $y, $black, $font, $_GET['message']);
// return image
header('Content-type: image/png');
ImagePNG($im);
exit;
}
?>
<html>
<head><title>Button Form</title></head>
<body>
<form action="<?= $PHP_SELF ?>" method="GET">
Enter message to appear on button:
<input type="text" name="message" /><br />
<input type="submit" value="Create Button" />
</form>
</body>
</html>
Bookmark/Search this post with:
- liviu's blog
- Login or register to post comments
Delicious
Digg
StumbleUpon
Propeller
Reddit
Magnoliacom
Newsvine
Furl
Facebook
Google
Yahoo
Technorati
Icerocket