Conditions | 30 |
Paths | 8006 |
Total Lines | 118 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
102 | function newbbCreateThumbnail($source, $thumb_width) |
||
103 | { |
||
104 | $cmd = ''; |
||
105 | $img_path = $GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments']); |
||
106 | $thumb_path = $img_path . '/thumbs'; |
||
107 | $src_file = $img_path . '/' . $source; |
||
108 | $new_file = $thumb_path . '/' . $source; |
||
109 | //$imageLibs = newbb_getImageLibs(); |
||
110 | |||
111 | if (!filesize($src_file) || !is_readable($src_file)) { |
||
112 | return false; |
||
113 | } |
||
114 | |||
115 | if (!is_dir($thumb_path) || !is_writable($thumb_path)) { |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | $imginfo = @getimagesize($src_file); |
||
120 | |||
121 | if (null === $imginfo) { |
||
122 | return false; |
||
123 | } |
||
124 | if ($imginfo[0] < $thumb_width) { |
||
125 | return false; |
||
126 | } |
||
127 | |||
128 | $newWidth = (int)min($imginfo[0], $thumb_width); |
||
129 | $newHeight = (int)($imginfo[1] * $newWidth / $imginfo[0]); |
||
130 | |||
131 | if (1 == $GLOBALS['xoopsModuleConfig']['image_lib'] || 0 == $GLOBALS['xoopsModuleConfig']['image_lib']) { |
||
132 | if (preg_match('#[A-Z]:|\\\\#Ai', __FILE__)) { |
||
133 | $cur_dir = __DIR__; |
||
134 | $src_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $src_file) . '"'; |
||
135 | $new_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $new_file) . '"'; |
||
136 | } else { |
||
137 | $src_file_im = @escapeshellarg($src_file); |
||
138 | $new_file_im = @escapeshellarg($new_file); |
||
139 | } |
||
140 | $path = empty($GLOBALS['xoopsModuleConfig']['path_magick']) ? '' : $GLOBALS['xoopsModuleConfig']['path_magick'] . '/'; |
||
141 | $magick_command = $path . 'convert -quality 85 -antialias -sample ' . $newWidth . 'x' . $newHeight . ' ' . $src_file_im . ' +profile "*" ' . str_replace('\\', '/', $new_file_im) . ''; |
||
142 | |||
143 | @passthru($magick_command); |
||
144 | if (file_exists($new_file)) { |
||
145 | return true; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | if (2 == $GLOBALS['xoopsModuleConfig']['image_lib'] || 0 == $GLOBALS['xoopsModuleConfig']['image_lib']) { |
||
150 | $path = empty($GLOBALS['xoopsModuleConfig']['path_netpbm']) ? '' : $GLOBALS['xoopsModuleConfig']['path_netpbm'] . '/'; |
||
151 | if (preg_match("/\.png$/", $source)) { |
||
152 | $cmd = $path . "pngtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "pnmtopng > $new_file"; |
||
153 | } elseif (preg_match("/\.(jpg|jpeg)$/", $source)) { |
||
154 | $cmd = $path . "jpegtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "ppmtojpeg -quality=90 > $new_file"; |
||
155 | } elseif (preg_match("/\.gif$/", $source)) { |
||
156 | $cmd = $path . "giftopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | ppmquant 256 | " . $path . "ppmtogif > $new_file"; |
||
157 | } |
||
158 | |||
159 | @exec($cmd, $output, $retval); |
||
160 | if (file_exists($new_file)) { |
||
161 | return true; |
||
162 | } |
||
163 | } |
||
164 | |||
165 | $type = $imginfo[2]; |
||
166 | $supported_types = []; |
||
167 | |||
168 | if (!extension_loaded('gd')) { |
||
169 | return false; |
||
170 | } |
||
171 | if (function_exists('imagegif')) { |
||
172 | $supported_types[] = 1; |
||
173 | } |
||
174 | if (function_exists('imagejpeg')) { |
||
175 | $supported_types[] = 2; |
||
176 | } |
||
177 | if (function_exists('imagepng')) { |
||
178 | $supported_types[] = 3; |
||
179 | } |
||
180 | |||
181 | $imageCreateFunction = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate'; |
||
182 | |||
183 | if (in_array($type, $supported_types)) { |
||
184 | switch ($type) { |
||
185 | case 1: |
||
186 | if (!function_exists('imagecreatefromgif')) { |
||
187 | return false; |
||
188 | } |
||
189 | $im = imagecreatefromgif($src_file); |
||
190 | $new_im = imagecreate($newWidth, $newHeight); |
||
191 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
192 | imagegif($new_im, $new_file); |
||
193 | imagedestroy($im); |
||
194 | imagedestroy($new_im); |
||
195 | break; |
||
196 | case 2: |
||
197 | $im = imagecreatefromjpeg($src_file); |
||
198 | $new_im = $imageCreateFunction($newWidth, $newHeight); |
||
199 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
200 | imagejpeg($new_im, $new_file, 90); |
||
201 | imagedestroy($im); |
||
202 | imagedestroy($new_im); |
||
203 | break; |
||
204 | case 3: |
||
205 | $im = imagecreatefrompng($src_file); |
||
206 | $new_im = $imageCreateFunction($newWidth, $newHeight); |
||
207 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
208 | imagepng($new_im, $new_file); |
||
209 | imagedestroy($im); |
||
210 | imagedestroy($new_im); |
||
211 | break; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | if (file_exists($new_file)) { |
||
216 | return true; |
||
217 | } |
||
218 | |||
219 | return false; |
||
220 | } |
||
222 |