source: trunk/client/inc/hpdf5/tecnickcom/tcpdf/include/tcpdf_images.php

Last change on this file was 347, checked in by roby, 4 years ago

Aggiornamento per compatibilità con php7.4

File size: 11.0 KB
RevLine 
[347]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 - info@tecnick.com
8// License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
9// -------------------------------------------------------------------
10// Copyright (C) 2002-2014 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 - info@tecnick.com
51 */
52class TCPDF_IMAGES {
53
54 /**
55 * Array of hinheritable SVG properties.
56 * @since 5.0.000 (2010-05-02)
57 * @public static
58 */
59 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');
60
61// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62
63 /**
64 * Return the image type given the file name or array returned by getimagesize() function.
65 * @param $imgfile (string) image file name
66 * @param $iminfo (array) array of image information returned by getimagesize() function.
67 * @return string image type
68 * @since 4.8.017 (2009-11-27)
69 * @public static
70 */
71 public static function getImageFileType($imgfile, $iminfo=array()) {
72 $type = '';
73 if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
74 $mime = explode('/', $iminfo['mime']);
75 if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
76 $type = strtolower(trim($mime[1]));
77 }
78 }
79 if (empty($type)) {
80 $type = strtolower(trim(pathinfo(parse_url($imgfile, PHP_URL_PATH), PATHINFO_EXTENSION)));
81 }
82 if ($type == 'jpg') {
83 $type = 'jpeg';
84 }
85 return $type;
86 }
87
88 /**
89 * Set the transparency for the given GD image.
90 * @param $new_image (image) GD image object
91 * @param $image (image) GD image object.
92 * return GD image object.
93 * @since 4.9.016 (2010-04-20)
94 * @public static
95 */
96 public static function setGDImageTransparency($new_image, $image) {
97 // default transparency color (white)
98 $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
99 // transparency index
100 $tid = imagecolortransparent($image);
101 $palletsize = imagecolorstotal($image);
102 if (($tid >= 0) AND ($tid < $palletsize)) {
103 // get the colors for the transparency index
104 $tcol = imagecolorsforindex($image, $tid);
105 }
106 $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
107 imagefill($new_image, 0, 0, $tid);
108 imagecolortransparent($new_image, $tid);
109 return $new_image;
110 }
111
112 /**
113 * Convert the loaded image to a PNG and then return a structure for the PDF creator.
114 * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
115 * @param $image (image) Image object.
116 * @param $tempfile (string) Temporary file name.
117 * return image PNG image object.
118 * @since 4.9.016 (2010-04-20)
119 * @public static
120 */
121 public static function _toPNG($image, $tempfile) {
122 // turn off interlaced mode
123 imageinterlace($image, 0);
124 // create temporary PNG image
125 imagepng($image, $tempfile);
126 // remove image from memory
127 imagedestroy($image);
128 // get PNG image data
129 $retvars = self::_parsepng($tempfile);
130 // tidy up by removing temporary image
131 unlink($tempfile);
132 return $retvars;
133 }
134
135 /**
136 * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
137 * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
138 * @param $image (image) Image object.
139 * @param $quality (int) JPEG quality.
140 * @param $tempfile (string) Temporary file name.
141 * return image JPEG image object.
142 * @public static
143 */
144 public static function _toJPEG($image, $quality, $tempfile) {
145 imagejpeg($image, $tempfile, $quality);
146 imagedestroy($image);
147 $retvars = self::_parsejpeg($tempfile);
148 // tidy up by removing temporary image
149 unlink($tempfile);
150 return $retvars;
151 }
152
153 /**
154 * Extract info from a JPEG file without using the GD library.
155 * @param $file (string) image file to parse
156 * @return array structure containing the image data
157 * @public static
158 */
159 public static function _parsejpeg($file) {
160 // check if is a local file
161 if (!@TCPDF_STATIC::file_exists($file)) {
162 return false;
163 }
164 $a = getimagesize($file);
165 if (empty($a)) {
166 //Missing or incorrect image file
167 return false;
168 }
169 if ($a[2] != 2) {
170 // Not a JPEG file
171 return false;
172 }
173 // bits per pixel
174 $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
175 // number of image channels
176 if (!isset($a['channels'])) {
177 $channels = 3;
178 } else {
179 $channels = intval($a['channels']);
180 }
181 // default colour space
182 switch ($channels) {
183 case 1: {
184 $colspace = 'DeviceGray';
185 break;
186 }
187 case 3: {
188 $colspace = 'DeviceRGB';
189 break;
190 }
191 case 4: {
192 $colspace = 'DeviceCMYK';
193 break;
194 }
195 default: {
196 $channels = 3;
197 $colspace = 'DeviceRGB';
198 break;
199 }
200 }
201 // get file content
202 $data = file_get_contents($file);
203 // check for embedded ICC profile
204 $icc = array();
205 $offset = 0;
206 while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
207 // get ICC sequence length
208 $length = (TCPDF_STATIC::_getUSHORT($data, ($pos - 2)) - 16);
209 // marker sequence number
210 $msn = max(1, ord($data[($pos + 12)]));
211 // number of markers (total of APP2 used)
212 $nom = max(1, ord($data[($pos + 13)]));
213 // get sequence segment
214 $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
215 // move forward to next sequence
216 $offset = ($pos + 14 + $length);
217 }
218 // order and compact ICC segments
219 if (count($icc) > 0) {
220 ksort($icc);
221 $icc = implode('', $icc);
222 if ((ord($icc[36]) != 0x61) OR (ord($icc[37]) != 0x63) OR (ord($icc[38]) != 0x73) OR (ord($icc[39]) != 0x70)) {
223 // invalid ICC profile
224 $icc = false;
225 }
226 } else {
227 $icc = false;
228 }
229 return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
230 }
231
232 /**
233 * Extract info from a PNG file without using the GD library.
234 * @param $file (string) image file to parse
235 * @return array structure containing the image data
236 * @public static
237 */
238 public static function _parsepng($file) {
239 $f = @fopen($file, 'rb');
240 if ($f === false) {
241 // Can't open image file
242 return false;
243 }
244 //Check signature
245 if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
246 // Not a PNG file
247 return false;
248 }
249 //Read header chunk
250 fread($f, 4);
251 if (fread($f, 4) != 'IHDR') {
252 //Incorrect PNG file
253 return false;
254 }
255 $w = TCPDF_STATIC::_freadint($f);
256 $h = TCPDF_STATIC::_freadint($f);
257 $bpc = ord(fread($f, 1));
258 $ct = ord(fread($f, 1));
259 if ($ct == 0) {
260 $colspace = 'DeviceGray';
261 } elseif ($ct == 2) {
262 $colspace = 'DeviceRGB';
263 } elseif ($ct == 3) {
264 $colspace = 'Indexed';
265 } else {
266 // alpha channel
267 fclose($f);
268 return 'pngalpha';
269 }
270 if (ord(fread($f, 1)) != 0) {
271 // Unknown compression method
272 fclose($f);
273 return false;
274 }
275 if (ord(fread($f, 1)) != 0) {
276 // Unknown filter method
277 fclose($f);
278 return false;
279 }
280 if (ord(fread($f, 1)) != 0) {
281 // Interlacing not supported
282 fclose($f);
283 return false;
284 }
285 fread($f, 4);
286 $channels = ($ct == 2 ? 3 : 1);
287 $parms = '/DecodeParms << /Predictor 15 /Colors '.$channels.' /BitsPerComponent '.$bpc.' /Columns '.$w.' >>';
288 //Scan chunks looking for palette, transparency and image data
289 $pal = '';
290 $trns = '';
291 $data = '';
292 $icc = false;
293 $n = TCPDF_STATIC::_freadint($f);
294 do {
295 $type = fread($f, 4);
296 if ($type == 'PLTE') {
297 // read palette
298 $pal = TCPDF_STATIC::rfread($f, $n);
299 fread($f, 4);
300 } elseif ($type == 'tRNS') {
301 // read transparency info
302 $t = TCPDF_STATIC::rfread($f, $n);
303 if ($ct == 0) { // DeviceGray
304 $trns = array(ord($t[1]));
305 } elseif ($ct == 2) { // DeviceRGB
306 $trns = array(ord($t[1]), ord($t[3]), ord($t[5]));
307 } else { // Indexed
308 if ($n > 0) {
309 $trns = array();
310 for ($i = 0; $i < $n; ++ $i) {
311 $trns[] = ord($t[$i]);
312 }
313 }
314 }
315 fread($f, 4);
316 } elseif ($type == 'IDAT') {
317 // read image data block
318 $data .= TCPDF_STATIC::rfread($f, $n);
319 fread($f, 4);
320 } elseif ($type == 'iCCP') {
321 // skip profile name
322 $len = 0;
323 while ((ord(fread($f, 1)) != 0) AND ($len < 80)) {
324 ++$len;
325 }
326 // get compression method
327 if (ord(fread($f, 1)) != 0) {
328 // Unknown filter method
329 fclose($f);
330 return false;
331 }
332 // read ICC Color Profile
333 $icc = TCPDF_STATIC::rfread($f, ($n - $len - 2));
334 // decompress profile
335 $icc = gzuncompress($icc);
336 fread($f, 4);
337 } elseif ($type == 'IEND') {
338 break;
339 } else {
340 TCPDF_STATIC::rfread($f, $n + 4);
341 }
342 $n = TCPDF_STATIC::_freadint($f);
343 } while ($n);
344 if (($colspace == 'Indexed') AND (empty($pal))) {
345 // Missing palette
346 fclose($f);
347 return false;
348 }
349 fclose($f);
350 return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
351 }
352
353} // END OF TCPDF_IMAGES CLASS
354
355//============================================================+
356// END OF FILE
357//============================================================+
Note: See TracBrowser for help on using the repository browser.