IT Community - Software Programming, Web Development and Technical Support

PHP:Tutorial - Getting to know GD

This is a discussion on PHP:Tutorial - Getting to know GD within the PHP Programming forums, part of the Web Development category; Introduction: Have you ever been on aim and received a message from one of your friends with a big long ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-23-2007, 09:56 AM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
Default PHP:Tutorial - Getting to know GD

Introduction:
Have you ever been on aim and received a message from one of your friends with a big long URL followed by something like "John%20Is%20Stupid." You know what ever it is, wont be worth your time, but you click on it anyway to find someone goofy looking holding a sign and on it printed in big bold letters “John Is Stupid.” Ever wonder how they work? Chances are the script takes advantage of the GD functions. In this tutorial you will learn many GD functions that you can use to generate images. The goal for this tutorial is for you to generate a dynamic user bar like the one below.

http://www.extreme-hq.com/other/tuto...hp?text=JUNKIE

Here is a list of the functions used in this tutorial:
header()
imagecolorallocate()
imagettfbbox()
imagesx()
abs()
imagettftext()
imagepng()
imagedestroy()

Solution:
The first thing you want to do is create the header type using the header function. Next you use the GD tools to create an image from a png file. In my case, I have userbar.png in the same directory as this script is in. The function imagecolorallocate creates a color using RGB (red-green-blue) format. The next three lines just set some basic information which isn’t that hard to understand. The last three lines to must of the work creating the image. The most important function here to pay attention to is imagettftext. The imagettftext requires 8 arguments which are the resource image, font size, the angle, x location, y location, font color, the actual font, and the string to be printed.

PHP Code:
<?php
header('Content-type: image/png');
$im = imagecreatefrompng ("userbar.png");
$color = imagecolorallocate($im, 0, 0, 0);
$text = "test";
$font = 'font.ttf';
$size = 8;
imagettftext($im, $size, 0, 15, 12, $color, $font, $text);
imagepng($im);
imagedestroy($im);
?>
Now that we have GD generating an image, lets make it a little more complex. We’ve added the
PHP Code:
$text = $_GET['text'];
line to allow the user to enter a random string, and we have also added the
PHP Code:
$size = imagettfbbox($fontsize, 0, $font, $text);
$dx = (imagesx($im)) - (abs($size[2]-$size[0])) - 20;
Which calculate where to start printing the string on the image. The function imagettfbbox generates the length of the font string in pixels. If you were to use something like strlen($text) that would just return the number of characters in the string, which isn’t very usefull in this case. Next, we use the imagesx() function to determine the width of the image, we take that value and subtract the string length in pixels and subtract an extra 20 pixels to leave some room toward the end of the image.
PHP Code:
<?php
header('Content-type: image/png');
$text = $_GET['text'];
$im = imagecreatefrompng ("userbar.png");
$color = imagecolorallocate($im, 0, 0, 0);
$font = 'font.ttf';
$fontsize = 6;
$size = imagettfbbox($fontsize, 0, $font, $text); //calculate the pixel of the string
$dx = (imagesx($im)) - (abs($size[2]-$size[0])) - 20; //calculate the location to start the text
imagettftext($im, $fontsize, 0, $dx, 13, $color, $font, $text);
imagepng($im);
imagedestroy($im);
?>
At this point you could call yourself done. However you could add some extra features such as make the font color vary with the user input. Adding a simple if statement will accomplish this.

PHP Code:
<?php
header('Content-type: image/png');
$text = $_GET['text'];

$im = imagecreatefrompng ("userbar.png");

if($text == "ADMINISTRATOR"){ //if administrator
$color = imagecolorallocate($im, 255, 0, 0); //red
}
elseif($text == "MODERATOR"){ //if moderator
$color = imagecolorallocate($im, 0, 0, 255); //blue
}
elseif($text == "JUNKIE"){ //if junkie
$color = imagecolorallocate($im, 0, 0, 0); //black
}
else { //something else...
$color = imagecolorallocate($im, 0, 128, 0); //green
}

$font = 'font.ttf'; //font file
$fontsize = 6; //font size
$stringsize = imagettfbbox($fontsize, 0, $font, $text); //calculate the pixel of the string
$dx = (imagesx($im)) - (abs($stringsize[2]-$stringsize[0])) - 20; //calculate the location to start the text
imagettftext($im, $fontsize, 0, $dx, 13, $color, $font, $text);
imagepng($im);
imagedestroy($im);
?>
Finally you can save this script as generator.php and display it in forums by using the html tags.

Code:

<img src="http://www.yourdomain.com/generator.php?text=JUNKIE" />
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pro*C Tutorial Sabari C and C++ Programming 0 09-11-2007 04:53 AM
VC++ A Short Cut - tutorial Karpagarajan C and C++ Programming 5 08-02-2007 06:28 AM
Java:Tutorial - A better looking GUI pranky Java Programming 2 03-08-2007 12:34 PM
What are the best tutorial sources? SageMother HTML, CSS and Javascript Coding Techniques 3 03-05-2007 10:37 AM
Java:Tutorial - Tic-Tac-Toe pranky Java Programming 6 02-23-2007 09:48 AM


All times are GMT -7. The time now is 06:48 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0