Home arrow Zend arrow Page 5 - PDFs with PHP part 2

Circles - Zend

This tutorial is intended for the PHP programmer who needs to incorporate PDF generation in a script without using external libraries such as PDFlib (often unavailable due to licensing restrictions or lack of funds). This tutorial is the second of two parts, and builds on what was covered in the first part. Therefore, if you have not yet gone through Part 1, you are advised to do so (or at least read through it), before going through this tutorial (Part 2). Apart from what was dealt with in Part 1, no knowledge of PDF file structure is required to understand this tutorial, as all references are explained.

TABLE OF CONTENTS:
  1. PDFs with PHP part 2
  2. Color
  3. RGB Color
  4. Line Drawing
  5. Circles
  6. Page Add Modifications
  7. File Checking
  8. Example Use
By: Zend
Rating: starstarstarstarstar / 5
January 07, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: