source-class-Com.Tecnick.Pdf.Image.Output

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * Output.php
  4:  *
  5:  * @since       2011-05-23
  6:  * @category    Library
  7:  * @package     PdfImage
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2011-2016 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-pdf-image
 12:  *
 13:  * This file is part of tc-lib-pdf-image software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Pdf\Image;
 17: 
 18: use \Com\Tecnick\Pdf\Image\Import;
 19: use \Com\Tecnick\Pdf\Encrypt\Encrypt;
 20: use \Com\Tecnick\Pdf\Image\Exception as ImageException;
 21: 
 22: /**
 23:  * Com\Tecnick\Pdf\Image\Output
 24:  *
 25:  * @since       2011-05-23
 26:  * @category    Library
 27:  * @package     PdfImage
 28:  * @author      Nicola Asuni <info@tecnick.com>
 29:  * @copyright   2011-2016 Nicola Asuni - Tecnick.com LTD
 30:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 31:  * @link        https://github.com/tecnickcom/tc-lib-pdf-image
 32:  */
 33: abstract class Output
 34: {
 35:     /**
 36:      * Current PDF object number
 37:      *
 38:      * @var int
 39:      */
 40:     protected $pon;
 41: 
 42:     /**
 43:      * Unit of measure conversion ratio
 44:      *
 45:      * @var float
 46:      */
 47:     protected $kunit = 1.0;
 48: 
 49:     /**
 50:      * Encrypt object
 51:      *
 52:      * @var Encrypt
 53:      */
 54:     protected $enc;
 55: 
 56:     /**
 57:      * True if we are in PDF/A mode.
 58:      *
 59:      * @var bool
 60:      */
 61:     protected $pdfa = false;
 62: 
 63:     /**
 64:      * Store image object IDs for the XObject Dctionary.
 65:      *
 66:      * @var array
 67:      */
 68:     protected $xobjdict = array();
 69: 
 70:     /**
 71:      * Initialize images data
 72:      *
 73:      * @param float   $kunit  Unit of measure conversion ratio.
 74:      * @param Encrypt $enc    Encrypt object.
 75:      * @param bool    $pdfa   True if we are in PDF/A mode.
 76:      */
 77:     public function __construct($kunit, Encrypt $enc, $pdfa = false)
 78:     {
 79:         $this->kunit = (float) $kunit;
 80:         $this->enc = $enc;
 81:         $this->pdfa = (bool) $pdfa;
 82:     }
 83: 
 84:     /**
 85:      * Returns current PDF object number
 86:      *
 87:      * @return int
 88:      */
 89:     public function getObjectNumber()
 90:     {
 91:         return $this->pon;
 92:     }
 93: 
 94:     /**
 95:      * Get the PDF output string to print the specified image ID
 96:      *
 97:      * @param int $iid        Image ID
 98:      * @param int $xpos       Abscissa (X coordinate) of the upper-left Image corner
 99:      * @param int $ypos       Ordinate (Y coordinate) of the upper-left Image corner
100:      * @param int $width      Image width in user units
101:      * @param int $height     Image height in user units
102:      * @param int $pageheight Page height in user units
103:      *
104:      * @return string
105:      */
106:     public function getSetImage($iid, $xpos, $ypos, $width, $height, $pageheight)
107:     {
108:         if (empty($this->image[$iid])) {
109:             throw new ImageException('Unknown image ID: '.$iid);
110:         }
111:         $out = 'q';
112:         $out .= sprintf(
113:             ' %F 0 0 %F %F %F cm',
114:             ($width * $this->kunit),
115:             ($height * $this->kunit),
116:             ($xpos * $this->kunit),
117:             (($pageheight - $ypos - $height) * $this->kunit) // reverse coordinate
118:         );
119:         if (!empty($this->cache[$this->image[$iid]['key']]['mask'])) {
120:             $out .= ' /IMGmask'.$iid.' Do';
121:             if (!empty($this->cache[$this->image[$iid]['key']]['plain'])) {
122:                 $out .= ' /IMGplain'.$iid.' Do';
123:             }
124:         } else {
125:             $out .= ' /IMG'.$iid.' Do';
126:         }
127:         $out .= ' Q';
128:         return $out;
129:     }
130: 
131:     /**
132:      * Get the PDF output string for Images
133:      *
134:      * @param int $pon Current PDF Object Number
135:      *
136:      * @return string
137:      */
138:     public function getOutImagesBlock($pon)
139:     {
140:         $this->pon = (int) $pon;
141:         $out = '';
142:         foreach ($this->image as $iid => $img) {
143:             if (empty($this->cache[$img['key']]['out'])) {
144:                 if (!empty($this->cache[$img['key']]['mask'])) {
145:                     $out .= $this->getOutImage($img, $this->cache[$img['key']]['mask'], 'mask');
146:                     if (!empty($this->cache[$img['key']]['plain'])) {
147:                         $out .= $this->getOutImage($img, $this->cache[$img['key']]['plain'], 'plain');
148:                     }
149:                 } else {
150:                     $out .= $this->getOutImage($img, $this->cache[$img['key']]);
151:                 }
152:                 $this->image[$iid] = $img;
153:             }
154: 
155:             if (!empty($this->cache[$img['key']]['mask']['obj'])) {
156:                 $this->xobjdict['IMG'.$img['iid']] = $this->cache[$img['key']]['mask']['obj'];
157:                 if (!empty($this->cache[$img['key']]['plain']['obj'])) {
158:                     $this->xobjdict['IMG'.$img['iid']] = $this->cache[$img['key']]['plain']['obj'];
159:                 }
160:             } else {
161:                 $this->xobjdict['IMG'.$img['iid']] = $this->cache[$img['key']]['obj'];
162:             }
163:         }
164:         return $out;
165:     }
166: 
167:     /**
168:      * Get the PDF output string for Image object
169:      *
170:      * @param array  $img  Image reference
171:      * @param array  $data Image raw data
172:      * @param string $sub  Sub image ('mask', 'plain' or empty string)
173:      *
174:      * @return string
175:      */
176:     protected function getOutImage(&$img, &$data, $sub = '')
177:     {
178:         $out = $this->getOutIcc($data)
179:                 .$this->getOutPalette($data)
180:                 .$this->getOutAltImages($img, $data, $sub);
181: 
182:         $data['obj'] = ++$this->pon;
183: 
184:         $out .= $this->pon.' 0 obj'."\n"
185:             .'<</Type /XObject'
186:             .' /Subtype /Image'
187:             .' /Width '.$data['width']
188:             .' /Height '.$data['height']
189:             .$this->getOutColorInfo($data);
190: 
191:         if (!empty($data['exturl'])) {
192:             // external stream
193:             $out .= ' /Length 0'
194:                 .' /F << /FS /URL /F '.$this->enc->escapeDataString($data['exturl'], $this->pon).' >>';
195:             if (!empty($data['filter'])) {
196:                 $out .= ' /FFilter /'.$data['filter'];
197:             }
198:             $out .= ' >> stream'."\n"
199:                 .'endstream'."\n";
200:         } else {
201:             if (!empty($data['filter'])) {
202:                 $out .= ' /Filter /'.$data['filter'];
203:             }
204:             if (!empty($data['parms'])) {
205:                 $out .= ' '.$data['parms'];
206:             }
207: 
208:             // Colour Key Masking
209:             if (!empty($data['trns'])) {
210:                 $trns = $this->getOutTransparency($data);
211:                 if (!empty($trns)) {
212:                     $out .= ' /Mask [ '.$trns.']';
213:                 }
214:             }
215: 
216:             $stream = $this->enc->encryptString($data['data'], $this->pon);
217:             $out .=' /Length '.strlen($stream)
218:                 .'>> stream'."\n"
219:                 .$stream."\n"
220:                 .'endstream'."\n";
221:         }
222: 
223:         $out .= 'endobj'."\n";
224: 
225:         $this->cache[$img['key']]['out'] = true; // mark this as done
226: 
227:         return $out;
228:     }
229: 
230:     /**
231:     * Return XObjects Dictionary portion for the images
232:     *
233:     * @return string
234:     */
235:     public function getXobjectDict()
236:     {
237:         $out = '';
238:         foreach ($this->xobjdict as $iid => $objid) {
239:             $out .= ' /'.$iid.' '.$objid.' 0 R';
240:         }
241:         return $out;
242:     }
243: 
244:     /**
245:      * Get the PDF output string for ICC object
246:      *
247:      * @param array  $data Image raw data
248:      *
249:      * @return string
250:      */
251:     protected function getOutIcc(&$data)
252:     {
253:         if (empty($data['icc'])) {
254:             return '';
255:         }
256: 
257:         $data['obj_icc'] = ++$this->pon;
258:         $stream = $this->enc->encryptString(gzcompress($data['icc']), $this->pon);
259: 
260:         return $this->pon.' 0 obj'."\n"
261:             .'<</N '.$data['channels']
262:             .' /Alternate /'.$data['colspace']
263:             .' /Filter /FlateDecode'
264:             .' /Length '.strlen($stream)
265:             .'>> stream'."\n"
266:             .$stream."\n"
267:             .'endstream'."\n"
268:             .'endobj'."\n";
269:     }
270: 
271:     /**
272:      * Get the PDF output string for Indexed palette object
273:      *
274:      * @param array  $data Image raw data
275:      *
276:      * @return string
277:      */
278:     protected function getOutPalette(&$data)
279:     {
280:         if ($data['colspace'] != 'Indexed') {
281:             return '';
282:         }
283: 
284:         $data['obj_pal'] = ++$this->pon;
285:         $stream = $this->enc->encryptString(gzcompress($data['pal']), $this->pon);
286: 
287:         return $this->pon.' 0 obj'."\n"
288:             .'<</Filter /FlateDecode'
289:             .' /Length '.strlen($stream)
290:             .'>> stream'."\n"
291:             .$stream."\n"
292:             .'endstream'."\n"
293:             .'endobj'."\n";
294:     }
295: 
296:     /**
297:      * Get the PDF output string for color and mask information
298:      *
299:      * @param array  $data Image raw data
300:      *
301:      * @return string
302:      */
303:     protected function getOutColorInfo($data)
304:     {
305:         $out = '';
306:         // set color space
307:         if (!empty($data['obj_icc'])) {
308:             // ICC Colour Space
309:             $out .= ' /ColorSpace [/ICCBased '.$data['obj_icc'].' 0 R]';
310:         } elseif (!empty($data['obj_pal'])) {
311:             // Indexed Colour Space
312:             $out .= ' /ColorSpace [/Indexed /DeviceRGB '
313:                 .((strlen($data['pal']) / 3) - 1)
314:                 .' '.$data['obj_pal'].' 0 R]';
315:         } else {
316:             // Device Colour Space
317:             $out .= ' /ColorSpace /'.$data['colspace'];
318:         }
319:         if ($data['colspace'] == 'DeviceCMYK') {
320:             $out .= ' /Decode [1 0 1 0 1 0 1 0]';
321:         }
322:         $out .= ' /BitsPerComponent '.$data['bits'];
323:         
324:         if (!empty($this->cache[$data['key']]['mask']['obj'])) {
325:             $out .= ' /SMask '.$this->cache[$data['key']]['mask']['obj'].' 0 R';
326:         }
327:         
328:         if (!empty($data['obj_alt'])) {
329:             // reference to alternate images dictionary
330:             $out .= ' /Alternates '.$data['obj_alt'].' 0 R';
331:         }
332:         return $out;
333:     }
334: 
335:     /**
336:      * Get the PDF output string for Alternate images object
337:      *
338:      * @param array  $img  Image reference
339:      * @param array  $data Image raw data
340:      * @param string $sub  Sub image ('mask', 'plain' or empty string)
341:      *
342:      * @return string
343:      */
344:     protected function getOutAltImages($img, &$data, $sub = '')
345:     {
346:         if ($this->pdfa || empty($img['altimgs']) || ($sub == 'mask')) {
347:             return '';
348:         }
349: 
350:         $data['obj_alt'] = ++$this->pon;
351: 
352:         $out = $this->pon.' 0 obj'."\n"
353:             .'[';
354:         foreach ($img['altimgs'] as $iid) {
355:             if (!empty($this->cache[$this->image[$iid]['key']]['obj'])) {
356:                 $out .= ' <<'
357:                     .' /Image '.$this->cache[$this->image[$iid]['key']]['obj'].' 0 R'
358:                     .' /DefaultForPrinting '.(empty($this->image[$iid]['defprint']) ? 'false' : 'true')
359:                     .' >>';
360:             }
361:         }
362:         $out .= ' ]'."\n"
363:             .'endobj'."\n";
364: 
365:         return $out;
366:     }
367: 
368:     /**
369:      * Get the PDF output string for color and mask information
370:      *
371:      * @param array  $data Image raw data
372:      *
373:      * @return string
374:      */
375:     protected function getOutTransparency($data)
376:     {
377:         $trns = '';
378:         foreach ($data['trns'] as $idx => $val) {
379:             if ($val == 0) {
380:                 $trns .= $idx.' '.$idx.' ';
381:             }
382:         }
383:         return $trns;
384:     }
385: }
386: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy