Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

An Introduction to the GD Library

pages (4): [1] 2 3 4


Are you getting bored with standard PHP functions?  Do you want to venture into additional libraries of PHP code? If so, GD is an additional module that allows the programmer to develop graphics using PHP. Cool huh? You can do things from drawing lines, to adjusting contrast, to anti-aliasing.

Before we start we need to make sure that your web server has GD enabled. To do this, make a new file called phpinfo.php with the following string as its only content:

<? phpinfo() ?>

This will output a big list of PHP settings, which is probably not very user friendly. You are looking for a specific section - it will look something like the following if GD is enabled on your server:

This is an example of the output of phpinfo() for a bundled GD distribution, which is the common one, as it comes with newer versions of PHP. If you don’t find this part in your phpinfo() output then you will need to find a server that does support the libraries.

So, what are we going to learn in this tutorial? Let’s have a look:

· Basic concepts
· Drawing lines
· Drawing polygons
· Drawing rectangles
· Drawing circles and ellipses
· Filling shapes
· Writing text on an image
· Outputting the image

That said, let’s get going! First of all, some basic concepts:

Basic Concepts
The first thing you should learn about GDlib is its capabilities. GD is a library of PHP functions that allows you to alter or create images online. You can adjust the output quality, load files and alter them, or create new images from scratch. PHP images can be used just like any other image, i.e. you could use the following HTML to display a dynamic PHP image on a webpage:

<img src=”myscript.php” style=”width: 400px; height: 200px” alt=”My dynamic PHP image” />

This is an example of valid XHTML syntax to display a dynamic image on a webpage. Note that a lot of things are depreciated in XHTML, so you can’t use attributes like height or width anymore, which is why I used a CSS-style definition instead. You can apply id’s or classes if that is your fancy.

So, let’s start with some basic concepts. When you create an image from scratch, you use the imagecreate() function to create a canvas. Its attributes are the x width and the y width. Here’s an example of the code for a 800x400px canvas:
 

<?php

$x = 800;
$y = 400;
$im = imagecreate($x, $y);

?>

You really need to get used to using image identifiers in GD, $im is the default variable and is used a lot, although you don’t have to use if you don't like it for some reason.

Next up, let’s learn how to allocate some colours, using imagecolorallocate(). With this, you can define colours for anything, and it is essential if you want colours in your image. This function accepts arguments 4 arguments: [image identifier], [R], [G], [B]. You put your image identifier variable in place, and numbers between 1 and 255 for each colour (red, green and blue). You can define as many different variables as you like in different variables. Let’s have a look at an example I prepared earlier:

<?php

$red = imagecolorallocate($im, 255, 0, 0);
$blue = imagecolorallocate($im, 0, 0, 255);
$green = imagecolorallocate($im, 0, 255, 0);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

?>

Coordinates are pretty easy to understand, using the concept: x, y, the top left corner is 0, 0. So if we have a 150px square image, the top right corner will be 150, 0, the bottom left will be 0, 150, and the bottom right will be 150, 150.

Outline functions (non fill functions) will by default draw a 1px outline, and you can figure out for yourself how to make thicker lines... ;)

Finally, let’s find out how to load an image as opposed to creating a canvas. There are special PHP functions to load images, and you can load an image in the following main formats: .jpeg, .gif, .wbmp, and .png. Instead of defining the image identifier with imagecreate() we simply define it using one of the following functions:

· JPG/JPEG: imagecreatefromjpeg([filename])
· GIF: imagecreatefromgif([filename])
· BMP: imagecreatefromwbmp([filename])
· PNG: imagecreatefrompng([filename])

And that’s it! You can run a few protection procedures if you like, to check whether the image was opened or not - all of these functions return false if there was an error.

Right. That’s the basic principles of GD out of the way.  You will need to know how to output the image once you’re finished, but that topic will be covered last.

- Tutorial written by Scrowler

Pages (4): [1] 2 3 4 Next>
Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments


There are no comments for this tutorial yet.
You can place a comment by clicking here.
Amazing Font Pack!

Featured Tutorialsmore

Sunbursts
Sunbursts
- Adobe Illustrator -
Xpresso Tutorial -...
Xpresso Tutorial -...
- Maxon Cinema 4D -
Razor Stryke Logo
Razor Stryke Logo
- Adobe Photoshop -
Pixel Stretch Swirl
Pixel Stretch Swirl
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Download Source File
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 887 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1052 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1054 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1935 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1832 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14441 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2751 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2772 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3439 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2356 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2499 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2798 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673