| Total Complexity | 54 |
| Total Lines | 301 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TCPDF_IMAGES often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TCPDF_IMAGES, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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) { |
||
| 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) { |
||
| 135 | } |
||
| 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) { |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Extract info from a JPEG file without using the GD library. |
||
| 157 | * @param string $file image file to parse |
||
| 158 | * @return array|false structure containing the image data |
||
| 159 | * @public static |
||
| 160 | */ |
||
| 161 | public static function _parsejpeg($file) { |
||
| 162 | // check if is a local file |
||
| 163 | if (!@TCPDF_STATIC::file_exists($file)) { |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | $a = getimagesize($file); |
||
| 167 | if (empty($a)) { |
||
| 168 | //Missing or incorrect image file |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | if ($a[2] != 2) { |
||
| 172 | // Not a JPEG file |
||
| 173 | return false; |
||
| 174 | } |
||
| 175 | // bits per pixel |
||
| 176 | $bpc = isset($a['bits']) ? intval($a['bits']) : 8; |
||
| 177 | // number of image channels |
||
| 178 | if (!isset($a['channels'])) { |
||
| 179 | $channels = 3; |
||
| 180 | } else { |
||
| 181 | $channels = intval($a['channels']); |
||
| 182 | } |
||
| 183 | // default colour space |
||
| 184 | switch ($channels) { |
||
| 185 | case 1: { |
||
| 186 | $colspace = 'DeviceGray'; |
||
| 187 | break; |
||
| 188 | } |
||
| 189 | case 3: { |
||
| 190 | $colspace = 'DeviceRGB'; |
||
| 191 | break; |
||
| 192 | } |
||
| 193 | case 4: { |
||
| 194 | $colspace = 'DeviceCMYK'; |
||
| 195 | break; |
||
| 196 | } |
||
| 197 | default: { |
||
| 198 | $channels = 3; |
||
| 199 | $colspace = 'DeviceRGB'; |
||
| 200 | break; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | // get file content |
||
| 204 | $data = file_get_contents($file); |
||
| 205 | // check for embedded ICC profile |
||
| 206 | $icc = array(); |
||
| 207 | $offset = 0; |
||
| 208 | while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) { |
||
| 209 | // get ICC sequence length |
||
| 210 | $length = (TCPDF_STATIC::_getUSHORT($data, ($pos - 2)) - 16); |
||
| 211 | // marker sequence number |
||
| 212 | $msn = max(1, ord($data[($pos + 12)])); |
||
| 213 | // number of markers (total of APP2 used) |
||
| 214 | $nom = max(1, ord($data[($pos + 13)])); |
||
| 215 | // get sequence segment |
||
| 216 | $icc[($msn - 1)] = substr($data, ($pos + 14), $length); |
||
| 217 | // move forward to next sequence |
||
| 218 | $offset = ($pos + 14 + $length); |
||
| 219 | } |
||
| 220 | // order and compact ICC segments |
||
| 221 | if (count($icc) > 0) { |
||
| 222 | ksort($icc); |
||
| 223 | $icc = implode('', $icc); |
||
| 224 | if ((ord($icc[36]) != 0x61) OR (ord($icc[37]) != 0x63) OR (ord($icc[38]) != 0x73) OR (ord($icc[39]) != 0x70)) { |
||
| 225 | // invalid ICC profile |
||
| 226 | $icc = false; |
||
| 227 | } |
||
| 228 | } else { |
||
| 229 | $icc = false; |
||
| 230 | } |
||
| 231 | return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Extract info from a PNG file without using the GD library. |
||
| 236 | * @param string $file image file to parse |
||
| 237 | * @return array|false structure containing the image data |
||
| 238 | * @public static |
||
| 239 | */ |
||
| 240 | public static function _parsepng($file) { |
||
| 353 | } |
||
| 354 | |||
| 355 | } // END OF TCPDF_IMAGES CLASS |
||
| 360 |