PHP
  Home arrow PHP arrow Page 6 - Beginning PHP4
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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 - Putting it all Together


    (Page 6 of 6 )

    So far we've covered creating an image, outputting it to the browser and cleaning up after ourselves. Between creating the image and outputting it we also covered creating lines, circles and rectangles on our image.

    PHP currently has no function that allows us to create a rectangle with rounded corners. In order to recap what we covered so far, we're going to create a function that does just that. We'll be able to pass this function the same information that you would pass to ImageRectangle(), but with an extra argument telling it the radius of the arc that we want to use for the corner. Our prototype will therefore be:


    
    udImageRoundRect($image, $x1, $y1, $x2, $y2, $arcradius, $color)
    



    We'll never use $x1, $y1, $x2, or $y2 as actual points in any of the plots, but we will use them to work out where our arcs must be centered, as well as where our lines must start and end. In the image above, we know that ($x1, $y1) is where our rectangle would have started if it didn't have a rounded corner. We can work out where the center of the arc must be by adding $arcradius to each of $x1 and $y1. Remember that we will only be adding to both x and y values in the top left-hand corner of the rectangle. In other corners we will have to subtract $arcradius from one or both of the values.

    Let's dive into the code and take a look. We're going to define our function in an include file called roundrect.inc:


    <?php
    //roundrect.inc
    function udImageRoundRect($image,$x1,$y1,$x2,$y2,$arcradius,$color) {
    $arcwidth = ($arcradius*2);
    // top left hand corner
    ImageArc($image, $x1+$arcradius, $y1+$arcradius,
    $arcwidth,      $arcwidth,
    180,            270,
    $color);
    



    The first thing we do is to double $arcradius, giving us the width of the arc; we can pass this directly to ImageArc().

    The next line draws the top left-hand corner arc. The center of the arc is at:

    x = $x1 + $arcradius (just to the right of the corner of the rectangle), y = $y1 + $arcradius (just down from the same corner). The width and height of the arc are both equal to $arcwidth, since we want the corner to be rounded, not ellipsoid. We start the arc at 180° (9 o'clock) and end at 270° (12 o'clock) – a 90° arc. As we move around the corners our degrees will shift by 90° each time.


    // top right hand corner
    ImageArc($image, $x2-$arcradius, $y1+$arcradius,
    $arcwidth,      $arcwidth,
    270,            360,            $color);
    



    Now that we're at the top right-hand corner we must use the $x2 value and this time subtract $arcradius – the center of the arc will be to the left of the rectangle corner. We are still working with $y1 and since it's at the same horizontal level, we still use $y1 + $arcradius. As you can see in the diagram opposite, we also need to shift each of our degrees clockwise by 90 degrees.



    Each of the bottom corners work in exactly the same way; you just have to remember whether to add or subtract, and which x and y values you should be working with:


    // bottom right hand corner
    ImageArc($image, $x2-$arcradius, $y2-$arcradius,
    $arcwidth,      $arcwidth,
    0,              90,             $color);
    // bottom left hand corner
    ImageArc($image, $x1+$arcradius, $y2-$arcradius,
    $arcwidth,      $arcwidth,
    90,             180,             $color);
    

    The last part of our script draws in the connecting lines between the rounded corners. Now if we were going to draw in our top line as if we weren't using rounded corners, we would simply use ($x1, $y1) as our first coordinate and ($x2, $y1) as our second coordinate. Since we have to take the corners into account, we must adjust some of these coordinates by $arcradius:


    // top line
    ImageLine($image, $x1+$arcradius, $y1,
    $x2-$arcradius, $y1, $color);
    // right line
    ImageLine($image, $x2, $y1+$arcradius,
    $x2, $y2-$arcradius, $color);
    // bottom line
    ImageLine($image, $x1+$arcradius, $y2,
    $x2-$arcradius, $y2, $color);
    // left line
    ImageLine($image, $x1, $y1+$arcradius,
    $x1, $y2-$arcradius, $color);
    }
    ?>
    

    And that's it for roundrect.inc. We can now write a PHP script like this:


    <?php
    //roundrect.php
    Header("Content-type: image/jpeg");
    include "roundrect.inc";
    



    $image = ImageCreate(200,150);
    $gray = ImageColorAllocate($image,204,204,204);
    $blue = ImageColorAllocate($image,0,0,255);
    udImageRoundRect($image,10,10,190,140,30,$blue);
    ImageJPEG($image);
    ImageDestroy($image);
    ?>
    



    The first line includes the roundrect.inc file created above, so we have access to our new user-defined function udImageRoundRect():


    
    udImageRoundRect($image,10,10,190,140,30,$blue);
    

    We start the rectangle at (10,10) and end it at (190,140); 10 pixels clear of each edge of the image. The radius of our corner arcs will be 30 pixels. If we run the script we get an image like this:




    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway