How to Create an Image in PHP

An Introduction to the Image Functions in PHP


How to Create an Image in PHP

by Christopher Heng, thesitewizard.com

PHP makes it very easy to do many things needed on a website, among which is to create an image. The ability to generate an image in PHP can be useful if you want to do things like create CAPTCHA images, or even design a banner or logo on the fly the way some free blogging software do.

By the end of this tutorial, you will be able to create or modify an existing image using PHP, set the colours ("colors" in US English) of its background, the text and the lines you draw, write text to that image, draw a line and set its thickness. You will also be able to either send that image to a web browser or to save it as a file. Armed with this basic foundation, you will also be able to knowledgeably explore further and perform more complex operations on images using PHP if you wish.

Prerequisites

Note: If you are here looking for an article on how to create banners or logos for your website, and not strictly for an article on how to write PHP scripts to generate such images, you may find my article on How to Create a Logo for Your Site the Quick and Easy Way more relevant.

  1. Basic PHP Knowledge

    I will assume that you already have some basic knowledge of how to write PHP programs. If not, you may wish to check out my basic PHP tutorial, How to Program in PHP.

  2. Your PHP Must Have Been Compiled with the GD Library

    For any of the functions listed here to be available, your PHP interpreter must have been compiled with the GD library. If you are using the PHP provided by a commercial web host, this is probably already provided as a standard feature.

  3. FreeType Must Be Compiled for TrueType Font Support

    If you want TrueType font support, your PHP must have FreeType support compiled into it as well.

An Introduction to Creating an Image from Scratch Using PHP

The easiest way to understand how to create an image is by looking at some sample code.

<?php
$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, "thesitewizard.com", $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );

header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>

The above code creates a 200x80 PNG image with a blue background and yellow text. It can be called from within your web page simply by referencing the php file. For example, if the PHP file that contains the above code is called myimage.php, then the HTML code to invoke it can simply be:

<img src="myimpage.php" alt="Image created by a PHP script" width="200" height="80">

Explanation of the Code

Modifying an Existing Image

In most cases, creating an image from scratch is overkill. For most web purposes, you can usually design the basic background of your image using a normal image editor and only add any additional text or graphical elements that need to be dynamically drawn using PHP. This allows you to speed up your scripts and reduce the resource consumption on your web server. It also lets you create your picture using professional picture designing tools.

To use an existing GIF, JPEG or PNG image as a canvas on which you add additional elements, use one of the following functions instead of imagecreate().

For example, if you created a GIF file called "mytemplate.gif", the function can be called as follows:

$myimage = imagecreatefromgif ( "mytemplate.gif" );

Like the basic imagecreate() function, these functions return FALSE if they fail to load the image for any reason.

Using TrueType Fonts

If you want to use a True Type font, you will need to use imagettftext() instead. For details on how to use this function, please consult the function's manual page on php.net.

You should note a few things, though, before using this function:

Drawing to Your Image

Besides the line drawing function used above, PHP has other functions that you can use. A complete list of functions, along with their descriptions, can be found on http://www.php.net/gd. To whet your appetite, functions include those that allow you to draw ellipses, arcs and polygons, change the style of your lines (to say dashed lines), and so on.

However, unless you have special reasons why you might want to dynamically draw complex pictures onto an image, you should consider creating your base image using a normal picture editor, load that image using a function like imagecreatefrompng(), and then only modifying small details with your script. Generating everything from scratch is unnecessary for most purposes, and can drain your web server resources.

Conclusion

Congratulations. You can now write scripts that create and output images, write text to those images, set the brush thickness for lines and draw those lines. You also have enough basic knowledge to intelligently explore further the other facilities available in PHP to design and modify your images.

Copyright © 2008-2014 by Christopher Heng. All rights reserved.
Get more free tips and articles like this, on web design, promotion, revenue and scripting, from http://www.thesitewizard.com/.

thesitewizard™ News Feed (RSS Site Feed)  Subscribe to thesitewizard.com newsfeed

Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at http://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce this article in whole or part, in any form, without obtaining my written permission.

Related Pages

New Articles

Popular Articles

How to Link to This Page

It will appear on your page as:

How to Create an Image in PHP





Home
Donate
Contact Us
Link to Us
Topics
Site Map

Getting Started
Web Design
Search Engines
Revenue Making
Domains
Web Hosting
Blogging
JavaScripts
PHP
Perl / CGI
HTML
CSS
.htaccess / Apache
Newsletters
General
Seasonal
Reviews
FAQs
Wizards

 

 
Free webmasters and programmers resources, scripts and tutorials
 
HowtoHaven.com: Free How-To Guides
 
Site Design Tips at thesitewizard.com
Find this site useful?
Please link to us.