Total Complexity | 67 |
Total Lines | 377 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
797 | class CGIF |
||
798 | { |
||
799 | public $m_gfh; |
||
800 | public $m_lpData; |
||
801 | public $m_img; |
||
802 | public $m_bLoaded; |
||
803 | |||
804 | /////////////////////////////////////////////////////////////////////////// |
||
805 | |||
806 | // CONSTRUCTOR |
||
807 | public function __construct() |
||
808 | { |
||
809 | $this->m_gfh = new CGIFFILEHEADER(); |
||
810 | $this->m_img = new CGIFIMAGE(); |
||
811 | $this->m_lpData = ''; |
||
812 | $this->m_bLoaded = false; |
||
813 | } |
||
814 | |||
815 | /////////////////////////////////////////////////////////////////////////// |
||
816 | |||
817 | public function loadFile($lpszFileName, $iIndex) |
||
818 | { |
||
819 | if ($iIndex < 0) { |
||
820 | return false; |
||
821 | } |
||
822 | |||
823 | // READ FILE |
||
824 | if (!($fh = @fopen($lpszFileName, 'rb'))) { |
||
825 | return false; |
||
826 | } |
||
827 | $this->m_lpData = @fread($fh, @filesize($lpszFileName)); |
||
828 | fclose($fh); |
||
829 | |||
830 | // GET FILE HEADER |
||
831 | if (!$this->m_gfh->load($this->m_lpData, $len = 0)) { |
||
832 | return false; |
||
833 | } |
||
834 | $this->m_lpData = substr($this->m_lpData, $len); |
||
835 | |||
836 | do { |
||
837 | if (!$this->m_img->load($this->m_lpData, $imgLen = 0)) { |
||
838 | return false; |
||
839 | } |
||
840 | $this->m_lpData = substr($this->m_lpData, $imgLen); |
||
841 | } |
||
842 | while ($iIndex-- > 0); |
||
843 | |||
844 | $this->m_bLoaded = true; |
||
845 | return true; |
||
846 | } |
||
847 | |||
848 | /////////////////////////////////////////////////////////////////////////// |
||
849 | |||
850 | public function getSize($lpszFileName, &$width, &$height) |
||
851 | { |
||
852 | if (!($fh = @fopen($lpszFileName, 'rb'))) { |
||
853 | return false; |
||
854 | } |
||
855 | $data = @fread($fh, @filesize($lpszFileName)); |
||
856 | @fclose($fh); |
||
857 | |||
858 | $gfh = new CGIFFILEHEADER(); |
||
859 | if (!$gfh->load($data, $len = 0)) { |
||
860 | return false; |
||
861 | } |
||
862 | |||
863 | $width = $gfh->m_nWidth; |
||
864 | $height = $gfh->m_nHeight; |
||
865 | return true; |
||
866 | } |
||
867 | |||
868 | /////////////////////////////////////////////////////////////////////////// |
||
869 | |||
870 | public function getBmp($bgColor) |
||
871 | { |
||
872 | $out = ''; |
||
873 | |||
874 | if (!$this->m_bLoaded) { |
||
875 | return false; |
||
876 | } |
||
877 | |||
878 | // PREPARE COLOR TABLE (RGBQUADs) |
||
879 | if ($this->m_img->m_gih->m_bLocalClr) { |
||
880 | $nColors = $this->m_img->m_gih->m_nTableSize; |
||
881 | $rgbq = $this->m_img->m_gih->m_colorTable->toRGBQuad(); |
||
882 | if ($bgColor != -1) { |
||
883 | $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor); |
||
884 | } |
||
885 | } elseif ($this->m_gfh->m_bGlobalClr) { |
||
886 | $nColors = $this->m_gfh->m_nTableSize; |
||
887 | $rgbq = $this->m_gfh->m_colorTable->toRGBQuad(); |
||
888 | if ($bgColor != -1) { |
||
889 | $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor); |
||
890 | } |
||
891 | } else { |
||
892 | $nColors = 0; |
||
893 | $rgbq = ''; |
||
894 | $bgColor = -1; |
||
895 | } |
||
896 | |||
897 | // PREPARE BITMAP BITS |
||
898 | $data = $this->m_img->m_data; |
||
899 | $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth; |
||
900 | $bmp = ''; |
||
901 | $nPad = ($this->m_gfh->m_nWidth % 4) ? 4 - ($this->m_gfh->m_nWidth % 4) : 0; |
||
902 | for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) { |
||
903 | for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) { |
||
904 | if ( |
||
905 | ($x >= $this->m_img->m_gih->m_nLeft) && |
||
906 | ($y >= $this->m_img->m_gih->m_nTop) && |
||
907 | ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) && |
||
908 | ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) { |
||
909 | // PART OF IMAGE |
||
910 | if (@$this->m_img->m_bTrans && (ord($data[$nPxl]) == $this->m_img->m_nTrans)) { |
||
911 | // TRANSPARENT -> BACKGROUND |
||
912 | if ($bgColor == -1) { |
||
913 | $bmp .= chr($this->m_gfh->m_nBgColor); |
||
914 | } else { |
||
915 | $bmp .= chr($bgColor); |
||
916 | } |
||
917 | } else { |
||
918 | $bmp .= $data[$nPxl]; |
||
919 | } |
||
920 | } else { |
||
921 | // BACKGROUND |
||
922 | if ($bgColor == -1) { |
||
923 | $bmp .= chr($this->m_gfh->m_nBgColor); |
||
924 | } else { |
||
925 | $bmp .= chr($bgColor); |
||
926 | } |
||
927 | } |
||
928 | } |
||
929 | $nPxl -= $this->m_gfh->m_nWidth << 1; |
||
930 | |||
931 | // ADD PADDING |
||
932 | for ($x = 0; $x < $nPad; $x++) { |
||
933 | $bmp .= "\x00"; |
||
934 | } |
||
935 | } |
||
936 | |||
937 | // BITMAPFILEHEADER |
||
938 | $out .= 'BM'; |
||
939 | $out .= $this->dword(14 + 40 + ($nColors << 2) + strlen($bmp)); |
||
940 | $out .= "\x00\x00"; |
||
941 | $out .= "\x00\x00"; |
||
942 | $out .= $this->dword(14 + 40 + ($nColors << 2)); |
||
943 | |||
944 | // BITMAPINFOHEADER |
||
945 | $out .= $this->dword(40); |
||
946 | $out .= $this->dword($this->m_gfh->m_nWidth); |
||
947 | $out .= $this->dword($this->m_gfh->m_nHeight); |
||
948 | $out .= "\x01\x00"; |
||
949 | $out .= "\x08\x00"; |
||
950 | $out .= "\x00\x00\x00\x00"; |
||
951 | $out .= "\x00\x00\x00\x00"; |
||
952 | $out .= "\x12\x0B\x00\x00"; |
||
953 | $out .= "\x12\x0B\x00\x00"; |
||
954 | $out .= $this->dword($nColors % 256); |
||
955 | $out .= "\x00\x00\x00\x00"; |
||
956 | |||
957 | // COLOR TABLE |
||
958 | if ($nColors > 0) { |
||
959 | $out .= $rgbq; |
||
960 | } |
||
961 | |||
962 | // DATA |
||
963 | $out .= $bmp; |
||
964 | |||
965 | return $out; |
||
966 | } |
||
967 | |||
968 | /////////////////////////////////////////////////////////////////////////// |
||
969 | |||
970 | public function getPng($bgColor) |
||
971 | { |
||
972 | $out = ''; |
||
973 | |||
974 | if (!$this->m_bLoaded) { |
||
975 | return false; |
||
976 | } |
||
977 | |||
978 | // PREPARE COLOR TABLE (RGBQUADs) |
||
979 | if ($this->m_img->m_gih->m_bLocalClr) { |
||
980 | $nColors = $this->m_img->m_gih->m_nTableSize; |
||
981 | $pal = $this->m_img->m_gih->m_colorTable->toString(); |
||
982 | if ($bgColor != -1) { |
||
983 | $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor); |
||
984 | } |
||
985 | } elseif ($this->m_gfh->m_bGlobalClr) { |
||
986 | $nColors = $this->m_gfh->m_nTableSize; |
||
987 | $pal = $this->m_gfh->m_colorTable->toString(); |
||
988 | if ($bgColor != -1) { |
||
989 | $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor); |
||
990 | } |
||
991 | } else { |
||
992 | $nColors = 0; |
||
993 | $pal = ''; |
||
994 | $bgColor = -1; |
||
995 | } |
||
996 | |||
997 | // PREPARE BITMAP BITS |
||
998 | $data = $this->m_img->m_data; |
||
999 | $nPxl = 0; |
||
1000 | $bmp = ''; |
||
1001 | for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) { |
||
1002 | $bmp .= "\x00"; |
||
1003 | for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) { |
||
1004 | if ( |
||
1005 | ($x >= $this->m_img->m_gih->m_nLeft) && |
||
1006 | ($y >= $this->m_img->m_gih->m_nTop) && |
||
1007 | ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) && |
||
1008 | ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) { |
||
1009 | // PART OF IMAGE |
||
1010 | $bmp .= $data[$nPxl]; |
||
1011 | } else { |
||
1012 | // BACKGROUND |
||
1013 | if ($bgColor == -1) { |
||
1014 | $bmp .= chr($this->m_gfh->m_nBgColor); |
||
1015 | } else { |
||
1016 | $bmp .= chr($bgColor); |
||
1017 | } |
||
1018 | } |
||
1019 | } |
||
1020 | } |
||
1021 | $bmp = gzcompress($bmp, 9); |
||
1022 | |||
1023 | /////////////////////////////////////////////////////////////////////// |
||
1024 | // SIGNATURE |
||
1025 | $out .= "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"; |
||
1026 | /////////////////////////////////////////////////////////////////////// |
||
1027 | // HEADER |
||
1028 | $out .= "\x00\x00\x00\x0D"; |
||
1029 | $tmp = 'IHDR'; |
||
1030 | $tmp .= $this->ndword($this->m_gfh->m_nWidth); |
||
1031 | $tmp .= $this->ndword($this->m_gfh->m_nHeight); |
||
1032 | $tmp .= "\x08\x03\x00\x00\x00"; |
||
1033 | $out .= $tmp; |
||
1034 | $out .= $this->ndword(crc32($tmp)); |
||
1035 | /////////////////////////////////////////////////////////////////////// |
||
1036 | // PALETTE |
||
1037 | if ($nColors > 0) { |
||
1038 | $out .= $this->ndword($nColors * 3); |
||
1039 | $tmp = 'PLTE'; |
||
1040 | $tmp .= $pal; |
||
1041 | $out .= $tmp; |
||
1042 | $out .= $this->ndword(crc32($tmp)); |
||
1043 | } |
||
1044 | /////////////////////////////////////////////////////////////////////// |
||
1045 | // TRANSPARENCY |
||
1046 | if (@$this->m_img->m_bTrans && ($nColors > 0)) { |
||
1047 | $out .= $this->ndword($nColors); |
||
1048 | $tmp = 'tRNS'; |
||
1049 | for ($i = 0; $i < $nColors; $i++) { |
||
1050 | $tmp .= ($i == $this->m_img->m_nTrans) ? "\x00" : "\xFF"; |
||
1051 | } |
||
1052 | $out .= $tmp; |
||
1053 | $out .= $this->ndword(crc32($tmp)); |
||
1054 | } |
||
1055 | /////////////////////////////////////////////////////////////////////// |
||
1056 | // DATA BITS |
||
1057 | $out .= $this->ndword(strlen($bmp)); |
||
1058 | $tmp = 'IDAT'; |
||
1059 | $tmp .= $bmp; |
||
1060 | $out .= $tmp; |
||
1061 | $out .= $this->ndword(crc32($tmp)); |
||
1062 | /////////////////////////////////////////////////////////////////////// |
||
1063 | // END OF FILE |
||
1064 | $out .= "\x00\x00\x00\x00IEND\xAE\x42\x60\x82"; |
||
1065 | |||
1066 | return $out; |
||
1067 | } |
||
1068 | |||
1069 | /////////////////////////////////////////////////////////////////////////// |
||
1070 | |||
1071 | // Added by James Heinrich <[email protected]> - January 5, 2003 |
||
1072 | |||
1073 | // Takes raw image data and plots it pixel-by-pixel on a new GD image and returns that |
||
1074 | // It's extremely slow, but the only solution when imagecreatefromstring() fails |
||
1075 | public function getGD_PixelPlotterVersion() |
||
1076 | { |
||
1077 | if (!$this->m_bLoaded) { |
||
1078 | return false; |
||
1079 | } |
||
1080 | |||
1081 | // PREPARE COLOR TABLE (RGBQUADs) |
||
1082 | if ($this->m_img->m_gih->m_bLocalClr) { |
||
1083 | $pal = $this->m_img->m_gih->m_colorTable->toString(); |
||
1084 | } elseif ($this->m_gfh->m_bGlobalClr) { |
||
1085 | $pal = $this->m_gfh->m_colorTable->toString(); |
||
1086 | } else { |
||
1087 | die('No color table available in getGD_PixelPlotterVersion()'); |
||
1088 | } |
||
1089 | |||
1090 | $PlottingIMG = imagecreate($this->m_gfh->m_nWidth, $this->m_gfh->m_nHeight); |
||
1091 | $NumColorsInPal = floor(strlen($pal) / 3); |
||
1092 | $ThisImageColor = array(); |
||
1093 | for ($i = 0; $i < $NumColorsInPal; $i++) { |
||
1094 | $ThisImageColor[$i] = imagecolorallocate( |
||
1095 | $PlottingIMG, |
||
1096 | ord($pal{($i * 3) + 0}), |
||
1097 | ord($pal{($i * 3) + 1}), |
||
1098 | ord($pal{($i * 3) + 2})); |
||
1099 | } |
||
1100 | |||
1101 | // PREPARE BITMAP BITS |
||
1102 | $data = $this->m_img->m_data; |
||
1103 | $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth; |
||
1104 | for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) { |
||
1105 | if (!phpthumb_functions::FunctionIsDisabled('set_time_limit')) { |
||
1106 | set_time_limit(30); |
||
1107 | } |
||
1108 | for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) { |
||
1109 | if ( |
||
1110 | ($x >= $this->m_img->m_gih->m_nLeft) && |
||
1111 | ($y >= $this->m_img->m_gih->m_nTop) && |
||
1112 | ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth)) && |
||
1113 | ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))) { |
||
1114 | // PART OF IMAGE |
||
1115 | if (@$this->m_img->m_bTrans && (ord($data[$nPxl]) == $this->m_img->m_nTrans)) { |
||
1116 | imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]); |
||
1117 | } else { |
||
1118 | imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[ord($data[$nPxl])]); |
||
1119 | } |
||
1120 | } else { |
||
1121 | // BACKGROUND |
||
1122 | imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]); |
||
1123 | } |
||
1124 | } |
||
1125 | $nPxl -= $this->m_gfh->m_nWidth << 1; |
||
1126 | |||
1127 | } |
||
1128 | |||
1129 | return $PlottingIMG; |
||
1130 | } |
||
1131 | |||
1132 | /////////////////////////////////////////////////////////////////////////// |
||
1133 | |||
1134 | public function dword($val) |
||
1135 | { |
||
1136 | $val = (int) $val; |
||
1137 | return chr($val & 0xFF).chr(($val & 0xFF00) >> 8).chr(($val & 0xFF0000) >> 16).chr(($val & 0xFF000000) >> 24); |
||
1138 | } |
||
1139 | |||
1140 | /////////////////////////////////////////////////////////////////////////// |
||
1141 | |||
1142 | public function ndword($val) |
||
1146 | } |
||
1147 | |||
1148 | /////////////////////////////////////////////////////////////////////////// |
||
1149 | |||
1150 | public function width() |
||
1151 | { |
||
1152 | return $this->m_gfh->m_nWidth; |
||
1153 | } |
||
1154 | |||
1155 | /////////////////////////////////////////////////////////////////////////// |
||
1156 | |||
1157 | public function height() |
||
1158 | { |
||
1159 | return $this->m_gfh->m_nHeight; |
||
1160 | } |
||
1161 | |||
1162 | /////////////////////////////////////////////////////////////////////////// |
||
1163 | |||
1164 | public function comment() |
||
1165 | { |
||
1166 | return $this->m_img->m_lpComm; |
||
1167 | } |
||
1168 | |||
1169 | /////////////////////////////////////////////////////////////////////////// |
||
1170 | |||
1171 | public function loaded() |
||
1174 | } |
||
1175 | } |
||
1176 |