Conditions | 30 |
Paths | 8006 |
Total Lines | 120 |
Code Lines | 84 |
Lines | 16 |
Ratio | 13.33 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
90 | function sf_createThumbnail($source, $thumb_width) |
||
91 | { |
||
92 | global $xoopsModuleConfig; |
||
93 | |||
94 | $img_path = XOOPS_ROOT_PATH . '/' . $xoopsModuleConfig['dir_attachments']; |
||
95 | $thumb_path = $img_path . '/thumbs'; |
||
96 | $src_file = $img_path . '/' . $source; |
||
97 | $new_file = $thumb_path . '/' . $source; |
||
98 | //$imageLibs = sf_getImageLibs(); |
||
99 | |||
100 | if (!filesize($src_file) || !is_readable($src_file)) { |
||
101 | return false; |
||
102 | } |
||
103 | |||
104 | if (!is_dir($thumb_path) || !is_writable($thumb_path)) { |
||
105 | return false; |
||
106 | } |
||
107 | |||
108 | $imginfo = @getimagesize($src_file); |
||
109 | |||
110 | if (null === $imginfo) { |
||
111 | return false; |
||
112 | } |
||
113 | if ($imginfo[0] < $thumb_width) { |
||
114 | return false; |
||
115 | } |
||
116 | |||
117 | $newWidth = (int)min($imginfo[0], $thumb_width); |
||
118 | $newHeight = (int)($imginfo[1] * $newWidth / $imginfo[0]); |
||
119 | |||
120 | if (1 == $xoopsModuleConfig['image_lib'] or 0 == $xoopsModuleConfig['image_lib']) { |
||
121 | if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) { |
||
122 | $cur_dir = __DIR__; |
||
123 | $src_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $src_file) . '"'; |
||
124 | $new_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $new_file) . '"'; |
||
125 | } else { |
||
126 | $src_file_im = @escapeshellarg($src_file); |
||
127 | $new_file_im = @escapeshellarg($new_file); |
||
128 | } |
||
129 | $path = empty($xoopsModuleConfig['path_magick']) ? '' : $xoopsModuleConfig['path_magick'] . '/'; |
||
130 | $magick_command = $path . 'convert -quality 85 -antialias -sample ' . $newWidth . 'x' . $newHeight . ' ' . $src_file_im . ' +profile "*" ' . str_replace('\\', '/', $new_file_im) . ''; |
||
131 | |||
132 | @passthru($magick_command); |
||
133 | if (file_exists($new_file)) { |
||
134 | return true; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | if (2 == $xoopsModuleConfig['image_lib'] or 0 == $xoopsModuleConfig['image_lib']) { |
||
139 | $path = empty($xoopsModuleConfig['path_netpbm']) ? '' : $xoopsModuleConfig['path_netpbm'] . '/'; |
||
140 | if (preg_match("/\.png/i", $source)) { |
||
141 | $cmd = $path . "pngtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "pnmtopng > $new_file"; |
||
142 | } elseif (preg_match("/\.(jpg|jpeg)/i", $source)) { |
||
143 | $cmd = $path . "jpegtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "ppmtojpeg -quality=90 > $new_file"; |
||
144 | } elseif (preg_match("/\.gif/i", $source)) { |
||
145 | $cmd = $path . "giftopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | ppmquant 256 | " . $path . "ppmtogif > $new_file"; |
||
146 | } |
||
147 | |||
148 | @exec($cmd, $output, $retval); |
||
149 | if (file_exists($new_file)) { |
||
150 | return true; |
||
151 | } |
||
152 | } |
||
153 | |||
154 | $type = $imginfo[2]; |
||
155 | $supported_types = []; |
||
156 | |||
157 | if (!extension_loaded('gd')) { |
||
158 | return false; |
||
159 | } |
||
160 | if (function_exists('imagegif')) { |
||
161 | $supported_types[] = 1; |
||
162 | } |
||
163 | if (function_exists('imagejpeg')) { |
||
164 | $supported_types[] = 2; |
||
165 | } |
||
166 | if (function_exists('imagepng')) { |
||
167 | $supported_types[] = 3; |
||
168 | } |
||
169 | |||
170 | $imageCreateFunction = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate'; |
||
171 | |||
172 | if (in_array($type, $supported_types)) { |
||
173 | switch ($type) { |
||
174 | case 1: |
||
175 | if (!function_exists('imagecreatefromgif')) { |
||
176 | return false; |
||
177 | } |
||
178 | $im = imagecreatefromgif($src_file); |
||
179 | $new_im = imagecreate($newWidth, $newHeight); |
||
180 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
181 | imagegif($new_im, $new_file); |
||
182 | imagedestroy($im); |
||
183 | imagedestroy($new_im); |
||
184 | break; |
||
185 | case 2: |
||
186 | $im = imagecreatefromjpeg($src_file); |
||
187 | $new_im = $imageCreateFunction($newWidth, $newHeight); |
||
188 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
189 | imagejpeg($new_im, $new_file, 90); |
||
190 | imagedestroy($im); |
||
191 | imagedestroy($new_im); |
||
192 | break; |
||
193 | case 3: |
||
194 | $im = imagecreatefrompng($src_file); |
||
195 | $new_im = $imageCreateFunction($newWidth, $newHeight); |
||
196 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
197 | imagepng($new_im, $new_file); |
||
198 | imagedestroy($im); |
||
199 | imagedestroy($new_im); |
||
200 | break; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | if (file_exists($new_file)) { |
||
205 | return true; |
||
206 | } else { |
||
207 | return false; |
||
208 | } |
||
209 | } |
||
210 | |||
212 |