Zum Inhalt

Circular text: Text im Kreis

CircularText($x, $y, $r, $text, $align, $kerning, $fontwidth) schreibt Text entlang eines Kreises um den Mittelpunkt, oben (top) oder unten (bottom). Das Skript braucht das Add-on Transformations. Typische Anwendung sind Stempel und Siegel.

Nummer82
OriginaltitelCircular text
AutorAndreas Würmser
LizenzFPDF (frei, auch für kommerzielle Nutzung)
Quellefpdf.org, Skript 82 (englisch, mit Archiv zum Herunterladen)

Quelltext

Speichern Sie die Klasse in einer eigenen Datei neben fpdf.php. Kommentare und Bezeichner stehen wie im Original auf Englisch.

PHP
<?php
require('transform.php');

class PDF_CircularText extends PDF_Transform{

    function CircularText($x, $y, $r, $text, $align='top', $kerning=120, $fontwidth=100)
    {
        $kerning/=100;
        $fontwidth/=100;        
        if($kerning==0) $this->Error('Please use values unequal to zero for kerning');
        if($fontwidth==0) $this->Error('Please use values unequal to zero for font width');        
        //get width of every letter
        $t=0;
        for($i=0; $i<strlen($text); $i++){
            $w[$i]=$this->GetStringWidth($text[$i]);
            $w[$i]*=$kerning*$fontwidth;
            //total width of string
            $t+=$w[$i];
        }
        //circumference
        $u=($r*2)*M_PI;
        //total width of string in degrees
        $d=($t/$u)*360;
        $this->StartTransform();
        // rotate matrix for the first letter to center the text
        // (half of total degrees)
        if($align=='top'){
            $this->Rotate($d/2, $x, $y);
        }
        else{
            $this->Rotate(-$d/2, $x, $y);
        }
        //run through the string
        for($i=0; $i<strlen($text); $i++){
            if($align=='top'){
                //rotate matrix half of the width of current letter + half of the width of preceding letter
                if($i==0){
                    $this->Rotate(-(($w[$i]/2)/$u)*360, $x, $y);
                }
                else{
                    $this->Rotate(-(($w[$i]/2+$w[$i-1]/2)/$u)*360, $x, $y);
                }
                if($fontwidth!=1){
                    $this->StartTransform();
                    $this->ScaleX($fontwidth*100, $x, $y);
                }
                $this->SetXY($x-$w[$i]/2, $y-$r);
            }
            else{
                //rotate matrix half of the width of current letter + half of the width of preceding letter
                if($i==0){
                    $this->Rotate((($w[$i]/2)/$u)*360, $x, $y);
                }
                else{
                    $this->Rotate((($w[$i]/2+$w[$i-1]/2)/$u)*360, $x, $y);
                }
                if($fontwidth!=1){
                    $this->StartTransform();
                    $this->ScaleX($fontwidth*100, $x, $y);
                }
                $this->SetXY($x-$w[$i]/2, $y+$r-($this->FontSize));
            }
            $this->Cell($w[$i],$this->FontSize,$text[$i],0,0,'C');
            if($fontwidth!=1){
                 $this->StopTransform();
            }
        }
        $this->StopTransform();
    }

}
?>

Beispiel

So wird die Erweiterung aufgerufen:

PHP
<?php
require('circulartext.php');

$pdf = new PDF_CircularText();
$pdf->AddPage();
$pdf->SetFont('Arial','',32);

$text='Circular Text';
$pdf->CircularText(105, 50, 30, $text, 'top');
$pdf->CircularText(105, 50, 30, $text, 'bottom');

$pdf->Output();
?>
Ergebnis des Beispiels als PDF, erste Seite
Ergebnis: PDF ansehen

Einbau

Add-ons werden nicht installiert, sondern eingebunden: Datei neben fpdf.php legen, mit require laden und die eigene Klasse von der Klasse des Add-ons ableiten. Brauchen Sie mehrere Add-ons, leiten Sie sie in einer Kette voneinander ab. Mehr dazu unter Installation und in der FAQ.