Completed
Branch master (a42ab4)
by samayo
64:27 queued 29:30
created

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

Complexity

Conditions 13
Paths 125

Size

Total Lines 86
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 62
nc 125
nop 8
dl 0
loc 86
rs 4.6605
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 Watermark Function.
4
 *
5
 * @author     D, 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 watermark($image, $mimeType, $imgWidth, $imgHeight, $watermark, $watermarkHeight, $watermarkWidth, $position = "center")
13
{
14
15
    // Calculate the watermark position
16
    switch ($position) {
17
        case "center":
18
            $marginBottom = round($imgHeight / 2);
19
            $marginRight = round($imgWidth / 2) - round($watermarkWidth / 2);
20
            break;
21
22
        case "top-left":
23
            $marginBottom = round($imgHeight - $watermarkHeight);
24
            $marginRight = round($imgWidth - $watermarkWidth);
25
            break;
26
27
        case "bottom-left":
28
            $marginBottom = 5;
29
            $marginRight = round($imgWidth - $watermarkWidth);
30
            break;
31
32
        case "top-right":
33
            $marginBottom = round($imgHeight - $watermarkHeight);
34
            $marginRight = 5;
35
            break;
36
37
        default:
38
            $marginBottom = 2;
39
            $marginRight = 2;
40
            break;
41
    }
42
43
44
    $watermark = imagecreatefrompng($watermark);
45
46
    switch ($mimeType) {
47
        case "jpeg":
48
        case "jpg":
49
            $createImage = imagecreatefromjpeg($image);
50
            break;
51
52
        case "png":
53
            $createImage = imagecreatefrompng($image);
54
            break;
55
56
        case "gif":
57
            $createImage = imagecreatefromgif($image);
58
            break;
59
60
        default:
61
            $createImage = imagecreatefromjpeg($image);
62
            break;
63
    }
64
65
    $sx = imagesx($watermark);
66
    $sy = imagesy($watermark);
67
    imagecopy(
68
        $createImage,
69
        $watermark,
70
        imagesx($createImage) - $sx - $marginRight,
71
        imagesy($createImage) - $sy - $marginBottom,
72
        0,
73
        0,
74
        imagesx($watermark),
75
        imagesy($watermark)
76
    );
77
78
79
    switch ($mimeType) {
80
        case "jpeg":
81
        case "jpg":
82
            imagejpeg($createImage, $image);
83
            break;
84
85
        case "png":
86
            imagepng($createImage, $image);
87
            break;
88
89
        case "gif":
90
            imagegif($createImage, $image);
91
            break;
92
93
        default:
94
            throw new \Exception("A watermark can only be applied to: jpeg, jpg, gif, png images ");
95
            break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
96
    }
97
}