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 |
||
223 | public function resizeImage() |
||
224 | { |
||
225 | /** @var Wflinks\Helper $helper */ |
||
226 | $helper = Wflinks\Helper::getInstance(); |
||
227 | |||
228 | // $this->_img_info = info array to the image being resized |
||
229 | // $this->_img_info[0] == width |
||
230 | // $this->_img_info[1] == height |
||
231 | // $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) |
||
232 | // $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 |
||
233 | /** |
||
234 | * Get image size and scale ratio |
||
235 | */ |
||
236 | $scale = \min($this->img_width / $this->_img_info[0], $this->img_height / $this->_img_info[1]); |
||
237 | /** |
||
238 | * If the image is larger than the max shrink it |
||
239 | */ |
||
240 | $newWidth = $this->img_width; |
||
241 | $newHeight = $this->img_height; |
||
242 | if ($scale < 1 && 1 == $this->img_aspect) { |
||
243 | $newWidth = \floor($scale * $this->_img_info[0]); |
||
244 | $newHeight = \floor($scale * $this->_img_info[1]); |
||
245 | } |
||
246 | $newWidth = ($newWidth > $this->_img_info[0]) ? $this->_img_info[0] : $newWidth; |
||
247 | $newHeight = ($newHeight > $this->_img_info[0]) ? $this->_img_info[0] : $newHeight; |
||
248 | |||
249 | $savefile = "{$newWidth}x{$newHeight}_{$this->_img_name}"; |
||
250 | $this->_save_image = "{$this->_save_path}/{$savefile}"; |
||
251 | |||
252 | if (0 == $this->img_update && \file_exists($this->_save_image)) { |
||
253 | if (1 == $this->_return_fullpath) { |
||
254 | return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
||
255 | } |
||
256 | |||
257 | return "{$this->_img_savepath}/{$savefile}"; |
||
258 | } |
||
259 | |||
260 | switch ($this->_image_type) { |
||
261 | case 'im': |
||
262 | if (!empty($helper->getConfig('path_magick')) && \is_dir($helper->getConfig('path_magick'))) { |
||
263 | if (\preg_match('#[A-Z]:|\\\\#Ai', __FILE__)) { |
||
264 | $cur_dir = __DIR__; |
||
265 | $src_file_im = '"' . $cur_dir . '\\' . \str_replace('/', '\\', $this->_source_image) . '"'; |
||
266 | $new_file_im = '"' . $cur_dir . '\\' . \str_replace('/', '\\', $this->_save_image) . '"'; |
||
267 | } else { |
||
268 | $src_file_im = \escapeshellarg($this->_source_image); |
||
269 | $new_file_im = \escapeshellarg($this->_save_image); |
||
270 | } |
||
271 | $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) . ''; |
||
272 | \passthru($magick_command); |
||
273 | |||
274 | return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
||
275 | } |
||
276 | |||
277 | return false; |
||
278 | break; |
||
279 | case 'gd1': |
||
280 | case 'gd2': |
||
281 | default: |
||
282 | |||
283 | $imageCreateFunction = (\function_exists('imagecreatetruecolor') |
||
284 | && 'gd2' === $this->_image_type) ? 'imagecreatetruecolor' : 'imagecreate'; |
||
285 | $imageCopyfunction = (\function_exists('imagecopyresampled') |
||
286 | && 'gd2' === $this->_image_type) ? 'imagecopyresampled' : 'imagecopyresized'; |
||
287 | |||
288 | switch ($this->_img_info[2]) { |
||
289 | case 1: |
||
290 | // GIF image |
||
291 | $img = \function_exists('imagecreatefromgif') ? \imagecreatefromgif($this->_source_image) : \imagecreatefrompng($this->_source_image); |
||
292 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); |
||
293 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
||
294 | if (\function_exists('imagegif')) { |
||
295 | \imagegif($tmp_img, $this->_save_image); |
||
296 | } else { |
||
297 | \imagepng($tmp_img, $this->_save_image); |
||
298 | } |
||
299 | \imagedestroy($tmp_img); |
||
300 | break; |
||
301 | case 2: |
||
302 | // echo $this->_save_image; |
||
303 | $img = \function_exists('imagecreatefromjpeg') ? \imagecreatefromjpeg($this->_source_image) : \imagecreatefrompng($this->_source_image); |
||
304 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); |
||
305 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
||
306 | if (\function_exists('imagejpeg')) { |
||
307 | \imagejpeg($tmp_img, $this->_save_image, $this->img_quality); |
||
308 | } else { |
||
309 | \imagepng($tmp_img, $this->_save_image); |
||
310 | } |
||
311 | \imagedestroy($tmp_img); |
||
312 | break; |
||
313 | case 3: |
||
314 | // PNG image |
||
315 | $img = \imagecreatefrompng($this->_source_image); |
||
316 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); |
||
317 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
||
318 | \imagepng($tmp_img, $this->_save_image); |
||
319 | \imagedestroy($tmp_img); |
||
320 | break; |
||
321 | default: |
||
322 | return false; |
||
323 | } |
||
324 | if (1 == $this->_return_fullpath) { |
||
325 | return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
||
326 | } |
||
327 | |||
328 | return "{$this->_img_savepath}/{$savefile}"; |
||
329 | break; |
||
330 | } |
||
331 | |||
332 | return false; |
||
333 | } |
||
397 |