Total Complexity | 67 |
Total Lines | 405 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like CGIF 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 CGIF, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
929 | class CGIF |
||
930 | { |
||
931 | public $m_gfh; |
||
932 | public $m_lpData; |
||
933 | public $m_img; |
||
934 | public $m_bLoaded; |
||
935 | |||
936 | /////////////////////////////////////////////////////////////////////////// |
||
937 | |||
938 | // CONSTRUCTOR |
||
939 | public function __construct() |
||
940 | { |
||
941 | $this->m_gfh = new CGIFFILEHEADER(); |
||
942 | $this->m_img = new CGIFIMAGE(); |
||
943 | $this->m_lpData = ''; |
||
944 | $this->m_bLoaded = false; |
||
945 | } |
||
946 | |||
947 | /////////////////////////////////////////////////////////////////////////// |
||
948 | |||
949 | /** |
||
950 | * @param $lpszFileName |
||
951 | * @param $iIndex |
||
952 | * @return bool |
||
953 | */ |
||
954 | public function loadFile($lpszFileName, $iIndex) |
||
955 | { |
||
956 | if ($iIndex < 0) { |
||
957 | return false; |
||
958 | } |
||
959 | |||
960 | // READ FILE |
||
961 | if (!($fh = @fopen($lpszFileName, 'rb'))) { |
||
962 | return false; |
||
963 | } |
||
964 | $this->m_lpData = @fread($fh, @filesize($lpszFileName)); |
||
965 | fclose($fh); |
||
966 | |||
967 | // GET FILE HEADER |
||
968 | if (!$this->m_gfh->load($this->m_lpData, $len = 0)) { |
||
969 | return false; |
||
970 | } |
||
971 | $this->m_lpData = substr($this->m_lpData, $len); |
||
972 | |||
973 | do { |
||
974 | if (!$this->m_img->load($this->m_lpData, $imgLen = 0)) { |
||
975 | return false; |
||
976 | } |
||
977 | $this->m_lpData = substr($this->m_lpData, $imgLen); |
||
978 | } while ($iIndex-- > 0); |
||
979 | |||
980 | $this->m_bLoaded = true; |
||
981 | |||
982 | return true; |
||
983 | } |
||
984 | |||
985 | /////////////////////////////////////////////////////////////////////////// |
||
986 | |||
987 | /** |
||
988 | * @param $lpszFileName |
||
989 | * @param $width |
||
990 | * @param $height |
||
991 | * @return bool |
||
992 | */ |
||
993 | public function getSize($lpszFileName, &$width, &$height) |
||
994 | { |
||
995 | if (!($fh = @fopen($lpszFileName, 'rb'))) { |
||
996 | return false; |
||
997 | } |
||
998 | $data = @fread($fh, @filesize($lpszFileName)); |
||
999 | @fclose($fh); |
||
1000 | |||
1001 | $gfh = new CGIFFILEHEADER(); |
||
1002 | if (!$gfh->load($data, $len = 0)) { |
||
1003 | return false; |
||
1004 | } |
||
1005 | |||
1006 | $width = $gfh->m_nWidth; |
||
1007 | $height = $gfh->m_nHeight; |
||
1008 | |||
1009 | return true; |
||
1010 | } |
||
1011 | |||
1012 | /////////////////////////////////////////////////////////////////////////// |
||
1013 | |||
1014 | /** |
||
1015 | * @param $bgColor |
||
1016 | * @return string |
||
1017 | */ |
||
1018 | public function getBmp($bgColor) |
||
1019 | { |
||
1020 | $out = ''; |
||
1021 | |||
1022 | if (!$this->m_bLoaded) { |
||
1023 | return false; |
||
1024 | } |
||
1025 | |||
1026 | // PREPARE COLOR TABLE (RGBQUADs) |
||
1027 | if ($this->m_img->m_gih->m_bLocalClr) { |
||
1028 | $nColors = $this->m_img->m_gih->m_nTableSize; |
||
1029 | $rgbq = $this->m_img->m_gih->m_colorTable->toRGBQuad(); |
||
1030 | if (-1 != $bgColor) { |
||
1031 | $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor); |
||
1032 | } |
||
1033 | } elseif ($this->m_gfh->m_bGlobalClr) { |
||
1034 | $nColors = $this->m_gfh->m_nTableSize; |
||
1035 | $rgbq = $this->m_gfh->m_colorTable->toRGBQuad(); |
||
1036 | if (-1 != $bgColor) { |
||
1037 | $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor); |
||
1038 | } |
||
1039 | } else { |
||
1040 | $nColors = 0; |
||
1041 | $rgbq = ''; |
||
1042 | $bgColor = -1; |
||
1043 | } |
||
1044 | |||
1045 | // PREPARE BITMAP BITS |
||
1046 | $data = $this->m_img->m_data; |
||
1047 | $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth; |
||
1048 | $bmp = ''; |
||
1049 | $nPad = ($this->m_gfh->m_nWidth % 4) ? 4 - ($this->m_gfh->m_nWidth % 4) : 0; |
||
1050 | for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) { |
||
1051 | for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) { |
||
1052 | if (($x >= $this->m_img->m_gih->m_nLeft) |
||
1053 | && ($y >= $this->m_img->m_gih->m_nTop) |
||
1054 | && ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) |
||
1055 | && ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) { |
||
1056 | // PART OF IMAGE |
||
1057 | if (@$this->m_img->m_bTrans && (ord($data{$nPxl}) == $this->m_img->m_nTrans)) { |
||
1058 | // TRANSPARENT -> BACKGROUND |
||
1059 | if (-1 == $bgColor) { |
||
1060 | $bmp .= chr($this->m_gfh->m_nBgColor); |
||
1061 | } else { |
||
1062 | $bmp .= chr($bgColor); |
||
1063 | } |
||
1064 | } else { |
||
1065 | $bmp .= $data{$nPxl}; |
||
1066 | } |
||
1067 | } else { |
||
1068 | // BACKGROUND |
||
1069 | if (-1 == $bgColor) { |
||
1070 | $bmp .= chr($this->m_gfh->m_nBgColor); |
||
1071 | } else { |
||
1072 | $bmp .= chr($bgColor); |
||
1073 | } |
||
1074 | } |
||
1075 | } |
||
1076 | $nPxl -= $this->m_gfh->m_nWidth << 1; |
||
1077 | |||
1078 | // ADD PADDING |
||
1079 | for ($x = 0; $x < $nPad; $x++) { |
||
1080 | $bmp .= "\x00"; |
||
1081 | } |
||
1082 | } |
||
1083 | |||
1084 | // BITMAPFILEHEADER |
||
1085 | $out .= 'BM'; |
||
1086 | $out .= $this->dword(14 + 40 + ($nColors << 2) + strlen($bmp)); |
||
1087 | $out .= "\x00\x00"; |
||
1088 | $out .= "\x00\x00"; |
||
1089 | $out .= $this->dword(14 + 40 + ($nColors << 2)); |
||
1090 | |||
1091 | // BITMAPINFOHEADER |
||
1092 | $out .= $this->dword(40); |
||
1093 | $out .= $this->dword($this->m_gfh->m_nWidth); |
||
1094 | $out .= $this->dword($this->m_gfh->m_nHeight); |
||
1095 | $out .= "\x01\x00"; |
||
1096 | $out .= "\x08\x00"; |
||
1097 | $out .= "\x00\x00\x00\x00"; |
||
1098 | $out .= "\x00\x00\x00\x00"; |
||
1099 | $out .= "\x12\x0B\x00\x00"; |
||
1100 | $out .= "\x12\x0B\x00\x00"; |
||
1101 | $out .= $this->dword($nColors % 256); |
||
1102 | $out .= "\x00\x00\x00\x00"; |
||
1103 | |||
1104 | // COLOR TABLE |
||
1105 | if ($nColors > 0) { |
||
1106 | $out .= $rgbq; |
||
1107 | } |
||
1108 | |||
1109 | // DATA |
||
1110 | $out .= $bmp; |
||
1111 | |||
1112 | return $out; |
||
1113 | } |
||
1114 | |||
1115 | /////////////////////////////////////////////////////////////////////////// |
||
1116 | |||
1117 | /** |
||
1118 | * @param $bgColor |
||
1119 | * @return string |
||
1120 | */ |
||
1121 | public function getPng($bgColor) |
||
1122 | { |
||
1123 | $out = ''; |
||
1124 | |||
1125 | if (!$this->m_bLoaded) { |
||
1126 | return false; |
||
1127 | } |
||
1128 | |||
1129 | // PREPARE COLOR TABLE (RGBQUADs) |
||
1130 | if ($this->m_img->m_gih->m_bLocalClr) { |
||
1131 | $nColors = $this->m_img->m_gih->m_nTableSize; |
||
1132 | $pal = $this->m_img->m_gih->m_colorTable->toString(); |
||
1133 | if (-1 != $bgColor) { |
||
1134 | $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor); |
||
1135 | } |
||
1136 | } elseif ($this->m_gfh->m_bGlobalClr) { |
||
1137 | $nColors = $this->m_gfh->m_nTableSize; |
||
1138 | $pal = $this->m_gfh->m_colorTable->toString(); |
||
1139 | if (-1 != $bgColor) { |
||
1140 | $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor); |
||
1141 | } |
||
1142 | } else { |
||
1143 | $nColors = 0; |
||
1144 | $pal = ''; |
||
1145 | $bgColor = -1; |
||
1146 | } |
||
1147 | |||
1148 | // PREPARE BITMAP BITS |
||
1149 | $data = $this->m_img->m_data; |
||
1150 | $nPxl = 0; |
||
1151 | $bmp = ''; |
||
1152 | for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) { |
||
1153 | $bmp .= "\x00"; |
||
1154 | for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) { |
||
1155 | if (($x >= $this->m_img->m_gih->m_nLeft) |
||
1156 | && ($y >= $this->m_img->m_gih->m_nTop) |
||
1157 | && ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) |
||
1158 | && ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) { |
||
1159 | // PART OF IMAGE |
||
1160 | $bmp .= $data{$nPxl}; |
||
1161 | } else { |
||
1162 | // BACKGROUND |
||
1163 | if (-1 == $bgColor) { |
||
1164 | $bmp .= chr($this->m_gfh->m_nBgColor); |
||
1165 | } else { |
||
1166 | $bmp .= chr($bgColor); |
||
1167 | } |
||
1168 | } |
||
1169 | } |
||
1170 | } |
||
1171 | $bmp = gzcompress($bmp, 9); |
||
1172 | |||
1173 | /////////////////////////////////////////////////////////////////////// |
||
1174 | // SIGNATURE |
||
1175 | $out .= "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"; |
||
1176 | /////////////////////////////////////////////////////////////////////// |
||
1177 | // HEADER |
||
1178 | $out .= "\x00\x00\x00\x0D"; |
||
1179 | $tmp = 'IHDR'; |
||
1180 | $tmp .= $this->ndword($this->m_gfh->m_nWidth); |
||
1181 | $tmp .= $this->ndword($this->m_gfh->m_nHeight); |
||
1182 | $tmp .= "\x08\x03\x00\x00\x00"; |
||
1183 | $out .= $tmp; |
||
1184 | $out .= $this->ndword(crc32($tmp)); |
||
1185 | /////////////////////////////////////////////////////////////////////// |
||
1186 | // PALETTE |
||
1187 | if ($nColors > 0) { |
||
1188 | $out .= $this->ndword($nColors * 3); |
||
1189 | $tmp = 'PLTE'; |
||
1190 | $tmp .= $pal; |
||
1191 | $out .= $tmp; |
||
1192 | $out .= $this->ndword(crc32($tmp)); |
||
1193 | } |
||
1194 | /////////////////////////////////////////////////////////////////////// |
||
1195 | // TRANSPARENCY |
||
1196 | if (@$this->m_img->m_bTrans && ($nColors > 0)) { |
||
1197 | $out .= $this->ndword($nColors); |
||
1198 | $tmp = 'tRNS'; |
||
1199 | for ($i = 0; $i < $nColors; $i++) { |
||
1200 | $tmp .= ($i == $this->m_img->m_nTrans) ? "\x00" : "\xFF"; |
||
1201 | } |
||
1202 | $out .= $tmp; |
||
1203 | $out .= $this->ndword(crc32($tmp)); |
||
1204 | } |
||
1205 | /////////////////////////////////////////////////////////////////////// |
||
1206 | // DATA BITS |
||
1207 | $out .= $this->ndword(strlen($bmp)); |
||
1208 | $tmp = 'IDAT'; |
||
1209 | $tmp .= $bmp; |
||
1210 | $out .= $tmp; |
||
1211 | $out .= $this->ndword(crc32($tmp)); |
||
1212 | /////////////////////////////////////////////////////////////////////// |
||
1213 | // END OF FILE |
||
1214 | $out .= "\x00\x00\x00\x00IEND\xAE\x42\x60\x82"; |
||
1215 | |||
1216 | return $out; |
||
1217 | } |
||
1218 | |||
1219 | /////////////////////////////////////////////////////////////////////////// |
||
1220 | |||
1221 | // Added by James Heinrich <[email protected]> - January 5, 2003 |
||
1222 | |||
1223 | // Takes raw image data and plots it pixel-by-pixel on a new GD image and returns that |
||
1224 | // It's extremely slow, but the only solution when imagecreatefromstring() fails |
||
1225 | /** |
||
1226 | * @return bool|resource |
||
1227 | */ |
||
1228 | public function getGD_PixelPlotterVersion() |
||
1229 | { |
||
1230 | if (!$this->m_bLoaded) { |
||
1231 | return false; |
||
1232 | } |
||
1233 | |||
1234 | // PREPARE COLOR TABLE (RGBQUADs) |
||
1235 | if ($this->m_img->m_gih->m_bLocalClr) { |
||
1236 | $pal = $this->m_img->m_gih->m_colorTable->toString(); |
||
1237 | } elseif ($this->m_gfh->m_bGlobalClr) { |
||
1238 | $pal = $this->m_gfh->m_colorTable->toString(); |
||
1239 | } else { |
||
1240 | die('No color table available in getGD_PixelPlotterVersion()'); |
||
1241 | } |
||
1242 | |||
1243 | $PlottingIMG = imagecreate($this->m_gfh->m_nWidth, $this->m_gfh->m_nHeight); |
||
1244 | $NumColorsInPal = floor(strlen($pal) / 3); |
||
1245 | $ThisImageColor = []; |
||
1246 | for ($i = 0; $i < $NumColorsInPal; $i++) { |
||
1247 | $ThisImageColor[$i] = imagecolorallocate($PlottingIMG, ord($pal{($i * 3) + 0}), ord($pal{($i * 3) + 1}), ord($pal{($i * 3) + 2})); |
||
1248 | } |
||
1249 | |||
1250 | // PREPARE BITMAP BITS |
||
1251 | $data = $this->m_img->m_data; |
||
1252 | $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth; |
||
1253 | for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) { |
||
1254 | if (!phpthumb_functions::FunctionIsDisabled('set_time_limit')) { |
||
1255 | set_time_limit(30); |
||
1256 | } |
||
1257 | for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) { |
||
1258 | if (($x >= $this->m_img->m_gih->m_nLeft) |
||
1259 | && ($y >= $this->m_img->m_gih->m_nTop) |
||
1260 | && ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) |
||
1261 | && ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) { |
||
1262 | // PART OF IMAGE |
||
1263 | if (@$this->m_img->m_bTrans && (ord($data{$nPxl}) == $this->m_img->m_nTrans)) { |
||
1264 | imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]); |
||
1265 | } else { |
||
1266 | imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[ord($data{$nPxl})]); |
||
1267 | } |
||
1268 | } else { |
||
1269 | // BACKGROUND |
||
1270 | imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]); |
||
1271 | } |
||
1272 | } |
||
1273 | $nPxl -= $this->m_gfh->m_nWidth << 1; |
||
1274 | } |
||
1275 | |||
1276 | return $PlottingIMG; |
||
1277 | } |
||
1278 | |||
1279 | /////////////////////////////////////////////////////////////////////////// |
||
1280 | |||
1281 | /** |
||
1282 | * @param $val |
||
1283 | * @return string |
||
1284 | */ |
||
1285 | public function dword($val) |
||
1286 | { |
||
1287 | $val = (int)$val; |
||
1288 | |||
1289 | return chr($val & 0xFF) . chr(($val & 0xFF00) >> 8) . chr(($val & 0xFF0000) >> 16) . chr(($val & 0xFF000000) >> 24); |
||
1290 | } |
||
1291 | |||
1292 | /////////////////////////////////////////////////////////////////////////// |
||
1293 | |||
1294 | /** |
||
1295 | * @param $val |
||
1296 | * @return string |
||
1297 | */ |
||
1298 | public function ndword($val) |
||
1303 | } |
||
1304 | |||
1305 | /////////////////////////////////////////////////////////////////////////// |
||
1306 | |||
1307 | public function width() |
||
1308 | { |
||
1309 | return $this->m_gfh->m_nWidth; |
||
1310 | } |
||
1311 | |||
1312 | /////////////////////////////////////////////////////////////////////////// |
||
1313 | |||
1314 | public function height() |
||
1315 | { |
||
1316 | return $this->m_gfh->m_nHeight; |
||
1317 | } |
||
1318 | |||
1319 | /////////////////////////////////////////////////////////////////////////// |
||
1320 | |||
1321 | public function comment() |
||
1322 | { |
||
1323 | return $this->m_img->m_lpComm; |
||
1324 | } |
||
1325 | |||
1326 | /////////////////////////////////////////////////////////////////////////// |
||
1327 | |||
1328 | /** |
||
1329 | * @return bool |
||
1330 | */ |
||
1331 | public function loaded() |
||
1334 | } |
||
1335 | } |
||
1336 |