Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Tutorials > PHP Tutorials

PHP Tutorials PHP Tutorials

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-03-2006, 03:37 AM
John's Avatar   
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,304
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Default PHP:Tutorial - Getting to know GD

Title: Create dynamic userbars with 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.

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($im000);
$text "test";
$font 'font.ttf';
$size 8;
imagettftext($im$size01512$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($fontsize0$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($im000);
$font 'font.ttf';
$fontsize 6;
$size imagettfbbox($fontsize0$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$fontsize0$dx13$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($im25500); //red
    

elseif(
$text == "MODERATOR"){ //if moderator
    
$color imagecolorallocate($im00255); //blue
    

elseif(
$text == "JUNKIE"){ //if junkie
    
$color imagecolorallocate($im000); //black
    

else { 
//something else...
    
$color imagecolorallocate($im01280); //green
    
}
    
$font 'font.ttf'//font file
$fontsize 6//font size
$stringsize imagettfbbox($fontsize0$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$fontsize0$dx13$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" />
Attached Files To view attachments in this forum your post count must be 1 or greater. You currently have 0 posts.

Last edited by John; 11-02-2008 at 12:13 AM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-03-2006, 04:28 AM
John's Avatar   
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,304
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Default

Here are two more scripts that take advantage of GD, I've posted these before but I figured i'll post them here to keep track of them.

Captcha Generator
PHP Code:
<?php
function RandomStringGenerator($length){
global 
$string;
    
$pattern "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
if(empty(
$length)){
    
$length "10";
    }
for(
$i=0$i<$length$i++){
    
$string .= $pattern{rand(0,61)};
    }

return 
$string;
}

function 
CaptchaGenerator(){
global 
$string;
RandomStringGenerator();
header("Content-type: image/png");
$im = @imagecreate(10050)
   or die(
"Cannot Initialize new GD image stream");
$background_color imagecolorallocate($im000);
$text_color imagecolorallocate($im25500);
imagestring($im1055,  $string$text_color);
imagepng($im);
imagedestroy($im);
}

CaptchaGenerator(); 
?>
Signature Rotator/Banner Rotator:

PHP Code:
<?php

$folder 
'./';

$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';

$img null;

if (
substr($folder,-1) != '/') {
   
$folder $folder.'/';
}

if (isset(
$_GET['img'])) {
   
$imageInfo pathinfo($_GET['img']);
   if (
       isset( 
$extListstrtolower$imageInfo['extension'] ) ] ) &&
       
file_exists$folder.$imageInfo['basename'] )
   ) {
       
$img $folder.$imageInfo['basename'];
   }
} else {
   
$fileList = array();
   
$handle opendir($folder);
   while ( 
false !== ( $file readdir($handle) ) ) {
       
$file_info pathinfo($file);
       if (
           isset( 
$extListstrtolower$file_info['extension'] ) ] )
       ) {
           
$fileList[] = $file;
       }
   }
   
closedir($handle);

   if (
count($fileList) > 0) {
       
$imageNumber time() % count($fileList);
       
$img $folder.$fileList[$imageNumber];
   }
}

if (
$img!=null) {
   
$imageInfo pathinfo($img);
   
$contentType 'Content-type: '.$extList$imageInfo['extension'] ];
   
header ($contentType);
   
readfile($img);
} else {
   if ( 
function_exists('imagecreate') ) {
       
header ("Content-type: image/png");
       
$im = @imagecreate (100100)
           or die (
"Cannot initialize new GD image stream");
       
$background_color imagecolorallocate ($im255255255);
       
$text_color imagecolorallocate ($im0,0,0);
       
imagestring ($im255,  "IMAGE ERROR"$text_color);
       
imagepng ($im);
       
imagedestroy($im);
   }
}

?>
To add a border to text:
PHP Code:
/**
 * Writes the given text with a border into the image using TrueType fonts.
 * @author John Ciacia < John@extreme-hq.com >
 * @param image An image resource.
 * @param size The font size.
 * @param angle The angle in degrees to rotate the text.
 * @param x Upper left corner of the text.
 * @param y Lower left corner of the text.
 * @param textcolor This is the color of the main text.
 * @param strokecolor This is the color of the text border.
 * @param fontfile The path to the TrueType font you wish to use.
 * @param text The text string in UTF-8 encoding.
 * @param px Number of pixels the text border will be.
 * @return Returns an array with 8 elements representing four points making the bounding 
 *         box of the text. The order of the points is lower left, lower right, upper right, 
 *         upper left. The points are relative to the text regardless of the angle, so 
 *         "upper left" means in the top left-hand corner when you see the text horizontally.
 * @see http://us.php.net/manual/en/function.imagettftext.php
 * @see http://forum.codecall.net
 * @see http://www.extreme-hq.com
 */
function imagettfstroketext(&$image$size$angle$x$y, &$textcolor, &$strokecolor$fontfile$text$px) {

    for(
$c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
        for(
$c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
            
$bg imagettftext($image$size$angle$c1$c2$strokecolor$fontfile$text);

   return 
imagettftext($image$size$angle$x$y$textcolor$fontfile$text);

Usage:
PHP Code:
$img imagecreatefrompng("/home/john/Desktop/test.png");
$font_color imagecolorallocate($img000);
$stroke_color imagecolorallocate($img25500);
$font "/home/john/Desktop/ABSTRACT.TTF";
$txt "This is a test..";
$px 2;
imagettfstroketext($img1001050$font_color$stroke_color$font$txt$px); 
Enjoy!

Last edited by John; 05-04-2008 at 01:37 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-03-2006, 09:31 AM
Jordan's Avatar   
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 18,274
Blog Entries: 90
Rep Power: 20
Jordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of light
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Default

Nice Tutorial, very well done.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-03-2006, 10:04 AM
TcM's Avatar   
TcM TcM is offline
Code Warrior
 
Join Date: Aug 2006
Posts: 9,677
Blog Entries: 6
Rep Power: 81
TcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to all
Default

:0 Im impressed I wish I knew PHP
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-03-2006, 12:54 PM
John's Avatar   
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,304
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Default

Thanks for the comments, ive added 2 more scripts to this section that you've already seen but also take advantage of GD so i figured they were worth showing
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 12-04-2006, 12:50 PM
TcM's Avatar   
TcM TcM is offline
Code Warrior
 
Join Date: Aug 2006
Posts: 9,677
Blog Entries: 6
Rep Power: 81
TcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to all
Default

I have a problem with the 1st tutorial.. I made this:-
Code:
<html>
<img src="http://localhost/generator.php?text=ADMINISTRATOR" />
</html>
Right? its localhost because I ran a Server on my PC to try this.. and it supports PHP but the image link is showing as broken.. any idea why?
I even tried to upload it to a hosting site that supports php.. still broken link...

Last edited by TcM; 12-04-2006 at 11:54 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 12-04-2006, 01:28 PM
Jordan's Avatar   
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 18,274
Blog Entries: 90
Rep Power: 20
Jordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of lightJordan is a glorious beacon of light
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Default

I'm betting you do not have GD working or compiled into your system. Is it a Windows System?

Create a PHP file named phpinfo.php and put this in it:

PHP Code:
<?php
phpinfo
();
?>
Then goto http://localhost/phpinfo.php - this lists everything that your PHP can do. Search for GD.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 12-04-2006, 02:02 PM
TcM's Avatar   
TcM TcM is offline
Code Warrior
 
Join Date: Aug 2006
Posts: 9,677
Blog Entries: 6
Rep Power: 81
TcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to all
Default

Well when I tried localhost/phpinfo.php nothing cameup.. perhaps its not configured correctly but then I tried it on a hosting site yes it is Enabled!!:-
Quote:
gd
GD Support enabled
GD Version bundled (2.0.28 compatible)
FreeType Support enabled
FreeType Linkage with freetype
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled
I opened the PHP file generator.php ( on the hosting site )and it said:-
Quote:
The image “http://tcm9669.9999mb.com/generator.php” cannot be displayed, because it contains errors.
huh??

Last edited by TcM; 12-04-2006 at 02:04 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 12-04-2006, 04:55 PM
John's Avatar   
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,304
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Default

If your running this script on a linux server, i believe the font i included is called font.TTF not font.ttf

Last edited by John; 12-05-2006 at 12:23 AM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 12-05-2006, 12:06 AM
TcM's Avatar   
TcM TcM is offline
Code Warrior
 
Join Date: Aug 2006
Posts: 9,677
Blog Entries: 6
Rep Power: 81
TcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to allTcM is a name known to all
Default

Well the hosting site would not let me upload .tff just .TFF .. Dam them! Well never mind I just wanted to try it!! but still nice tut...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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
PHP:Tutorial - Email Verification John PHP Tutorials 3 09-19-2007 12:19 PM
PHP:Tutorial The Date John PHP Tutorials 0 01-10-2007 06:10 PM


All times are GMT -5. The time now is 10:47 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads