Debian Codegraph @

lib/3rdParty/tcpdf/include/tcpdf_images.php

raw symbols SCIP
1 <?php
2 //============================================================+
3 // File name : tcpdf_images.php
4 // Version : 1.0.005
5 // Begin : 2002-08-03
6 // Last Update : 2014-11-15
7 // Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - [email protected]
8 // License : GNU-LGPL v3 (https://www.gnu.org/copyleft/lesser.html)
9 // -------------------------------------------------------------------
10 // Copyright (C) 2002-2026 Nicola Asuni - Tecnick.com LTD
11 //
12 // This file is part of TCPDF software library.
13 //
14 // TCPDF is free software: you can redistribute it and/or modify it
15 // under the terms of the GNU Lesser General Public License as
16 // published by the Free Software Foundation, either version 3 of the
17 // License, or (at your option) any later version.
18 //
19 // TCPDF is distributed in the hope that it will be useful, but
20 // WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 // See the GNU Lesser General Public License for more details.
23 //
24 // You should have received a copy of the License
25 // along with TCPDF. If not, see
26 // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
27 //
28 // See LICENSE.TXT file for more information.
29 // -------------------------------------------------------------------
30 //
31 // Description :
32 // Static image methods used by the TCPDF class.
33 //
34 //============================================================+
35
36 /**
37 * @file
38 * This is a PHP class that contains static image methods for the TCPDF class.<br>
39 * @package com.tecnick.tcpdf
40 * @author Nicola Asuni
41 * @version 1.0.005
42 */
43
44 /**
45 * @class TCPDF_IMAGES
46 * Static image methods used by the TCPDF class.
47 * @package com.tecnick.tcpdf
48 * @brief PHP class for generating PDF documents without requiring external extensions.
49 * @version 1.0.005
50 * @author Nicola Asuni - [email protected]
51 */
52 class TCPDF_IMAGES {
53
54 /**
55 * Array of hinheritable SVG properties.
56 * @since 5.0.000 (2010-05-02)
57 * @public static
58 *
59 * @var string[]
60 */
61 public static $svginheritprop = array('clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering', 'cursor', 'direction', 'display', 'fill', 'fill-opacity', 'fill-rule', 'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'glyph-orientation-horizontal', 'glyph-orientation-vertical', 'image-rendering', 'kerning', 'letter-spacing', 'marker', 'marker-end', 'marker-mid', 'marker-start', 'pointer-events', 'shape-rendering', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'text-anchor', 'text-rendering', 'visibility', 'word-spacing', 'writing-mode');
62
63 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
64
65 /**
66 * Return the image type given the file name or array returned by getimagesize() function.
67 * @param string $imgfile image file name
68 * @param array $iminfo array of image information returned by getimagesize() function.
69 * @return string image type
70 * @since 4.8.017 (2009-11-27)
71 * @public static
72 */
73 public static function getImageFileType($imgfile, $iminfo=array()) {
74 $type = '';
75 if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
76 $mime = explode('/', $iminfo['mime']);
77 if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
78 $type = strtolower(trim($mime[1]));
79 }
80 }
81 if (empty($type)) {
82 $type = strtolower(trim(pathinfo(parse_url($imgfile, PHP_URL_PATH), PATHINFO_EXTENSION)));
83 }
84 if ($type == 'jpg') {
85 $type = 'jpeg';
86 }
87 return $type;
88 }
89
90 /**
91 * Set the transparency for the given GD image.
92 * @param resource $new_image GD image object
93 * @param resource $image GD image object.
94 * @return resource GD image object $new_image
95 * @since 4.9.016 (2010-04-20)
96 * @public static
97 */
98 public static function setGDImageTransparency($new_image, $image) {
99 // default transparency color (white)
100 $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
101 // transparency index
102 $tid = imagecolortransparent($image);
103 $palletsize = imagecolorstotal($image);
104 if (($tid >= 0) AND ($tid < $palletsize)) {
105 // get the colors for the transparency index
106 $tcol = imagecolorsforindex($image, $tid);
107 }
108 $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
109 imagefill($new_image, 0, 0, $tid);
110 imagecolortransparent($new_image, $tid);
111 return $new_image;
112 }
113
114 /**
115 * Convert the loaded image to a PNG and then return a structure for the PDF creator.
116 * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
117 * @param resource $image Image object.
118 * @param string $tempfile Temporary file name.
119 * return image PNG image object.
120 * @since 4.9.016 (2010-04-20)
121 * @public static
122 */
123 public static function _toPNG($image, $tempfile) {
124 // turn off interlaced mode
125 imageinterlace($image, 0);
126 // create temporary PNG image
127 imagepng($image, $tempfile);
128 // remove image from memory
129 if (PHP_VERSION_ID < 80000) {
130 imagedestroy($image);
131 }
132 // get PNG image data
133 $retvars = self::_parsepng($tempfile);
134 // tidy up by removing temporary image
135 unlink($tempfile);
136 return $retvars;
137 }
138
139 /**
140 * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
141 * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
142 * @param resource $image Image object.
143 * @param int $quality JPEG quality.
144 * @param string $tempfile Temporary file name.
145 * return array|false image JPEG image object.
146 * @public static
147 */
148 public static function _toJPEG($image, $quality, $tempfile) {
149 imagejpeg($image, $tempfile, $quality);
150 if (PHP_VERSION_ID < 80000) {
151 imagedestroy($image);
152 }
153 $retvars = self::_parsejpeg($tempfile);
154 // tidy up by removing temporary image
155 unlink($tempfile);
156 return $retvars;
157 }
158
159 /**
160 * Extract info from a JPEG file without using the GD library.
161 * @param string $file image file to parse
162 * @return array|false structure containing the image data
163 * @public static
164 */
165 public static function _parsejpeg($file) {
166 // check if is a local file
167 if (!@TCPDF_STATIC::file_exists($file)) {
168 return false;
169 }
170 $a = getimagesize($file);
171 if (empty($a)) {
172 //Missing or incorrect image file
173 return false;
174 }
175 if ($a[2] != 2) {
176 // Not a JPEG file
177 return false;
178 }
179 // bits per pixel
180 $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
181 // number of image channels
182 if (!isset($a['channels'])) {
183 $channels = 3;
184 } else {
185 $channels = intval($a['channels']);
186 }
187 // default colour space
188 switch ($channels) {
189 case 1: {
190 $colspace = 'DeviceGray';
191 break;
192 }
193 case 3: {
194 $colspace = 'DeviceRGB';
195 break;
196 }
197 case 4: {
198 $colspace = 'DeviceCMYK';
199 break;
200 }
201 default: {
202 $channels = 3;
203 $colspace = 'DeviceRGB';
204 break;
205 }
206 }
207 // get file content
208 $data = file_get_contents($file);
209 // check for embedded ICC profile
210 $icc = array();
211 $offset = 0;
212 while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
213 // get ICC sequence length
214 $length = (TCPDF_STATIC::_getUSHORT($data, ($pos - 2)) - 16);
215 // marker sequence number
216 $msn = max(1, ord($data[($pos + 12)]));
217 // number of markers (total of APP2 used)
218 $nom = max(1, ord($data[($pos + 13)]));
219 // get sequence segment
220 $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
221 // move forward to next sequence
222 $offset = ($pos + 14 + $length);
223 }
224 // order and compact ICC segments
225 if (count($icc) > 0) {
226 ksort($icc);
227 $icc = implode('', $icc);
228 if ((ord($icc[36]) != 0x61) OR (ord($icc[37]) != 0x63) OR (ord($icc[38]) != 0x73) OR (ord($icc[39]) != 0x70)) {
229 // invalid ICC profile
230 $icc = false;
231 }
232 } else {
233 $icc = false;
234 }
235 return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
236 }
237
238 /**
239 * Extract info from a PNG file without using the GD library.
240 * @param string $file image file to parse
241 * @return array|false structure containing the image data
242 * @public static
243 */
244 public static function _parsepng($file) {
245 $f = @fopen($file, 'rb');
246 if ($f === false) {
247 // Can't open image file
248 return false;
249 }
250 //Check signature
251 if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
252 // Not a PNG file
253 return false;
254 }
255 //Read header chunk
256 fread($f, 4);
257 if (fread($f, 4) != 'IHDR') {
258 //Incorrect PNG file
259 return false;
260 }
261 $w = TCPDF_STATIC::_freadint($f);
262 $h = TCPDF_STATIC::_freadint($f);
263 $bpc = ord(fread($f, 1));
264 $ct = ord(fread($f, 1));
265 if ($ct == 0) {
266 $colspace = 'DeviceGray';
267 } elseif ($ct == 2) {
268 $colspace = 'DeviceRGB';
269 } elseif ($ct == 3) {
270 $colspace = 'Indexed';
271 } else {
272 // alpha channel
273 fclose($f);
274 return 'pngalpha';
275 }
276 if (ord(fread($f, 1)) != 0) {
277 // Unknownn compression method
278 fclose($f);
279 return false;
280 }
281 if (ord(fread($f, 1)) != 0) {
282 // Unknownn filter method
283 fclose($f);
284 return false;
285 }
286 if (ord(fread($f, 1)) != 0) {
287 // Interlacing not supported
288 fclose($f);
289 return false;
290 }
291 fread($f, 4);
292 $channels = ($ct == 2 ? 3 : 1);
293 $parms = '/DecodeParms << /Predictor 15 /Colors '.$channels.' /BitsPerComponent '.$bpc.' /Columns '.$w.' >>';
294 //Scan chunks looking for palette, transparency and image data
295 $pal = '';
296 $trns = '';
297 $data = '';
298 $icc = false;
299 $n = TCPDF_STATIC::_freadint($f);
300 do {
301 $type = fread($f, 4);
302 if ($type == 'PLTE') {
303 // read palette
304 $pal = TCPDF_STATIC::rfread($f, $n);
305 fread($f, 4);
306 } elseif ($type == 'tRNS') {
307 // read transparency info
308 $t = TCPDF_STATIC::rfread($f, $n);
309 if ($ct == 0) { // DeviceGray
310 $trns = array(ord($t[1]));
311 } elseif ($ct == 2) { // DeviceRGB
312 $trns = array(ord($t[1]), ord($t[3]), ord($t[5]));
313 } else { // Indexed
314 if ($n > 0) {
315 $trns = array();
316 for ($i = 0; $i < $n; ++ $i) {
317 $trns[] = ord($t[$i]);
318 }
319 }
320 }
321 fread($f, 4);
322 } elseif ($type == 'IDAT') {
323 // read image data block
324 $data .= TCPDF_STATIC::rfread($f, $n);
325 fread($f, 4);
326 } elseif ($type == 'iCCP') {
327 // skip profile name
328 $len = 0;
329 while ((ord(fread($f, 1)) != 0) AND ($len < 80)) {
330 ++$len;
331 }
332 // get compression method
333 if (ord(fread($f, 1)) != 0) {
334 // Unknownn filter method
335 fclose($f);
336 return false;
337 }
338 // read ICC Color Profile
339 $icc = TCPDF_STATIC::rfread($f, ($n - $len - 2));
340 // decompress profile
341 $icc = gzuncompress($icc);
342 fread($f, 4);
343 } elseif ($type == 'IEND') {
344 break;
345 } else {
346 TCPDF_STATIC::rfread($f, $n + 4);
347 }
348 $n = TCPDF_STATIC::_freadint($f);
349 } while ($n);
350 if (($colspace == 'Indexed') AND (empty($pal))) {
351 // Missing palette
352 fclose($f);
353 return false;
354 }
355 fclose($f);
356 return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
357 }
358
359 } // END OF TCPDF_IMAGES CLASS
360
361 //============================================================+
362 // END OF FILE
363 //============================================================+