NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Webmaster Tutorials
Reload this Page Tutorial: A Basic intro to using PHP GD functions to generate and manipulate images.

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 11-27-2003, 05:31 PM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Tutorial: A Basic intro to using PHP GD functions to generate and manipulate images.


Installing the GD library on Windows:

Browse to your PHP directory, most likely chp and search for the the file php_gd2.dll. It's probably in your chpextensions directory. Copy it to to your PHP directory, probably chp. Now open up your php.ini file which is probably located at c:windowsphp.ini and search for ;extension=php_gd2.dll and uncomment it by removing the semi-colon.

Creating an image with PHP
PHP Code:
<html>
<body>
<?php

    
//Creating the Image
    
$im imagecreate(320200);

    
//Defining colors
    
$black imagecolorallocate($im000);
    
$white imagecolorallocate($im255255255);
    
$wdtcolor imagecolorallocate($im244080);

    
//Drawing white background and black outline
    
imagefilledrectangle($im00319199$white);
    
imagerectangle($im00319199$black);

    
//Drawing text on the image
    
imagestring($im5110100"WebDev"$wdtcolor);

    
//Creating the image output in png format
    
imagepng($im,'image.png');

    
//freeing up any memory associated with the image
    
imagedestroy($im);
?>

<img src="image.png" width="320" height="200">
</body>
</html>
imagecreate()
We use the imagecreate function to create a palette based image. The imagecreate function takes in two parameters, width and height in terms of pixels. We assign $im as our image identifier. The image identifier is used to distinguish one image from another and is required for all the image editing functions.

imagecolorallocate()
We use the imagecolorallocate function to define the colors that we're going to use in the image. Imagecolorallocate takes in four paramaters, the image identifier, and the Red, Green, and Blue (RGB) components of a color. If you have two images you must assign the colors separately for each image. So if you were going to use the color blue in an image you would have to declare it twice, once with each image identifier. Here's a few more examples for different colors:
PHP Code:
$red imagecolorallocate($im25500);
$green imagecolorallocate($im02550);
$blue imagecolorallocate($im00255); 
imagefilledrectangle()
We use the imagefilledrectangle function to fill in our image with a white back ground. The imagefilledrectangle function takes in six parameters. The first paramater is the image identifier, the next four are the coordinates for the upper left and lower right corner of the rectangle. The last parameter is the color identifier which was created using the imagecollorallocate function.

imagerectangle()
The image rectangle function is identical to the imagefilledrectangle function except for it only draws an outline.

imagestring()
We use the image string function to print out a string on our image. The imagestring function takes in six paramaters. The first parameter as usual is the image identifier. The next parameter is the font size which by default only has five different sizes. Checkout the Imagettftext and imageloadfont functions for more info on using different fonts. The next two paramaters are the horizontal and vertical coordinates of the upper left corner of the text and the last parameter is the color identifier.

imagedestory()
The image destory function simply frees up any memory associated with the image.

The PHP GD functions can be useful for displaying poll results, creating charts, graphs and more.

A simple example of a vertical bar graph
PHP Code:
<html>
<body>
<?php

    
//Creating the Image
    
$im imagecreate(320200);

    
//Defining colors
    
$black imagecolorallocate($im000);
    
$white imagecolorallocate($im255255255);
    
$blue imagecolorallocate($im244080);

    
//Drawing white background and black outline
    
imagefilledrectangle($im00319199$white);
    
imagerectangle($im00319199$black);


    
//Drawing Vertical Bars
    
imagefilledrectangle($im22040160$blue);
    
imagefilledrectangle($im426080160$blue);
    
imagefilledrectangle($im82100120160$blue);
    
imagefilledrectangle($im12280160160$blue);
    
imagefilledrectangle($im16210200160$blue);
    
imagefilledrectangle($im20240240160$blue);
    
imagefilledrectangle($im24275280160$blue);
????: NamePros.com http://www.namepros.com/webmaster-tutorials/15690-tutorial-basic-intro-using-php-gd.html
    
imagefilledrectangle($im28250320160$blue);

    
//Printing out vertical Strings beneath each bar
    
imagestringup($im515195"Op1"$black);
    
imagestringup($im555195"Op2"$black);
    
imagestringup($im595195"Op3"$black);
    
imagestringup($im5135195"Op4"$black);
    
imagestringup($im5175195"Op5"$black);
    
imagestringup($im5215195"Op6"$black);
    
imagestringup($im5255195"Op7"$black);
    
imagestringup($im5295195"Op8"$black);


    
//Creating the image output in png format
    
imagepng($im,'image.png');

    
//freeing up any memory associated with the image
    
imagedestroy($im);
?>

<img src="image.png" width="320" height="200">
</body>
</html>
You could build on this and dynamically resize the bars according to results you pull from a database or text file, or other calculations you may use in your scripts.

imagestringup()
In the above example we used another function imagestringup. The imagestringup function works just like the imagestring function except for it prints vertically from bottom to top and the coordinates start at the lower left corner of the text.
????: NamePros.com http://www.namepros.com/showthread.php?t=15690

This is just the very basics of using the PHP GD functions to hopefully help get you an idea of how they work and get you started using them. But there's so much more you can do with PHP and GD. Take a look here http://php.net/image for a complete list of functions and a whole lot more info.
deadserious is offline  
Old 11-28-2003, 10:28 AM   #2 (permalink)
Senior Member
Join Date: May 2003
Posts: 2,187
adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough
 


Breast Cancer
u r a genius!!!!!!!!!!!!!!!


thanks!!

been searching for time something like this
adam_uk is offline  
Old 12-09-2003, 05:42 PM THREAD STARTER               #3 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Hey, cool! Nice to see someone found it useful. The gd library can be quite useful and even fun once you mess around with it a bit.
deadserious is offline  
Old 12-10-2003, 12:42 AM   #4 (permalink)
Senior Member
Join Date: May 2003
Posts: 2,187
adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough
 


Breast Cancer
yeh seems very handy
adam_uk is offline  
Closed Thread


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


Liquid Web Smart Servers  
All times are GMT -7. The time now is 12:26 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger