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 |
||
99 | function sf_createThumbnail($source, $thumb_width) |
||
100 | { |
||
101 | global $xoopsModuleConfig; |
||
102 | |||
103 | $img_path = XOOPS_ROOT_PATH . '/' . $xoopsModuleConfig['dir_attachments']; |
||
104 | $thumb_path = $img_path . '/thumbs'; |
||
105 | $src_file = $img_path . '/' . $source; |
||
106 | $new_file = $thumb_path . '/' . $source; |
||
107 | //$imageLibs = sf_getImageLibs(); |
||
108 | |||
109 | if (!filesize($src_file) || !is_readable($src_file)) { |
||
110 | return false; |
||
111 | } |
||
112 | |||
113 | if (!is_dir($thumb_path) || !is_writable($thumb_path)) { |
||
114 | return false; |
||
115 | } |
||
116 | |||
117 | $imginfo = @getimagesize($src_file); |
||
118 | |||
119 | if (null == $imginfo) { |
||
120 | return false; |
||
121 | } |
||
122 | if ($imginfo[0] < $thumb_width) { |
||
123 | return false; |
||
124 | } |
||
125 | |||
126 | $newWidth = (int)min($imginfo[0], $thumb_width); |
||
127 | $newHeight = (int)($imginfo[1] * $newWidth / $imginfo[0]); |
||
128 | |||
129 | if ($xoopsModuleConfig['image_lib'] == 1 or $xoopsModuleConfig['image_lib'] == 0) { |
||
130 | if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) { |
||
131 | $cur_dir = __DIR__; |
||
132 | $src_file_im = '"' . $cur_dir . '\\' . strtr($src_file, '/', '\\') . '"'; |
||
133 | $new_file_im = '"' . $cur_dir . '\\' . strtr($new_file, '/', '\\') . '"'; |
||
134 | } else { |
||
135 | $src_file_im = @escapeshellarg($src_file); |
||
136 | $new_file_im = @escapeshellarg($new_file); |
||
137 | } |
||
138 | $path = empty($xoopsModuleConfig['path_magick']) ? '' : $xoopsModuleConfig['path_magick'] . '/'; |
||
139 | $magick_command = $path . 'convert -quality 85 -antialias -sample ' . $newWidth . 'x' . $newHeight . ' ' . $src_file_im . ' +profile "*" ' . str_replace('\\', '/', $new_file_im) . ''; |
||
140 | |||
141 | @passthru($magick_command); |
||
142 | if (file_exists($new_file)) { |
||
143 | return true; |
||
144 | } |
||
145 | } |
||
146 | |||
147 | if ($xoopsModuleConfig['image_lib'] == 2 or $xoopsModuleConfig['image_lib'] == 0) { |
||
148 | $path = empty($xoopsModuleConfig['path_netpbm']) ? '' : $xoopsModuleConfig['path_netpbm'] . '/'; |
||
149 | if (preg_match("/\.png/i", $source)) { |
||
150 | $cmd = $path . "pngtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "pnmtopng > $new_file"; |
||
151 | } elseif (preg_match("/\.(jpg|jpeg)/i", $source)) { |
||
152 | $cmd = $path . "jpegtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "ppmtojpeg -quality=90 > $new_file"; |
||
153 | } elseif (preg_match("/\.gif/i", $source)) { |
||
154 | $cmd = $path . "giftopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | ppmquant 256 | " . $path . "ppmtogif > $new_file"; |
||
155 | } |
||
156 | |||
157 | @exec($cmd, $output, $retval); |
||
158 | if (file_exists($new_file)) { |
||
159 | return true; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | $type = $imginfo[2]; |
||
164 | $supported_types = array(); |
||
165 | |||
166 | if (!extension_loaded('gd')) { |
||
167 | return false; |
||
168 | } |
||
169 | if (function_exists('imagegif')) { |
||
170 | $supported_types[] = 1; |
||
171 | } |
||
172 | if (function_exists('imagejpeg')) { |
||
173 | $supported_types[] = 2; |
||
174 | } |
||
175 | if (function_exists('imagepng')) { |
||
176 | $supported_types[] = 3; |
||
177 | } |
||
178 | |||
179 | $imageCreateFunction = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate'; |
||
180 | |||
181 | if (in_array($type, $supported_types)) { |
||
182 | switch ($type) { |
||
183 | case 1: |
||
184 | if (!function_exists('imagecreatefromgif')) { |
||
185 | return false; |
||
186 | } |
||
187 | $im = imagecreatefromgif($src_file); |
||
188 | $new_im = imagecreate($newWidth, $newHeight); |
||
189 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
190 | imagegif($new_im, $new_file); |
||
191 | imagedestroy($im); |
||
192 | imagedestroy($new_im); |
||
193 | break; |
||
194 | View Code Duplication | case 2: |
|
195 | $im = imagecreatefromjpeg($src_file); |
||
196 | $new_im = $imageCreateFunction($newWidth, $newHeight); |
||
197 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
198 | imagejpeg($new_im, $new_file, 90); |
||
199 | imagedestroy($im); |
||
200 | imagedestroy($new_im); |
||
201 | break; |
||
202 | View Code Duplication | case 3: |
|
203 | $im = imagecreatefrompng($src_file); |
||
204 | $new_im = $imageCreateFunction($newWidth, $newHeight); |
||
205 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
206 | imagepng($new_im, $new_file); |
||
207 | imagedestroy($im); |
||
208 | imagedestroy($new_im); |
||
209 | break; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | if (file_exists($new_file)) { |
||
214 | return true; |
||
215 | } else { |
||
216 | return false; |
||
217 | } |
||
218 | } |
||
219 | |||
221 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.