<?
require_once('bililitePDF_2.php');

class 
image{
    public 
$w; // nominal width
    
public $h; // nominal height
    
public $subtype; // Form or Image
    
public $n; // object number, to be set when the object is completed
    
public function __construct ($w, $h, $subtype, $n=-1) {
        
$this->w = $w;
        
$this->h = $h;
        
$this->subtype = $subtype;
        
$this->n = $n;
    }
}

class 
bililitePDF_3 extends bililitePDF_2{
    protected 
$images = array();

    public function 
placeimage($name, $x, $y, $theta=0, $xscale=1, $yscale=NULL){
        
// put an image (created with appendXform or appendImage)
        // centered at $x,$y and rotated around the center by angle
        // $theta, with width and height scaled by factors $xscale and $yscale
        
if (is_null($yscale)) $yscale = $xscale;
        
$name = $this->toString($name);
        if (
is_resource($name)) $name = (string) $name; // explicitly cast images
        
$type = $this->images[$name]->subtype;
        
$w = $this->images[$name]->w;
        
$h = $this->images[$name]->h;
        
$centerx = -$w/2; // to move the center to $x, $y rather than the lower left
        
$centery = -$h/2;
        if (
$type == 'Image'){
            
// images are always plotted at 1 x 1
            // so rescale them to "natural" size
            
$xscale *= $w;
            
$yscale *= $h;
            
$centerx = -0.5;
            
$centery = -0.5;
        }
        
$rotation = sprintf('%f %f %f %f', cos($theta), sin($theta), -sin($theta), cos($theta));
        
// scale if needed; other adjustments if needed
        
$this->currentPage->contents .= "
            q
            1 0 0 1 
$x $y cm
            
$rotation 0 0 cm
            
$xscale 0 0 $yscale 0 0 cm
            1 0 0  1 
$centerx $centery cm
        "
;
        
$this->currentPage->contents .= $this->createName($name)." Do Q\n";
    }
    public function 
newForm(){
        
// Call before starting an XObject Form
        
$this->currentPage = new content();
    }
    public function 
appendForm($name, $w, $h){
        
// call after the XObject Form is complete, to create it with name $name, width $w, height $h
        
$this->currentPage->dict += array(
            
'Subtype' => $this->createName('Form'),
            
'Resources' => $this->createReference(RESOURCES),
            
'BBox' => $this->createArray(array(0, 0, $w, $h))
        );
        
$n = $this->appendObject($this->createStream($this->currentPage));
        
$this->images[$name] = new image($w, $h, 'Form', $n);
        
$this->currentPage = $this->pages[count($this->pages)-1];
    }
    public function 
appendImage($im){
        
// create an image XObject from PHP image resource $im
        // (such as created from imagecreatetruecolor)
        // and use it in the document with placeimage($im, $x, $y)
        
$image = new content();
        
$sx = imagesx($im);
        
$sy = imagesy($im);
        
$image->dict += array(
            
'Subtype' => $this->createName('Image'),
            
'Width' => $sx,
            
'Height' => $sy,
            
'ColorSpace' => $this->createName('DeviceRGB'),
            
'BitsPerComponent' => 8
        
);
        for (
$row = 0; $row < $sy; ++$row) for ($col = 0; $col < $sx; ++$col){
            
$colorindex = imagecolorat($im, $col, $row);
            
$colors = imagecolorsforindex($im, $colorindex);
            
$image->contents .= sprintf('%c%c%c', $colors['red'], $colors['green'], $colors['blue']);
        }
        
$n = $this->appendObject($this->createStream($image));
        
$this->images[$this->toString($im)] = new image($sx, $sy, 'Image', $n);
    }
    protected function 
dictResources(){
        
$dict = parent::dictResources();
        
$imagelist = array();
        foreach (
$this->images as $key=>$image) $imagelist[$key] = $this->createReference($image->n);
        
$dict['XObject'] = $this->createDictionary($imagelist);
        return 
$dict;
    }
}
?>