1: <?php
2: /**
3: * Jpeg.php
4: *
5: * @since 2011-05-23
6: * @category Library
7: * @package PdfImage
8: * @author Nicola Asuni <info@tecnick.com>
9: * @copyright 2011-2015 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\Import;
17:
18: use \Com\Tecnick\File\File;
19: use \Com\Tecnick\File\Byte;
20: use \Com\Tecnick\Pdf\Image\Exception as ImageException;
21:
22: /**
23: * Com\Tecnick\Pdf\Image\Import\Jpeg
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: class Jpeg
34: {
35: /**
36: * Extract data from a JPEG image
37: *
38: * @param string $data Image raw data
39: *
40: * @return array Image raw data array
41: */
42: public function getData($data)
43: {
44: $data['filter'] = 'DCTDecode';
45: $data['data'] = $data['raw'];
46: $byte = new Byte($data['raw']);
47: // extract embedded ICC profile (if any)
48: $icc = array();
49: $offset = 0;
50: while (($pos = strpos($data['raw'], 'ICC_PROFILE'."\0", $offset)) !== false) {
51: // get ICC sequence length
52: $length = ($byte->getUShort($pos - 2) - 16);
53: // marker sequence number
54: $msn = max(1, ord($data['raw'][($pos + 12)]));
55: // number of markers (total of APP2 used)
56: //$nom = max(1, ord($data['raw'][($pos + 13)]));
57: // get sequence segment
58: $icc[($msn - 1)] = substr($data['raw'], ($pos + 14), $length);
59: // move forward to next sequence
60: $offset = ($pos + 14 + $length);
61: }
62: // order and compact ICC segments
63: if (count($icc) > 0) {
64: ksort($icc);
65: $icc = implode('', $icc);
66: if (substr($icc, 36, 4) == 'acsp') {
67: // valid ICC profile
68: $data['icc'] = $icc;
69: }
70: }
71: return $data;
72: }
73: }
74: