PHP
  Home arrow PHP arrow Page 4 - Beginning PHP4
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Beginning PHP4
By: Dev Shed
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 22
    2000-12-19


    Table of Contents:
  • Beginning PHP4
  • Introduction
  • Laying a Foundation
  • Creating an Image
  • Drawing on our Image
  • Putting it all Together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Beginning PHP4 - Creating an Image
    ( Page 4 of 6 )

    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.



     
     
    >>> More PHP Articles          >>> More By Dev Shed
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT