| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
class TCPDF_IMAGES { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static function setGDImageTransparency($new_image, $image) { |
| 99 |
|
| 100 |
$tcol = array('red' => 255, 'green' => 255, 'blue' => 255); |
| 101 |
|
| 102 |
$tid = imagecolortransparent($image); |
| 103 |
$palletsize = imagecolorstotal($image); |
| 104 |
if (($tid >= 0) AND ($tid < $palletsize)) { |
| 105 |
|
| 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 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static function _toPNG($image, $tempfile) { |
| 124 |
|
| 125 |
imageinterlace($image, 0); |
| 126 |
|
| 127 |
imagepng($image, $tempfile); |
| 128 |
|
| 129 |
if (PHP_VERSION_ID < 80000) { |
| 130 |
imagedestroy($image); |
| 131 |
} |
| 132 |
|
| 133 |
$retvars = self::_parsepng($tempfile); |
| 134 |
|
| 135 |
unlink($tempfile); |
| 136 |
return $retvars; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 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 |
|
| 155 |
unlink($tempfile); |
| 156 |
return $retvars; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static function _parsejpeg($file) { |
| 166 |
|
| 167 |
if (!@TCPDF_STATIC::file_exists($file)) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
$a = getimagesize($file); |
| 171 |
if (empty($a)) { |
| 172 |
|
| 173 |
return false; |
| 174 |
} |
| 175 |
if ($a[2] != 2) { |
| 176 |
|
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
$bpc = isset($a['bits']) ? intval($a['bits']) : 8; |
| 181 |
|
| 182 |
if (!isset($a['channels'])) { |
| 183 |
$channels = 3; |
| 184 |
} else { |
| 185 |
$channels = intval($a['channels']); |
| 186 |
} |
| 187 |
|
| 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 |
|
| 208 |
$data = file_get_contents($file); |
| 209 |
|
| 210 |
$icc = array(); |
| 211 |
$offset = 0; |
| 212 |
while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) { |
| 213 |
|
| 214 |
$length = (TCPDF_STATIC::_getUSHORT($data, ($pos - 2)) - 16); |
| 215 |
|
| 216 |
$msn = max(1, ord($data[($pos + 12)])); |
| 217 |
|
| 218 |
$nom = max(1, ord($data[($pos + 13)])); |
| 219 |
|
| 220 |
$icc[($msn - 1)] = substr($data, ($pos + 14), $length); |
| 221 |
|
| 222 |
$offset = ($pos + 14 + $length); |
| 223 |
} |
| 224 |
|
| 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 |
|
| 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 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public static function _parsepng($file) { |
| 245 |
$f = @fopen($file, 'rb'); |
| 246 |
if ($f === false) { |
| 247 |
|
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) { |
| 252 |
|
| 253 |
return false; |
| 254 |
} |
| 255 |
|
| 256 |
fread($f, 4); |
| 257 |
if (fread($f, 4) != 'IHDR') { |
| 258 |
|
| 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 |
|
| 273 |
fclose($f); |
| 274 |
return 'pngalpha'; |
| 275 |
} |
| 276 |
if (ord(fread($f, 1)) != 0) { |
| 277 |
|
| 278 |
fclose($f); |
| 279 |
return false; |
| 280 |
} |
| 281 |
if (ord(fread($f, 1)) != 0) { |
| 282 |
|
| 283 |
fclose($f); |
| 284 |
return false; |
| 285 |
} |
| 286 |
if (ord(fread($f, 1)) != 0) { |
| 287 |
|
| 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 |
|
| 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 |
|
| 304 |
$pal = TCPDF_STATIC::rfread($f, $n); |
| 305 |
fread($f, 4); |
| 306 |
} elseif ($type == 'tRNS') { |
| 307 |
|
| 308 |
$t = TCPDF_STATIC::rfread($f, $n); |
| 309 |
if ($ct == 0) { |
| 310 |
$trns = array(ord($t[1])); |
| 311 |
} elseif ($ct == 2) { |
| 312 |
$trns = array(ord($t[1]), ord($t[3]), ord($t[5])); |
| 313 |
} else { |
| 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 |
|
| 324 |
$data .= TCPDF_STATIC::rfread($f, $n); |
| 325 |
fread($f, 4); |
| 326 |
} elseif ($type == 'iCCP') { |
| 327 |
|
| 328 |
$len = 0; |
| 329 |
while ((ord(fread($f, 1)) != 0) AND ($len < 80)) { |
| 330 |
++$len; |
| 331 |
} |
| 332 |
|
| 333 |
if (ord(fread($f, 1)) != 0) { |
| 334 |
|
| 335 |
fclose($f); |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
$icc = TCPDF_STATIC::rfread($f, ($n - $len - 2)); |
| 340 |
|
| 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 |
|
| 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 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|