Zend
  Home arrow Zend arrow Page 5 - PDFs with PHP part 2
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
ZEND

PDFs with PHP part 2
By: Zend
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2004-01-07

    Table of Contents:
  • PDFs with PHP part 2
  • Color
  • RGB Color
  • Line Drawing
  • Circles
  • Page Add Modifications
  • File Checking
  • Example Use

  • 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

    Anyone looking for a way to modernize legacy data or easily migrate to a more cost-effective database without sacrificing functionality will benefit from this seminar. View the Intro to Advantage Database Server now!

    PDFs with PHP part 2 - Circles
    (Page 5 of 8 )


    Drawing a circle is slightly more complex. There is no native PDF syntax to draw a circle. However, luckily, PDF can draw not only straight lines, but also complex cubic Bezier curves. Using a combination of these we can create our own function to draw a circle.

    The following is a fairly good general introduction to Bezier curves in PDF, which you can use to create complex patterns or drawings, not only circles.


    function circle($x$y$r$style ''

        $style 
    strtolower($style); 
        
    if ($style == 'f') { 
            $op 
    'f';      // Style is fill only. 
        } elseif ($style == 'fd' || $style == 'df') { 
            $op = 'B';      // Style is fill and stroke. 
        } else { 
            $op = 'S';      // Style is stroke only. 
        } 
        
    $y $this->_h $y;                 // Adjust y value. 
        $b = $r * 0.552;                     // Length of the Bezier 
                                             // controls. 
        /* Move from the given origin and set the current point 
         * to the start of the first Bezier curve. */ 
        $c = sprintf('%.2f %.2f m', $x - $r, $y); 
        $x = $x - $r; 
        /* First circle quarter. */ 
        $c .= sprintf(' %.2f %.2f %.2f %.2f %.2f %.2f c', 
                      $x, $y + $b,           // First control point. 
                      $x + $r - $b, $y + $r, // Second control point. 
                      $x + $r, $y + $r);     // Final point. 
        /* Set x/y to the final point. */ 
        $x = $x + $r; 
        $y = $y + $r; 
        /* Second circle quarter. */ 
        $c .= sprintf(' %.2f %.2f %.2f %.2f %.2f %.2f c', 
                      $x + $b, $y, 
                      $x + $r, $y - $r + $b, 
                      $x + $r, $y - $r); 
        /* Set x/y to the final point. */ 
        $x = $x + $r; 
        $y = $y - $r; 
        /* Third circle quarter. */ 
        $c .= sprintf(' %.2f %.2f %.2f %.2f %.2f %.2f c', 
                      $x, $y - $b, 
                      $x - $r + $b, $y - $r, 
                      $x - $r, $y - $r); 
        /* Set x/y to the final point. */ 
        $x = $x - $r; 
        $y = $y - $r; 
        /* Fourth circle quarter. */ 
        $c .= sprintf(' %.2f %.2f %.2f %.2f %.2f %.2f c %s', 
                      $x - $b, $y, 
                      $x - $r, $y + $r - $b, 
                      $x - $r, $y + $r, 
                      $op); 
        /* Output the whole string. */ 
        $this->_out($c); 



    Note that the process is essentially one of creating four joined arcs, each one starting from where the previous one finished.

    PDF is “aware” of the current x/y position after having drawn an object. This means that you can concatenate several draw objects, following each new object on from where the last one left over, without having to explicitly move to the new x/y start position.

    Line Width

    Since we have introduced lines and drawings, it will be good to have a function to control the line width of our drawings. This function sets our class variable $this->_line_width and outputs it to the buffer, if there is an open document.


    function setLineWidth($width

        $this
    ->_line_width $width
        
    if ($this->_page 0) { 
            $this
    ->_out(sprintf('%.2f w'$width)); 
        





    More Zend Articles
    More By Zend


     

       

    ZEND ARTICLES

    - Taking the Zend Certified PHP Engineer Exam:...
    - Quick Introduction to PHP 5
    - PHP SOAP Extension
    - Improving Performance
    - PDFs with PHP part 2
    - PDFs with PHP part 1
    - PHP at Lycos
    - Build Database Interfaces

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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