Completed
Push — master ( d09fc3...fde49e )
by samayo
05:21
created

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

Complexity

Conditions 13
Paths 125

Size

Total Lines 85
Code Lines 62

Duplication

Lines 18
Ratio 21.18 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 13
eloc 62
c 1
b 0
f 1
nc 125
nop 8
dl 18
loc 85
rs 4.6605

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
    // Calculate the watermark position
15
    switch ($position) {
16
        case "center":
17
            $marginBottom  = round($imgHeight / 2);
18
            $marginRight   = round($imgWidth / 2) - round($watermarkWidth / 2);
19
            break;
20
21
        case "top-left":
22
            $marginBottom  = round($imgHeight - $watermarkHeight);
23
            $marginRight   = round($imgWidth - $watermarkWidth);
24
            break;
25
26
        case "bottom-left":
27
            $marginBottom  = 5;
28
            $marginRight   = round($imgWidth - $watermarkWidth);
29
            break;
30
31
        case "top-right":
32
            $marginBottom  = round($imgHeight - $watermarkHeight);
33
            $marginRight   = 5;
34
            break;
35
36
        default:
37
            $marginBottom  = 2;
38
            $marginRight   = 2;
39
            break;
40
    }
41
42
43
    $watermark = imagecreatefrompng($watermark);
44
45
    switch ($mimeType) {
46
        case "jpeg":
47
        case "jpg":
48
            $createImage = imagecreatefromjpeg($image);
49
            break;
50
51
        case "png":
52
            $createImage = imagecreatefrompng($image);
53
            break;
54
55
        case "gif":
56
            $createImage = imagecreatefromgif($image);
57
            break;
58
59
        default:
60
            $createImage = imagecreatefromjpeg($image);
61
            break;
62
    }
63
64
    $sx = imagesx($watermark);
65
    $sy = imagesy($watermark);
66
    imagecopy(
67
        $createImage,
68
        $watermark,
69
        imagesx($createImage) - $sx - $marginRight,
70
        imagesy($createImage) - $sy - $marginBottom,
71
        0,
72
        0,
73
        imagesx($watermark),
74
        imagesy($watermark)
75
    );
76
77
78 View Code Duplication
    switch ($mimeType) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
        case "jpeg":
80
        case "jpg":
81
             imagejpeg($createImage, $image);
82
            break;
83
84
        case "png":
85
             imagepng($createImage, $image);
86
            break;
87
88
        case "gif":
89
             imagegif($createImage, $image);
90
            break;
91
92
        default:
93
            throw new \Exception("A watermark can only be applied to: jpeg, jpg, gif, png images ");
94
            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...
95
    }
96
}