Tutorial Downloads .com

Home

BEGINNING PHP4 : GENERATING GRAPHICS PART 1 - LAYING A FOUNDATION

BEGINNING PHP4 : GENERATING GRAPHICS PART 1 - LAYING A FOUNDATION DESCRIPTION PHP contains a range of functions that allow you to open, manipulate and output graphics to the web browser. During this chapter we will explore how these functions work and how we can apply them to display our data.

This free tutorial is a sample from the book Beginning Php 4 (Programmer to Programmer)

By the end of the chapter we will not only have seen how to create images on the fly, but will have built a practical application using these methods and concepts.

As with all things in life, we have to learn to walk before we can run - before we can jump in and start producing the goods, we have a few basics to get through.

PHP uses the gd graphics library for all but its most basic image functions. Provided you have a recent version of the library, you can create and manipulate images in a number of different formats; the two most noteworthy are JPEG and PNG. These are both compressed file formats, which means that they use mathematical algorithms to reduce the amount of data required to completely describe the image. They therefore play a very important role in keeping your file sizes small and download times short!

It's important to be able to recognize where you should use each format - they use quite different compression techniques, and most images will be better suited to one or the other.

The JPEG format uses lossy compression. What this means is that some of the data in the original image is lost during compression. The format is designed to work best with images like photographs (that's where the "P" in "JPEG" comes from), where there's a lot of subtle shading and not too much fine detail. It's the format to use when a slight loss in quality won't be too apparent to the viewer.

The PNG format on the other hand is compressed in a lossless fashion. It works best with images that contain lines and large blocks of color, cartoons for example. When the image is uncompressed, it will contain all of its original information. This means that sharp edges and straight lines (which suffer under JPEG compression) will be reproduced faithfully.

Early versions of gd (and thus PHP) contained support for GIF files, which are similar in many respects to PNG. However, Unisys holds a patent on the LZW compression algorithm used to create fully compressed GIFs, and consequently GIF support has been completely replaced by that for PNG files since gd version 1.6. All is far from lost though, as JPEG and the excellent PNG image encoding formats should be sufficient for all your graphics needs.

Before we even look at the technicalities of creating the image, let's go through the steps involved in getting PHP to create an image and display it in the browser:

This is effectively just a section of memory in which we define an image before outputting it to the browser or to disk.

1. Create an image canvas for PHP to work on - this is simply a reserved portion of server memory, onto which the script will "draw" (that is, write data) before outputting it to the browser or disk as an image.

2. Draw the picture on the image canvas.

3. Send the image to the browser.

4. Clean up memory by throwing away the image canvas.

Creating an Image

As mentioned above, the first thing we need to do is create a blank image canvas for PHP to work on - a call to the ImageCreate() function does just this. The only two things we need to tell it are the width and height of the image we wish to create. ImageCreate() will then return an identifier that identifies that blank image in memory. All the subsequent image functions we use will have to refer to the image in memory by means of this identifier.

This identifier is similar to a file handle or database link ID, as we've used in recent chapters, but refers to the location of our image canvas in memory rather than an open file on the disk or an active database connection.

$image = ImageCreate(200,150);

We've now defined $image as the identifier referring to the new, blank canvas (200 pixels wide by 150 pixels high) that ImageCreate() has just created for us. Now that this 200 x 150 pixel image is in memory we can start drawing on it. First though, we need to know what color we'll be drawing in.

Setting up Colors

Before we can tell PHP that we want to use a certain color on our canvas, we have to create that color. We do this in a similar fashion to that of creating the image canvas:

$gray = ImageColorAllocate($image,204,204,204);
$blue = ImageColorAllocate($image,0,0,255);

We've used the function ImageColorAllocate() to define two colors for our image: a gray and a blue. Each color is assigned a unique identifier and tied to an existing image canvas. As you can see, this function requires 4 pieces of information:

  • The first is the identifier of the image canvas with which this color will be used. This is the same identifier that we saw returned to us earlier from the ImageCreate() function.
  • The second, third and fourth things we need to tell ImageColorAllocate() are the respective values of red, green and blue components for that color, which must lie between 0 and 255.

Computers make up color by mixing different quantities of Red, Green and Blue. This is what is known as RGB mode color. Each of red, green and blue can range from 0 to 255. Setting each of red, green and blue to 0 will give us black (total absence of color), a value of 255 for all three will give us white. If we want to stick to web-safe colors, then we have to limit our values of red, green and blue to multiples of 51. That gives us 6 possible values for each of red, green and blue, for a total of 216 colors.

$gray and $blue are now two new identifiers to the colors gray and blue. We created the former by specifying equal values of each color. Note that 204 was not a number we pulled from the hat, but a multiple of 51, and we've therefore defined a web-safe color. We defined $blue by telling ImageColorAllocate() that the red component was 0, the green component was 0 and that the blue component was 255.

The Image Coordinate System

While we're dealing with theory here, we may as well take a look at how the image coordinate system works. It may sound like a mouthful but it's simply a way for us to precisely describe points in the image.

If you're familiar with creating graphs, you'll probably be used to the x and y values radiating outwards from the bottom left hand corner. In PHP, all coordinates radiate outwards from the top left-hand corner of the image, as the image below shows:

With this in mind, let's take a look at how our blank $image is laid out:

The x-y coordinates for the top left corner of $image are 0,0. This will be true for every image that you create. The x-coordinates extend to the right for 200 pixels and y-coordinates extend down for 150 pixels, so the bottom right-hand corner of the image has x-y coordinates of 199,149.


Resource:
Posted By : PHPbrain
On date : 06.03.08

Most used PHP Tutorials

PHP Tutorials ( Ebooks , PDF's )
A Programmer’s Introduction to PHP 4.0 John.Wiley.and.Sons.PHP5.and.MySQL.Bible.eBook.pdf New Riders - PHP Functions Essential Reference.chm The Underground PHP and Oracle Manual
BEGINNING PHP4 : GENERATING GRAPHICS PART 1 - LAYING A FOUNDATION
BEGINNING PHP4 : GENERATING GRAPHICS PART 1 - LAYING A FOUNDATION DESCRIPTION PHP contains a range of functions that allow you to open, manipulate and output graphics to the web browser. During this chapter we will explore how these functions work and how we can apply them to display our data.
PHP Handout ( php pdf study material )
ATDB - 2004 PHP Handout - Volume 3 - PHP tutorial. Page - 1. Vol. 3 : PHP tutorial. PHP Handout. ATDB 2004. Page 2. ATDB - 2004 PHP Handout - Volume 3 - PHP …
PHP/MySQL Tutorial
Open source has brought a lot more than Linux to the computing world. It has also given us PHP and MySQL. According to Graeme, PHP and MySQL are the world’s best combination for creating data-driven sites. In the first installment of this three-lesson tutorial, our Kiwi guide covers everything you need to know to begin developing database hubs. He gives instructions for installation on both Unix and Windows, and then goes on to show some simple scripts that will insert information into a database and display that data on a Web page.
Beginning PHP and MySQL: From Novice to Professional, Third Edition
A comprehensive introduction to … the scripting language PHP and the MySQL database server. This book will not only expose … core aspects of both technologies, but will provide valuable insight into how they are used in unison to create dynamic data-driven Web applications.
Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites
If you know HTML, this guide will have you building interactive websites quickly. You'll learn how to create responsive, data-driven websites with PHP, MySQL, and JavaScript, regardless of whether you already know how to program. Discover how the powerful combination of PHP and MySQL provides an easy way to build modern websites complete with dynamic data and user interaction. You'll also learn how to add JavaScript to create rich Internet applications and websites.
Magento Beginner's Guide
Get your Magento store up and running using Magento Beginner's Guide by William Rice.
Database Handling with PHP/MySQL Tutorial ( php pdf study material )
This php pdf study material gives Database Handling with PHP/MySQL Tutorial.Unlike other scripting languages for Web page development (i.e. ASP), PHP is open-source, crossplatform, and offers excellent connectivity to most of today’s common databases including Oracle, Sybase, Microsoft SQL Server, MySQL, Postgresql, ODBC (and others). PHP also offers integration with various external libraries which enable the developer to do anything from generating PDF docum ents, accessing secure payment services and producing graphic output, to parsing XML.
PHP and MySQL Web Development - tutorial Book
The PHP server-side scripting language and the MySQL database management system (DBMS) make a potent pair. Both are open-source products--free of charge for most purposes--remarkably strong, and capable of handling all but the most enormous transaction loads. Both are supported by large, skilled, and enthusiastic communities of architects, programmers, and designers. PHP and MySQL Web Development introduces readers (who are assumed to have little or no experience with the title subjects) to PHP and MySQL for the purpose of creating dynamic Internet sites. It teaches the same skills as introductory Active Server Pages (ASP) and ColdFusion books--technologies that address the same niche.
PHP - Advanced Tutorial
PHP - Advanced Tutorial
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide
It hasn't taken Web developers long to discover that when it comes to creating dynamic, database-driven Web sites, MySQL and PHP provide a winning open source combination. Add this book to the mix, and there's no limit to the powerful, interactive Web sites that developers can create. With step-by-step instructions, complete scripts, and expert tips to guide readers, veteran author and database designer Larry Ullman gets right down to business: After grounding readers with separate discussions of first the scripting language (PHP) and then the database program (MySQL), he goes on to cover security, sessions and cookies, and using additional Web tools, with several sections devoted to creating sample applications.
AJAX Programming with PHP
This php pdf study material gives the details of AJAX Programming with PHP
PHP amp Zend Framework Tutorial
This php pdf study material gives the details of PHP amp Zend Framework Tutorial.PHP & Zend Framework Tutorial. I did a lot of googling, installing, re-installing, . but finally got my first php project to …
PHP-Nuke Tutorial
This php pdf study material gives the details of PHP-Nuke Tutorial..Encyclopedia - Definition of terms. Ephemerids - A “quote of the day” block. PHP-Nuke Tutorial. How to operate your PHP-Nuke website. 22603 West Main Street …
Learn to Implement a Shopping Cart on your website using Zend Framework
This example-driven tutorial will takes you through the process of building Model-View-Controller-based web applications. Users will learn to create and develop a storefront application on their websites.

Latest added PHP Tutorials

Beginning PHP and MySQL: From Novice to Professional, Third Edition
A comprehensive introduction to … the scripting language PHP and the MySQL database server. This book will not only expose … core aspects of both technologies, but will provide valuable insight into how they are used in unison to create dynamic data-driven Web applications.
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide
It hasn't taken Web developers long to discover that when it comes to creating dynamic, database-driven Web sites, MySQL and PHP provide a winning open source combination. Add this book to the mix, and there's no limit to the powerful, interactive Web sites that developers can create. With step-by-step instructions, complete scripts, and expert tips to guide readers, veteran author and database designer Larry Ullman gets right down to business: After grounding readers with separate discussions of first the scripting language (PHP) and then the database program (MySQL), he goes on to cover security, sessions and cookies, and using additional Web tools, with several sections devoted to creating sample applications.
The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP
Dreamweaver CS4 is a massive step forward in terms of integration with the rest of the CS4 suite (Flash, Fireworks, Photoshop, etc.), and also includes a whole host of exciting new features of its own. The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP concentrates on getting the best out of Dreamweaver CS4, rather than going into every menu item and toolbar icon. The emphasis is on developing websites compliant with the latest web standards, using CSS, JavaScript libraries (with particular emphasis on Spry, Adobe's implementation of Ajax), and PHP.
PHP and MySQL Web Development - tutorial Book
The PHP server-side scripting language and the MySQL database management system (DBMS) make a potent pair. Both are open-source products--free of charge for most purposes--remarkably strong, and capable of handling all but the most enormous transaction loads. Both are supported by large, skilled, and enthusiastic communities of architects, programmers, and designers. PHP and MySQL Web Development introduces readers (who are assumed to have little or no experience with the title subjects) to PHP and MySQL for the purpose of creating dynamic Internet sites. It teaches the same skills as introductory Active Server Pages (ASP) and ColdFusion books--technologies that address the same niche.
Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites
If you know HTML, this guide will have you building interactive websites quickly. You'll learn how to create responsive, data-driven websites with PHP, MySQL, and JavaScript, regardless of whether you already know how to program. Discover how the powerful combination of PHP and MySQL provides an easy way to build modern websites complete with dynamic data and user interaction. You'll also learn how to add JavaScript to create rich Internet applications and websites.
Learn to Implement a Shopping Cart on your website using Zend Framework
This example-driven tutorial will takes you through the process of building Model-View-Controller-based web applications. Users will learn to create and develop a storefront application on their websites.
User Interaction and Email Automation
In this tutorial we will see how Symfony can generate nice looking forms for us, before creating our own formatting class. We then progress to create a fully customized form. We will also learn about how Symfony can be expanded to use the other third-party libraries, and how can we convert a module into a fully working plugin that can be packaged up and reused in other projects.
Agile works best in PHP.
In this tutorial, we will cover the following: • Introductions to agile philosophy, including agile values and agile principles • Common problems and fears that developers face while developing a product • What is meant by agility and how it can help • Extreme programming principles • Advantages of agile process models • Team agility • Agile process models • Agile principles for the PHP project team
Magento Beginner's Guide
Get your Magento store up and running using Magento Beginner's Guide by William Rice.
Macronimous - Writing clean, secure and easy to maintain PHP code
Any code when written in a clean, easy to understand and formatted way is readily accepted and acclaimed by one and all.