Conditions | 32 |
Paths | 5026 |
Total Lines | 146 |
Code Lines | 96 |
Lines | 84 |
Ratio | 57.53 % |
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 |
||
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 | } |
||
189 |