Web Development

  Homes arrow Web Development arrow Beginning PHP4 : Generating Graphics Part 3 -...
 Webmaster Tools
 
Base64 Encoding 
Browser Settings 
CSS Coder 
CSS Navigation Menu 
Datetime Converter 
DHTML Tooltip 
Dig Utility 
DNS Utility 
Dropdown Menu 
Fetch Content 
Fetch Header 
Floating Layer 
htaccess Generator 
HTML to PHP 
HTML Encoder 
HTML Entities 
IP Convert 
Meta Tags 
Password Encryption
 
Password Strength
 
Pattern Extractor 
Ping Utility 
Pop-Up Window 
Regex Extractor 
Regex Match 
Scrollbar Color 
Source Viewer 
Syntax Highlighting 
URL Encoding 
Web Safe Colors 
Forums Sitemap 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WEB DEVELOPMENT

Beginning PHP4 : Generating Graphics Part 3 - Putting it all Together
By: Developer Shed
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2004-04-17

    Table of Contents:

    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
     
     

    SEARCH DEV MECHANIC

    TOOLS YOU CAN USE

    advertisement

    Professional JSP : Architecture Part 3 - Architectures
    by Wrox Books

    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.

    More Web Development Articles
    More By Developer Shed

       

    WEB DEVELOPMENT ARTICLES

    - On Page SEO for New Domains
    - Improve Your Site`s Speed
    - Safari Books Online Review
    - Creating an Estore From the Ground Up
    - Most Common SEO Mistakes Developers Make
    - Making the Most of Your Titles and Meta Desc...
    - Five Ways Using Flash Can Damage Your Site
    - A Web Designer`s Guide to Colors
    - Use Webstarts to Create a Free Site
    - More Than Just Looks. How Your Web Design C...
    - How to Design Content Pages
    - Mint Review
    - Make Your WordPress Website Look Professional
    - How to Create a Mobile Web Site
    - Meta Tags: Still Useful?

    Developer Shed Affiliates

     



    © 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap