fpdf
PHP powered
home was ist fpdf? downloads & add-ons dokumentation faq forum impressum links kontakt
 

Downloads > Add-On

 
         
 

Clipping

Informations

Author: Andreas Würmser
License: Freeware

Description

This script allows to define a clipping area. A clipping area restricts the display and prevents any elements from showing outside of it.
3 shapes are available: text, rectangle and ellipse. For each one, you can choose whether to draw the outline or not.

ClippingText(float x, float y, string txt [, boolean outline])
ClippingRect(float x, float y, float w, float h [, boolean outline])
ClippingEllipse(float x, float y, float rx, float ry [, boolean outline])

The default value of outline is false.
Once you have finished using the clipping, you must remove it with UnsetClipping().

Additionally, a clipped version of Cell() is provided:

ClippedCell(double w [, double h [, string txt [, mixed border [, int ln [, string align[, int fill [, mixed link]]]]]]])

Source

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

class
PDF_Clipping extends FPDF
{
    function ClippingText($x, $y, $txt, $outline=false)
    {
        $op=$outline ? 5 : 7;
        $this->_out(sprintf('q BT %.2f %.2f Td %d Tr (%s) Tj 0 Tr ET',
            $x*$this->k,
            ($this->h-$y)*$this->k,
            $op,
            $this->_escape($txt)));
    }

    function ClippingRect($x, $y, $w, $h, $outline=false)
    {
        $op=$outline ? 'S' : 'n';
        $this->_out(sprintf('q %.2f %.2f %.2f %.2f re W %s',
            $x*$this->k,
            ($this->h-$y)*$this->k,
            $w*$this->k, -$h*$this->k,
            $op));
    }

    function ClippingEllipse($x, $y, $rx, $ry, $outline=false)
    {
        $op=$outline ? 'S' : 'n';
        $lx=4/3*(M_SQRT2-1)*$rx;
        $ly=4/3*(M_SQRT2-1)*$ry;
        $k=$this->k;
        $h=$this->h;
        $this->_out(sprintf('q %.2f %.2f m %.2f %.2f %.2f %.2f %.2f %.2f c',
            ($x+$rx)*$k, ($h-$y)*$k,
            ($x+$rx)*$k, ($h-($y-$ly))*$k,
            ($x+$lx)*$k, ($h-($y-$ry))*$k,
            $x*$k, ($h-($y-$ry))*$k));
        $this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c',
            ($x-$lx)*$k, ($h-($y-$ry))*$k,
            ($x-$rx)*$k, ($h-($y-$ly))*$k,
            ($x-$rx)*$k, ($h-$y)*$k));
        $this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c',
            ($x-$rx)*$k, ($h-($y+$ly))*$k,
            ($x-$lx)*$k, ($h-($y+$ry))*$k,
            $x*$k, ($h-($y+$ry))*$k));
        $this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c W %s',
            ($x+$lx)*$k, ($h-($y+$ry))*$k,
            ($x+$rx)*$k, ($h-($y+$ly))*$k,
            ($x+$rx)*$k, ($h-$y)*$k,
            $op));
    }

    function UnsetClipping()
    {
        $this->_out('Q');
    }

    function ClippedCell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='')
    {
        if($border || $fill || $this->y+$h>$this->PageBreakTrigger)
        {
            $this->Cell($w, $h, '', $border, 0, '', $fill);
            $this->x-=$w;
        }
        $this->ClippingRect($this->x, $this->y, $w, $h);
        $this->Cell($w, $h, $txt, '', $ln, $align, 0, $link);
        $this->UnsetClipping();
    }
}
?>

Example

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

$pdf = new PDF_Clipping();
$pdf->AddPage();

//example of clipped cell
$pdf->SetFont('Arial', '', 14);
$pdf->SetX(72);
$pdf->ClippedCell(60, 6, 'These are clipping examples', 1);

//example of clipping text
$pdf->SetFont('Arial', 'B', 120);
//set the outline color
$pdf->SetDrawColor(0);
//set the outline width (note that only its outer half will be shown)
$pdf->SetLineWidth(2);
//draw the clipping text
$pdf->ClippingText(40, 55, 'CLIPS', true);
//fill it with the image
$pdf->Image('clips.jpg', 40, 10, 130);
//remove the clipping
$pdf->UnsetClipping();

//example of clipping rectangle
$pdf->ClippingRect(45, 65, 116, 20, true);
$pdf->Image('clips.jpg', 40, 10, 130);
$pdf->UnsetClipping();

//example of clipping ellipse
$pdf->ClippingEllipse(102, 104, 16, 10, true);
$pdf->Image('clips.jpg', 40, 10, 130);
$pdf->UnsetClipping();

$pdf->Output();
?>

View the result here.

Download

ZIP | TGZ
 






 
         
         
 
 
fpdf.de © Copyright 2004-2006 carrib internet solutions - Beachten Sie bitte die Nutzungsbedingungen
Eine Verwendung der auf fpdf.de befindlichen Inhalte (Texte, Grafiken) ist nur mit Zustimmung des Betreibers zulässig.