Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ImageXObject
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
2 / 2
23
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 toPdf
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
1 / 1
22
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\Graphics\XObject;
6
7use Phpdftk\Pdf\Core\PdfArray;
8use Phpdftk\Pdf\Core\PdfBoolean;
9use Phpdftk\Pdf\Core\PdfDictionary;
10use Phpdftk\Pdf\Core\PdfName;
11use Phpdftk\Pdf\Core\PdfNumber;
12use Phpdftk\Pdf\Core\PdfReference;
13use Phpdftk\Pdf\Core\PdfStream;
14use Phpdftk\Pdf\Core\PdfString;
15
16/**
17 * Image XObject (/Subtype /Image) — ISO 32000-2 §8.9.5, Table 89.
18 */
19class ImageXObject extends PdfStream
20{
21    public const PDF_TYPE    = 'XObject';
22    public const PDF_SUBTYPE = 'Image';
23
24    public int $width;                       // /Width - required
25    public int $height;                      // /Height - required
26    public mixed $colorSpace;                // /ColorSpace
27    public int $bitsPerComponent;            // /BitsPerComponent
28
29    /** /Filter may be a single name or an array of names for filter chains. */
30    public PdfName|PdfArray|null $filter = null;
31    /** /DecodeParms may be a single dict or an array of dicts (one per filter). */
32    public PdfDictionary|PdfArray|null $decodeParams = null;
33
34    public ?PdfName $intent = null;          // /Intent
35    public ?bool $imageMask = null;          // /ImageMask
36    public ?PdfReference $mask = null;       // /Mask
37    public ?PdfReference $sMask = null;      // /SMask
38    public ?int $sMaskInData = null;         // /SMaskInData
39    public ?PdfArray $decode = null;         // /Decode
40    public ?bool $interpolate = null;        // /Interpolate
41    public ?PdfArray $alternates = null;     // /Alternates
42    public ?PdfName $nameField = null;       // /Name (deprecated)
43    public ?int $structParent = null;        // /StructParent
44    public ?PdfString $id = null;            // /ID
45    public ?PdfDictionary $opi = null;       // /OPI
46    public ?PdfReference $metadata = null;   // /Metadata - XMP
47    public ?PdfReference $oc = null;         // /OC
48    public ?PdfArray $af = null;             // /AF
49    public ?PdfReference $measure = null;    // /Measure
50    public ?PdfReference $ptData = null;     // /PtData
51    public ?PdfArray $matte = null;          // /Matte - pre-blended SMask matte
52
53    public function __construct(
54        int $width,
55        int $height,
56        mixed $colorSpace,
57        int $bitsPerComponent = 8,
58        string $data = '',
59    ) {
60        parent::__construct(new PdfDictionary(), $data);
61        $this->width = $width;
62        $this->height = $height;
63        $this->colorSpace = $colorSpace;
64        $this->bitsPerComponent = $bitsPerComponent;
65    }
66
67    public function toPdf(): string
68    {
69        $this->dictionary->set('Type', new PdfName(self::PDF_TYPE));
70        $this->dictionary->set('Subtype', new PdfName(self::PDF_SUBTYPE));
71        $this->dictionary->set('Width', new PdfNumber($this->width));
72        $this->dictionary->set('Height', new PdfNumber($this->height));
73
74        if ($this->colorSpace instanceof \Phpdftk\Pdf\Core\Serializable) {
75            $this->dictionary->set('ColorSpace', $this->colorSpace);
76        } else {
77            $this->dictionary->set('ColorSpace', new PdfName((string) $this->colorSpace));
78        }
79
80        $this->dictionary->set('BitsPerComponent', new PdfNumber($this->bitsPerComponent));
81
82        if ($this->filter !== null) {
83            $this->dictionary->set('Filter', $this->filter);
84        }
85        if ($this->decodeParams !== null) {
86            $this->dictionary->set('DecodeParms', $this->decodeParams);
87        }
88        if ($this->intent !== null) {
89            $this->dictionary->set('Intent', $this->intent);
90        }
91        if ($this->imageMask !== null) {
92            $this->dictionary->set('ImageMask', new PdfBoolean($this->imageMask));
93        }
94        if ($this->mask !== null) {
95            $this->dictionary->set('Mask', $this->mask);
96        }
97        if ($this->sMask !== null) {
98            $this->dictionary->set('SMask', $this->sMask);
99        }
100        if ($this->sMaskInData !== null) {
101            $this->dictionary->set('SMaskInData', new PdfNumber($this->sMaskInData));
102        }
103        if ($this->decode !== null) {
104            $this->dictionary->set('Decode', $this->decode);
105        }
106        if ($this->interpolate !== null) {
107            $this->dictionary->set('Interpolate', new PdfBoolean($this->interpolate));
108        }
109        if ($this->alternates !== null) {
110            $this->dictionary->set('Alternates', $this->alternates);
111        }
112        if ($this->nameField !== null) {
113            $this->dictionary->set('Name', $this->nameField);
114        }
115        if ($this->structParent !== null) {
116            $this->dictionary->set('StructParent', new PdfNumber($this->structParent));
117        }
118        if ($this->id !== null) {
119            $this->dictionary->set('ID', $this->id);
120        }
121        if ($this->opi !== null) {
122            $this->dictionary->set('OPI', $this->opi);
123        }
124        if ($this->metadata !== null) {
125            $this->dictionary->set('Metadata', $this->metadata);
126        }
127        if ($this->oc !== null) {
128            $this->dictionary->set('OC', $this->oc);
129        }
130        if ($this->af !== null) {
131            $this->dictionary->set('AF', $this->af);
132        }
133        if ($this->measure !== null) {
134            $this->dictionary->set('Measure', $this->measure);
135        }
136        if ($this->ptData !== null) {
137            $this->dictionary->set('PtData', $this->ptData);
138        }
139        if ($this->matte !== null) {
140            $this->dictionary->set('Matte', $this->matte);
141        }
142
143        return parent::toPdf();
144    }
145}