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