Passed
Push — master ( ef3387...d0a4bc )
by Anthony
07:14
created

Watermark::createWatermark()   F

Complexity

Conditions 32
Paths 5026

Size

Total Lines 146
Code Lines 96

Duplication

Lines 84
Ratio 57.53 %

Importance

Changes 0
Metric Value
cc 32
eloc 96
nc 5026
nop 6
dl 84
loc 146
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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:

1
<?php
2
/*
3
* CKFinder
4
* ========
5
* http://cksource.com/ckfinder
6
* Copyright (C) 2007-2013, CKSource - Frederico Knabben. All rights reserved.
7
*
8
* The software, this file and its contents are subject to the CKFinder
9
* License. Please read the license.txt file before using, installing, copying,
10
* modifying or distribute this file or part of its contents. The contents of
11
* this file is part of the Source Code of CKFinder.
12
*
13
* CKFinder extension: adds watermark to uploaded images.
14
*/
15
16
class Watermark
17
{
18
    function onAfterFileUpload($currentFolder, $uploadedFile, $sFilePath)
19
    {
20
        global $config;
21
        $watermarkSettings = $config['Plugin_Watermark'];
22
23
        $this->createWatermark($sFilePath, $watermarkSettings['source'], $watermarkSettings['marginRight'],
24
            $watermarkSettings['marginBottom'], $watermarkSettings['quality'], $watermarkSettings['transparency']);
25
26
        return true;
27
    }
28
29
    function createWatermark($sourceFile, $watermarkFile, $marginLeft = 5, $marginBottom = 5, $quality = 90, $transparency = 100)
30
    {
31
        if (!file_exists($watermarkFile)) {
32
            $watermarkFile = dirname(__FILE__) . "/" . $watermarkFile;
33
        }
34
        if (!file_exists($watermarkFile)) {
35
            return false;
36
        }
37
38
        $watermarkImageAttr = @getimagesize($watermarkFile);
39
        $sourceImageAttr = @getimagesize($sourceFile);
40
        if ($sourceImageAttr === false || $watermarkImageAttr === false) {
41
            return false;
42
        }
43
44
        switch ($watermarkImageAttr['mime'])
45
        {
46
            case 'image/gif':
47
                {
48
                    if (@imagetypes() & IMG_GIF) {
49
                        $oWatermarkImage = @imagecreatefromgif($watermarkFile);
50
                    } else {
51
                        $ermsg = 'GIF images are not supported';
52
                    }
53
                }
54
                break;
55
            case 'image/jpeg':
56
                {
57
                    if (@imagetypes() & IMG_JPG) {
58
                        $oWatermarkImage = @imagecreatefromjpeg($watermarkFile) ;
59
                    } else {
60
                        $ermsg = 'JPEG images are not supported';
61
                    }
62
                }
63
                break;
64
            case 'image/png':
65
                {
66
                    if (@imagetypes() & IMG_PNG) {
67
                        $oWatermarkImage = @imagecreatefrompng($watermarkFile) ;
68
                    } else {
69
                        $ermsg = 'PNG images are not supported';
70
                    }
71
                }
72
                break;
73
            case 'image/wbmp':
74
                {
75
                    if (@imagetypes() & IMG_WBMP) {
76
                        $oWatermarkImage = @imagecreatefromwbmp($watermarkFile);
77
                    } else {
78
                        $ermsg = 'WBMP images are not supported';
79
                    }
80
                }
81
                break;
82
            default:
83
                $ermsg = $watermarkImageAttr['mime'].' images are not supported';
84
                break;
85
        }
86
87
        switch ($sourceImageAttr['mime'])
88
        {
89
            case 'image/gif':
90
                {
91
                    if (@imagetypes() & IMG_GIF) {
92
                        $oImage = @imagecreatefromgif($sourceFile);
93
                    } else {
94
                        $ermsg = 'GIF images are not supported';
95
                    }
96
                }
97
                break;
98
            case 'image/jpeg':
99
                {
100
                    if (@imagetypes() & IMG_JPG) {
101
                        $oImage = @imagecreatefromjpeg($sourceFile) ;
102
                    } else {
103
                        $ermsg = 'JPEG images are not supported';
104
                    }
105
                }
106
                break;
107
            case 'image/png':
108
                {
109
                    if (@imagetypes() & IMG_PNG) {
110
                        $oImage = @imagecreatefrompng($sourceFile) ;
111
                    } else {
112
                        $ermsg = 'PNG images are not supported';
113
                    }
114
                }
115
                break;
116
            case 'image/wbmp':
117
                {
118
                    if (@imagetypes() & IMG_WBMP) {
119
                        $oImage = @imagecreatefromwbmp($sourceFile);
120
                    } else {
121
                        $ermsg = 'WBMP images are not supported';
122
                    }
123
                }
124
                break;
125
            default:
126
                $ermsg = $sourceImageAttr['mime'].' images are not supported';
127
                break;
128
        }
129
130
        if (isset($ermsg) || false === $oImage || false === $oWatermarkImage) {
131
            return false;
132
        }
133
134
        $watermark_width = $watermarkImageAttr[0];
135
        $watermark_height = $watermarkImageAttr[1];
136
        $dest_x = $sourceImageAttr[0] - $watermark_width - $marginLeft;
137
        $dest_y = $sourceImageAttr[1] - $watermark_height - $marginBottom;
138
139
        if ( $sourceImageAttr['mime'] == 'image/png')
140
        {
141
            if(function_exists('imagesavealpha') && function_exists('imagecolorallocatealpha') )
142
            {
143
                 $bg = imagecolorallocatealpha($oImage, 255, 255, 255, 127); // (PHP 4 >= 4.3.2, PHP 5)
144
                 imagefill($oImage, 0, 0 , $bg);
145
                 imagealphablending($oImage, false);
146
                 imagesavealpha($oImage, true);  // (PHP 4 >= 4.3.2, PHP 5)
147
            }
148
        }
149
        if ($watermarkImageAttr['mime'] == 'image/png') {
150
            imagecopy($oImage, $oWatermarkImage, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
151
        }
152
        else {
153
            imagecopymerge($oImage, $oWatermarkImage, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $transparency);
154
        }
155
156
        switch ($sourceImageAttr['mime'])
157
        {
158
            case 'image/gif':
159
                imagegif($oImage, $sourceFile);
160
                break;
161
            case 'image/jpeg':
162
                imagejpeg($oImage, $sourceFile, $quality);
163
                break;
164
            case 'image/png':
165
                imagepng($oImage, $sourceFile);
166
                break;
167
            case 'image/wbmp':
168
                imagewbmp($oImage, $sourceFile);
169
                break;
170
        }
171
172
        imageDestroy($oImage);
173
        imageDestroy($oWatermarkImage);
174
    }
175
}
176
177
$watermark = new Watermark();
178
$config['Hooks']['AfterFileUpload'][] = array($watermark, 'onAfterFileUpload');
179
if (empty($config['Plugin_Watermark']))
180
{
181
    $config['Plugin_Watermark'] = array(
182
        "source" => "logo.gif",
183
        "marginRight" => 5,
184
        "marginBottom" => 5,
185
        "quality" => 90,
186
        "transparency" => 80,
187
    );
188
}
189