Skip to content

PHP FPDF library Example to Draw 3D Solid Color Star Shape in PDF Document

This script draws a star in a given position, with a given number of points and with a given radius.

Star(float X, float Y, float rin, float rout, int points [, string style])

X: abscissa of center.
Y: ordinate of center.
rin: internal radius.
rout: external radius.
points: number of points that the star is composed of.
style: style of rendering, the same as for Rect(): D, F or FD.

Note: if rin=rout, the star will appear as a circle.

star.php

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

class PDF_Star extends FPDF
{

function Star($x, $y, $rin, $rout, $points, $style='D')
{
    if($style=='F')
        $op = 'f';
    elseif($style=='FD' || $style=='DF')
        $op = 'B';
    else
        $op = 'S';
    $dth = M_PI/$points;
    $th = 0;
    $k = $this->k;
    $h = $this->h;
    $points_string = '';
    for($i=0;$i<($points*2)+1;$i++)
    {
        $th += $dth;
        $cx = $x + (($i%2==0 ? $rin : $rout) * cos($th));
        $cy = $y + (($i%2==0 ? $rin : $rout) * sin($th));
        $points_string .= sprintf('%.2F %.2F', $cx*$k, ($h-$cy)*$k);
        if($i==0)
            $points_string .= ' m ';
        else
            $points_string .= ' l ';
    }
    $this->_out($points_string . $op);
}

}
?>

index.php

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

$pdf = new PDF_Star();
$pdf->AddPage();
$pdf->SetDrawColor(0,0,0);
$pdf->SetFillColor(255,0,0);
$pdf->SetLineWidth(0.5);
$pdf->Star(100,60,40,30,36,'DF');
$pdf->Output();
?>