Conditions | 26 |
Paths | 7336 |
Total Lines | 110 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
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 |
||
227 | public function resizeImage() |
||
228 | { |
||
229 | /** @var Wflinks\Helper $helper */ |
||
230 | $helper = Wflinks\Helper::getInstance(); |
||
231 | |||
232 | // $this->_img_info = info array to the image being resized |
||
233 | // $this->_img_info[0] == width |
||
234 | // $this->_img_info[1] == height |
||
235 | // $this->_img_info[2] == is a flag indicating the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order) |
||
236 | // $this->_img_info[3] == is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag |
||
237 | /** |
||
238 | * Get image size and scale ratio |
||
239 | */ |
||
240 | $scale = \min($this->img_width / $this->_img_info[0], $this->img_height / $this->_img_info[1]); |
||
241 | /** |
||
242 | * If the image is larger than the max shrink it |
||
243 | */ |
||
244 | $newWidth = $this->img_width; |
||
245 | $newHeight = $this->img_height; |
||
246 | if ($scale < 1 && 1 == $this->img_aspect) { |
||
247 | $newWidth = \floor($scale * $this->_img_info[0]); |
||
248 | $newHeight = \floor($scale * $this->_img_info[1]); |
||
249 | } |
||
250 | $newWidth = ($newWidth > $this->_img_info[0]) ? $this->_img_info[0] : $newWidth; |
||
251 | $newHeight = ($newHeight > $this->_img_info[0]) ? $this->_img_info[0] : $newHeight; |
||
252 | |||
253 | $savefile = "{$newWidth}x{$newHeight}_{$this->_img_name}"; |
||
254 | $this->_save_image = "{$this->_save_path}/{$savefile}"; |
||
255 | |||
256 | if (0 == $this->img_update && \file_exists($this->_save_image)) { |
||
257 | if (1 == $this->_return_fullpath) { |
||
258 | return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
||
259 | } |
||
260 | |||
261 | return "{$this->_img_savepath}/{$savefile}"; |
||
262 | } |
||
263 | |||
264 | switch ($this->_image_type) { |
||
265 | case 'im': |
||
266 | if (!empty($helper->getConfig('path_magick')) && \is_dir($helper->getConfig('path_magick'))) { |
||
267 | if (\preg_match('#[A-Z]:|\\\\#Ai', __FILE__)) { |
||
268 | $cur_dir = __DIR__; |
||
269 | $src_file_im = '"' . $cur_dir . '\\' . \str_replace('/', '\\', $this->_source_image) . '"'; |
||
270 | $new_file_im = '"' . $cur_dir . '\\' . \str_replace('/', '\\', $this->_save_image) . '"'; |
||
271 | } else { |
||
272 | $src_file_im = \escapeshellarg($this->_source_image); |
||
273 | $new_file_im = \escapeshellarg($this->_save_image); |
||
274 | } |
||
275 | $magick_command = $helper->getConfig('path_magick') . '/convert -quality {$helper->getConfig("imagequality")} -antialias -sample {$newWidth}x{$newHeight} {$src_file_im} +profile "*" ' . \str_replace('\\', '/', $new_file_im) . ''; |
||
276 | \passthru($magick_command); |
||
277 | |||
278 | return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
||
279 | } |
||
280 | |||
281 | return false; |
||
282 | break; |
||
283 | case 'gd1': |
||
284 | case 'gd2': |
||
285 | default: |
||
286 | |||
287 | $imageCreateFunction = (\function_exists('imagecreatetruecolor') |
||
288 | && 'gd2' === $this->_image_type) ? 'imagecreatetruecolor' : 'imagecreate'; |
||
289 | $imageCopyfunction = (\function_exists('imagecopyresampled') |
||
290 | && 'gd2' === $this->_image_type) ? 'imagecopyresampled' : 'imagecopyresized'; |
||
291 | |||
292 | switch ($this->_img_info[2]) { |
||
293 | case 1: |
||
294 | // GIF image |
||
295 | $img = \function_exists('imagecreatefromgif') ? \imagecreatefromgif($this->_source_image) : \imagecreatefrompng($this->_source_image); |
||
296 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); |
||
297 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
||
298 | if (\function_exists('imagegif')) { |
||
299 | \imagegif($tmp_img, $this->_save_image); |
||
300 | } else { |
||
301 | \imagepng($tmp_img, $this->_save_image); |
||
302 | } |
||
303 | \imagedestroy($tmp_img); |
||
304 | break; |
||
305 | case 2: |
||
306 | // echo $this->_save_image; |
||
307 | $img = \function_exists('imagecreatefromjpeg') ? \imagecreatefromjpeg($this->_source_image) : \imagecreatefrompng($this->_source_image); |
||
308 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); |
||
309 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
||
310 | if (\function_exists('imagejpeg')) { |
||
311 | \imagejpeg($tmp_img, $this->_save_image, $this->img_quality); |
||
312 | } else { |
||
313 | \imagepng($tmp_img, $this->_save_image); |
||
314 | } |
||
315 | \imagedestroy($tmp_img); |
||
316 | break; |
||
317 | case 3: |
||
318 | // PNG image |
||
319 | $img = \imagecreatefrompng($this->_source_image); |
||
320 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); |
||
321 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
||
322 | \imagepng($tmp_img, $this->_save_image); |
||
323 | \imagedestroy($tmp_img); |
||
324 | break; |
||
325 | default: |
||
326 | return false; |
||
327 | } |
||
328 | if (1 == $this->_return_fullpath) { |
||
329 | return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
||
330 | } |
||
331 | |||
332 | return "{$this->_img_savepath}/{$savefile}"; |
||
333 | break; |
||
334 | } |
||
335 | |||
336 | return false; |
||
337 | } |
||
401 |