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