WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Counting - Creating a more sophisticated GIF based counter using PHP and MySQL
Categories : Beginner Guides, MySQL, PHP, To PHP, To MySQL Picture not available
ScriptMakers.com
Date : 2000-02-27
Grade : 0 of 5 (graded 0 times)
Viewed : 9962
Search : More Articles by ScriptMakers.com
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


"Real" counters must be called by image tags and require the GD 1.3 library. As with our last counter, we must start off with much of the same script.


<?
$path
= '/home/www/counter/images/';

$ending = '.GIF';

$default_style = 'GOLD';

$style = ($style)?$style:$default_style;

$size = GetImageSize($path.'1'.$style.$ending);
?>


Because we'll be using the length of the variable $count more then once, we might as well set it as a variable. We also have to get the height and width of the image we'll be using.


<?
$path
= '/home/www/counter/images/';

$ending = '.GIF';

$default_style = 'GOLD';

$style = ($style)?$style:$default_style;

$size = GetImageSize($path. '1'.$style.$ending);

$length_count = strlen($count);

$width = $size[0];
$height = $size[1];
?>




Now that all the variables are set, it's time to begin creating the image. We are creating an image that has the width of one of our digits multiplied by $length_count. The height remains as $height (the length of $count will never change the height of the image).


<?
$path
= '/home/www/counter/images/';

$ending = '.GIF';

$default_style = 'GOLD';

$style = ($style)?$style:$default_style;

$size = GetImageSize($path. '1'.$style.$ending);

$length_count = strlen($count);

$width = $size[0];
$height = $size[1];

$im = imagecreate($width * $length_count, $height);


?>


Now the hard part begins. As with our last script, this one must loop through every digit in $count, outputting its corresponding every time. First, we need the loop.


<?
$path
= '/home/www/counter/images/';

$ending = '.GIF';

$default_style = 'GOLD';

$style = ($style)?$style:$default_style;

$size = GetImageSize($path. '1'.$style.$ending);

$length_count = strlen($count);

$width = $size[0];
$height = $size[1];

$im = imagecreate($width * $length_count, $height);

for (
$i = 0; $i < $length_count; $i++) {

}

?>


We must declare each digit that we're using. The easiest way to do this is to put them all into the array $im_input[]. So if the number we wanted to print out was "100", we would need one $im_input[1] and two $im_input[0]. But since we're using the same digit, it seems rather pointless to declare the same digit twice. To stop this from happening, we can simply put an if() clause in our script. It simply says , "If the variable $im_input[current_digit] doesn't exist, then create it using the $path, $style, and $ending values that we've already set."


<?
$path
= '/home/www/counter/images/';

$ending = '.GIF';

$default_style = 'GOLD';

$style = ($style)?$style:$default_style;

$size = GetImageSize($path. '1'.$style.$ending);

$length_count = strlen($count);

$width = $size[0];
$height = $size[1];

$im = imagecreate($width * $length_count, $height);

for (
$i = 0; $i < $length_count; $i++) {
if(!
$im_input[$count[$i]]){
$im_input[$count[$i]] = imagecreatefromgif($path.$count[$i].$style.$ending);
}

}

?>


Now we just have to add that digit image to the image that we're building. We do this using the undocumented ImageCopy() function.


<?
$path
= '/home/www/counter/images/';

$ending = '.GIF';

$default_style = 'GOLD';

$style = ($style)?$style:$default_style;

$size = GetImageSize($path. '1'.$style.$ending);

$length_count = strlen($count);

$width = $size[0];
$height = $size[1];

$im = imagecreate($width * $length_count, $height);

for (
$i = 0; $i < $length_count; $i++) {
if(!
$im_input[$count[$i]]){
$im_input[$count[$i]] = imagecreatefromgif($path.$count[$i].$style.$ending);
}

imagecopy($im, $im_input[$count[$i]], $i*$width, 0, 0, 0, $width, $height);
}

?>


Now all we have to do is to output the image. Because this isn't an HTML page, the proper headers must be sent. Also, to clear up memory, it's a good idea to delete the image after it's outputted.


<?
$path
= '/home/www/counter/images/';

$ending = '.GIF';

$default_style = 'GOLD';

$style = ($style)?$style:$default_style;

$size = GetImageSize($path. '1'.$style.$ending);

$length_count = strlen($count);

$width = $size[0];
$height = $size[1];

$im = imagecreate($width * $length_count, $height);

for (
$i = 0; $i < $length_count; $i++) {
if(!
$im_input[$count[$i]]){
$im_input[$count[$i]] = imagecreatefromgif($path.$count[$i].$style.$ending);
}

imagecopy($im, $im_input[$count[$i]], $i*$width, 0, 0, 0, $width, $height);
}

Header( "Content-type: image/gif");
ImageGif($im);
ImageDestroy($im);

?>


You're done.









Beginners guide to PHP and MySQL - Creating a simple guest book
Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL
Counting - Creating a simple counter
Categories : PHP, MySQL, Beginner Guides, To PHP, To MySQL
Counting - Creating a GIF based counter using PHP and MySQL
Categories : Beginner Guides, PHP, To PHP, To MySQL, MySQL
Jump Start to Easy URLs
Categories : PHP, Beginner Guides, MySQL, File System, To PHP
Start Using MySQL
Categories : MySQL, Databases, To MySQL, Beginner Guides
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Grabb'n and Pars'n
Categories : Beginner Guides, PHP, To PHP
How TO Install PHP, Apache and MySQL on Linux or Unix
Categories : PHP, MySQL, Apache, Installation, Beginner Guides
Beginners Guide to PHP - Introduction to cookies
Categories : Beginner Guides, Cookies, To PHP, PHP
How To add paging (Pagination) with PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP
Who's Linking?
Categories : PHP, Beginner Guides, To PHP
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
Saving Images in MySQL
Categories : MySQL, PHP, Graphics, Databases
Custom MySQL-functions
Categories : Databases, MySQL, PHP, PHP Functions
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite