Dashed rectangle

Informations

Author: Antoine Michéa
License: GPL

Description

Allows to draw a dashed rectangle.

DashedRect(float x1, float y1, float x2, float y2 [, float width [, int nb]])

x1, y1: upper left corner of the rectangle.
x2, y2: lower right corner of the rectangle.
width: dash thickness (1 by default).
nb: number of dashes per line (15 by default).

Note: for a more complete support of dashes, see this script.

Source

<?php
/*
Author     : Antoine Michéa
Web        : saturn-share.org
Program    : dashed_rect.php
License    : GPL v2
Description: Allows to draw a dashed rectangle. Parameters are:
             x1, y1 : upper left corner of the rectangle.
             x2, y2 : lower right corner of the rectangle.
             width  : dash thickness (1 by default).
             nb     : number of dashes per line (15 by default).
Date       : 2003-01-07
*/

require('fpdf.php');

class PDF_DashedRect extends FPDF
{
    function DashedRect($x1, $y1, $x2, $y2, $width=1, $nb=15)
    {
        $this->SetLineWidth($width);
        $longueur=abs($x1-$x2);
        $hauteur=abs($y1-$y2);
        if($longueur>$hauteur) {
            $Pointilles=($longueur/$nb)/2; // length of dashes
        }
        else {
            $Pointilles=($hauteur/$nb)/2;
        }
        for($i=$x1;$i<=$x2;$i+=$Pointilles+$Pointilles) {
            for($j=$i;$j<=($i+$Pointilles);$j++) {
                if($j<=($x2-1)) {
                    $this->Line($j,$y1,$j+1,$y1); // upper dashes
                    $this->Line($j,$y2,$j+1,$y2); // lower dashes
                }
            }
        }
        for($i=$y1;$i<=$y2;$i+=$Pointilles+$Pointilles) {
            for($j=$i;$j<=($i+$Pointilles);$j++) {
                if($j<=($y2-1)) {
                    $this->Line($x1,$j,$x1,$j+1); // left dashes
                    $this->Line($x2,$j,$x2,$j+1); // right dashes
                }
            }
        }
    }
}
?>

Example

<?php
require('dashed_rect.php');

$pdf=new PDF_DashedRect();
$pdf->AddPage();
$pdf->SetDrawColor(200);
$pdf->DashedRect(40,30,165,60);
$pdf->SetFont('Arial','B',30);
$pdf->SetXY(40,30);
$pdf->Cell(125,30,'Enjoy dashes!',0,0,'C');
$pdf->Output();
?>
View the result here.

Download

ZIP | TGZ