Conditions | 37 |
Paths | > 20000 |
Total Lines | 133 |
Code Lines | 82 |
Lines | 133 |
Ratio | 100 % |
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 |
||
138 | function createThumb($sourceFile, $targetFile, $maxWidth, $maxHeight, $quality, $preserverAspectRatio, $bmpSupported = false) |
||
139 | { |
||
140 | $sourceImageAttr = @getimagesize($sourceFile); |
||
141 | if ($sourceImageAttr === false) { |
||
142 | return false; |
||
143 | } |
||
144 | $sourceImageWidth = isset($sourceImageAttr[0]) ? $sourceImageAttr[0] : 0; |
||
145 | $sourceImageHeight = isset($sourceImageAttr[1]) ? $sourceImageAttr[1] : 0; |
||
146 | $sourceImageMime = isset($sourceImageAttr["mime"]) ? $sourceImageAttr["mime"] : ""; |
||
147 | $sourceImageBits = isset($sourceImageAttr["bits"]) ? $sourceImageAttr["bits"] : 8; |
||
148 | $sourceImageChannels = isset($sourceImageAttr["channels"]) ? $sourceImageAttr["channels"] : 3; |
||
149 | |||
150 | if (!$sourceImageWidth || !$sourceImageHeight || !$sourceImageMime) { |
||
151 | return false; |
||
152 | } |
||
153 | |||
154 | $iFinalWidth = $maxWidth == 0 ? $sourceImageWidth : $maxWidth; |
||
155 | $iFinalHeight = $maxHeight == 0 ? $sourceImageHeight : $maxHeight; |
||
156 | |||
157 | if ($sourceImageWidth <= $iFinalWidth && $sourceImageHeight <= $iFinalHeight) { |
||
158 | if ($sourceFile != $targetFile) { |
||
159 | copy($sourceFile, $targetFile); |
||
160 | } |
||
161 | return true; |
||
162 | } |
||
163 | |||
164 | if ($preserverAspectRatio) |
||
165 | { |
||
166 | // Gets the best size for aspect ratio resampling |
||
167 | $oSize = CKFinder_Connector_CommandHandler_Thumbnail::GetAspectRatioSize($iFinalWidth, $iFinalHeight, $sourceImageWidth, $sourceImageHeight ); |
||
168 | } |
||
169 | else { |
||
170 | $oSize = array('Width' => $iFinalWidth, 'Height' => $iFinalHeight); |
||
171 | } |
||
172 | |||
173 | CKFinder_Connector_Utils_Misc::setMemoryForImage($sourceImageWidth, $sourceImageHeight, $sourceImageBits, $sourceImageChannels); |
||
174 | |||
175 | switch ($sourceImageAttr['mime']) |
||
176 | { |
||
177 | case 'image/gif': |
||
178 | { |
||
179 | if (@imagetypes() & IMG_GIF) { |
||
180 | $oImage = @imagecreatefromgif($sourceFile); |
||
181 | } else { |
||
182 | $ermsg = 'GIF images are not supported'; |
||
183 | } |
||
184 | } |
||
185 | break; |
||
186 | case 'image/jpeg': |
||
187 | { |
||
188 | if (@imagetypes() & IMG_JPG) { |
||
189 | $oImage = @imagecreatefromjpeg($sourceFile) ; |
||
190 | } else { |
||
191 | $ermsg = 'JPEG images are not supported'; |
||
192 | } |
||
193 | } |
||
194 | break; |
||
195 | case 'image/png': |
||
196 | { |
||
197 | if (@imagetypes() & IMG_PNG) { |
||
198 | $oImage = @imagecreatefrompng($sourceFile) ; |
||
199 | } else { |
||
200 | $ermsg = 'PNG images are not supported'; |
||
201 | } |
||
202 | } |
||
203 | break; |
||
204 | case 'image/wbmp': |
||
205 | { |
||
206 | if (@imagetypes() & IMG_WBMP) { |
||
207 | $oImage = @imagecreatefromwbmp($sourceFile); |
||
208 | } else { |
||
209 | $ermsg = 'WBMP images are not supported'; |
||
210 | } |
||
211 | } |
||
212 | break; |
||
213 | case 'image/bmp': |
||
214 | { |
||
215 | /* |
||
216 | * This is sad that PHP doesn't support bitmaps. |
||
217 | * Anyway, we will use our custom function at least to display thumbnails. |
||
218 | * We'll not resize images this way (if $sourceFile === $targetFile), |
||
219 | * because user defined imagecreatefrombmp and imagecreatebmp are horribly slow |
||
220 | */ |
||
221 | if ($bmpSupported && (@imagetypes() & IMG_JPG) && $sourceFile != $targetFile) { |
||
222 | $oImage = CKFinder_Connector_Utils_Misc::imageCreateFromBmp($sourceFile); |
||
223 | } else { |
||
224 | $ermsg = 'BMP/JPG images are not supported'; |
||
225 | } |
||
226 | } |
||
227 | break; |
||
228 | default: |
||
229 | $ermsg = $sourceImageAttr['mime'].' images are not supported'; |
||
230 | break; |
||
231 | } |
||
232 | |||
233 | if (isset($ermsg) || false === $oImage) { |
||
234 | return false; |
||
235 | } |
||
236 | |||
237 | |||
238 | $oThumbImage = imagecreatetruecolor($oSize["Width"], $oSize["Height"]); |
||
239 | //imagecopyresampled($oThumbImage, $oImage, 0, 0, 0, 0, $oSize["Width"], $oSize["Height"], $sourceImageWidth, $sourceImageHeight); |
||
240 | CKFinder_Connector_Utils_Misc::fastImageCopyResampled($oThumbImage, $oImage, 0, 0, 0, 0, $oSize["Width"], $oSize["Height"], $sourceImageWidth, $sourceImageHeight, (int)max(floor($quality/20), 1)); |
||
241 | |||
242 | switch ($sourceImageAttr['mime']) |
||
243 | { |
||
244 | case 'image/gif': |
||
245 | imagegif($oThumbImage, $targetFile); |
||
246 | break; |
||
247 | case 'image/jpeg': |
||
248 | case 'image/bmp': |
||
249 | imagejpeg($oThumbImage, $targetFile, $quality); |
||
250 | break; |
||
251 | case 'image/png': |
||
252 | imagepng($oThumbImage, $targetFile); |
||
253 | break; |
||
254 | case 'image/wbmp': |
||
255 | imagewbmp($oThumbImage, $targetFile); |
||
256 | break; |
||
257 | } |
||
258 | |||
259 | $_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config"); |
||
260 | if (file_exists($targetFile) && ($perms = $_config->getChmodFiles())) { |
||
261 | $oldUmask = umask(0); |
||
262 | chmod($targetFile, $perms); |
||
263 | umask($oldUmask); |
||
264 | } |
||
265 | |||
266 | imageDestroy($oImage); |
||
267 | imageDestroy($oThumbImage); |
||
268 | |||
269 | return true; |
||
270 | } |
||
271 | |||
323 |