Completed
Branch master (dacad6)
by samayo
02:24 queued 01:12
created

func.image-resize.php ➔ resize()   D

Complexity

Conditions 13
Paths 200

Size

Total Lines 79
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 54
nc 200
nop 8
dl 0
loc 79
rs 4.761
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Image Resizing Function.
4
 *
5
 * @author     Daniel, Simon <[email protected]>
6
 * @link       https://github.com/samayo/bulletproof
7
 * @copyright  Copyright (c) 2015 Simon Daniel
8
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
9
 */
10
namespace Bulletproof;
11
12
function resize($image, $mimeType, $imgWidth, $imgHeight, $newWidth, $newHeight, $ratio = FALSE, $upsize = TRUE)
13
{
14
15
    // First, calculate the height.
16
    $height = intval($newWidth / $imgWidth * $imgHeight);
17
18
    // If the height is too large, set it to the maximum height and calculate the width.
19
    if ($height > $newHeight) {
20
21
        $height = $newHeight;
22
        $newWidth = intval($height / $imgHeight * $imgWidth);
23
    }
24
25
    // If we don't allow upsizing check if the new width or height are too big.
26
    if (!$upsize) {
27
        // If the given width is larger then the image height, then resize it.
28
        if ($newWidth > $imgWidth) {
29
            $newWidth = $imgWidth;
30
            $height = intval($newWidth / $imgWidth * $imgHeight);
31
        }
32
33
        // If the given height is larger then the image height, then resize it.
34
        if ($height > $imgHeight) {
35
            $height = $imgHeight;
36
            $newWidth = intval($height / $imgHeight * $imgWidth);
37
        }
38
    }
39
40
    if ($ratio == true) {
41
        $source_aspect_ratio = $imgWidth / $imgHeight;
42
        $thumbnail_aspect_ratio = $newWidth / $newHeight;
43
        if ($imgWidth <= $newWidth && $imgHeight <= $newHeight) {
44
            $newWidth = $imgWidth;
45
            $newHeight = $imgHeight;
46
        } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
47
            $newWidth = (int)($newHeight * $source_aspect_ratio);
48
            $newHeight = $newHeight;
49
        } else {
50
            $newWidth = $newWidth;
51
            $newHeight = (int)($newWidth / $source_aspect_ratio);
52
        }
53
    }
54
55
    $imgString = file_get_contents($image);
56
57
    $imageFromString = imagecreatefromstring($imgString);
58
    $tmp = imagecreatetruecolor($newWidth, $newHeight);
59
    imagealphablending($tmp, false);
60
    imagesavealpha($tmp, true);
61
    imagecopyresampled(
62
        $tmp,
63
        $imageFromString,
64
        0,
65
        0,
66
        0,
67
        0,
68
        $newWidth,
69
        $newHeight,
70
        $imgWidth,
71
        $imgHeight
72
    );
73
74
    switch ($mimeType) {
75
        case "jpeg":
76
        case "jpg":
77
            imagejpeg($tmp, $image, 90);
78
            break;
79
        case "png":
80
            imagepng($tmp, $image, 0);
81
            break;
82
        case "gif":
83
            imagegif($tmp, $image);
84
            break;
85
        default:
86
            throw new \Exception(" Only jpg, jpeg, png and gif files can be resized ");
87
            break;
88
    }
89
90
}
91
92
93