9
9
namespace Ajaxray \PHPWatermark ;
10
10
11
11
// https://www.sitepoint.com/adding-text-watermarks-with-imagick/
12
- // https ://www.sitepoint.com/watermarking-images/
12
+ // http ://www.imagemagick.org/Usage/annotating/#watermarking
13
13
14
14
class Watermark
15
15
{
@@ -26,14 +26,17 @@ class Watermark
26
26
27
27
28
28
// @TODO : Option to change style
29
- const PRINT_STYLE_DARK = 1 ;
30
- const PRINT_STYLE_LIGHT = 2 ;
31
- const PRINT_STYLE_BEVEL = 3 ;
29
+ const IMG_STYLE_COLORLESS = 1 ;
30
+ const IMG_STYLE_DISSOLVE = 2 ;
31
+ const TEXT_STYLE_DARK = 1 ;
32
+ const TEXT_STYLE_LIGHT = 2 ;
33
+ const TEXT_STYLE_BEVEL = 3 ;
32
34
33
35
private $ position = 'Center ' ;
34
- private $ offsetX = 5 ;
35
- private $ offsetY = 5 ;
36
+ private $ offsetX = 0 ;
37
+ private $ offsetY = 0 ;
36
38
private $ tiled = false ;
39
+ private $ tileSize = ['100 ' , '100 ' ];
37
40
38
41
/**
39
42
* Font name. Should be one of the list displayed by `convert -list font` command
@@ -44,7 +47,6 @@ class Watermark
44
47
private $ fontSize = 12 ;
45
48
private $ opacity = 0.4 ;
46
49
private $ rotate = 0 ;
47
- private $ style = self ::PRINT_STYLE_BEVEL ;
48
50
private $ source ;
49
51
50
52
/**
@@ -58,19 +60,32 @@ public function __construct($source)
58
60
return $ this ;
59
61
}
60
62
61
- public function withText ($ text , $ destination )
63
+ public function withText ($ text , $ writeTo = null )
62
64
{
65
+ $ destination = $ writeTo ?: $ this ->source ;
63
66
$ this ->ensureExists ($ this ->source );
64
- $ this ->ensureWritable (dirname ($ destination ));
67
+ $ this ->ensureWritable (( $ writeTo ? dirname ($ destination ) : $ destination ));
65
68
66
69
exec ($ this ->buildTextMarkCommand ($ text , $ destination ), $ output , $ returnCode );
67
70
return (empty ($ output ) && $ returnCode == 0 );
68
71
}
69
72
73
+ public function withImage ($ marker , $ writeTo = null )
74
+ {
75
+ $ destination = $ writeTo ?: $ this ->source ;
76
+ $ this ->ensureExists ($ this ->source );
77
+ $ this ->ensureExists ($ marker );
78
+ $ this ->ensureWritable (($ writeTo ? dirname ($ destination ) : $ destination ));
79
+
80
+ exec ($ this ->buildImageMarkCommand ($ marker , $ destination ), $ output , $ returnCode );
81
+ return (empty ($ output ) && $ returnCode == 0 );
82
+ }
83
+
70
84
public function buildTextMarkCommand ($ text , $ destination )
71
85
{
72
- $ text = escapeshellarg ( $ text );
86
+ $ source = $ this -> getSource ( );
73
87
$ destination = escapeshellarg ($ destination );
88
+ $ text = escapeshellarg ($ text );
74
89
75
90
$ anchor = 'gravity ' . $ this ->getPosition ();
76
91
$ rotate = ($ this ->getRotate () == '0 ' )? '' : "rotate {$ this ->getRotate ()}" ;
@@ -83,16 +98,38 @@ public function buildTextMarkCommand($text, $destination)
83
98
$ offsetLight = "{$ offset [0 ]}, {$ offset [1 ]}" ;
84
99
$ offsetDark = ($ offset [0 ] + 1 ) .', ' . ($ offset [1 ] + 1 );
85
100
101
+ $ draw = " -draw \"$ rotate $ anchor $ colorLight text $ offsetLight $ text $ colorDark text $ offsetDark $ text \" " ;
102
+
103
+ // @TODO : Fix issue with single quote
86
104
if ($ this ->isTiled ()) {
87
- $ command = "convert -size 140x80 xc:none $ font - $ colorLight - $ anchor -draw \"$ rotate text 10,10 $ text \" miff:- " ;
88
- $ command .= " | composite -tile - {$ this ->source } $ destination " ;
105
+ $ size = "-size " . implode ('x ' , $ this ->getTileSize ());
106
+ $ command = "convert $ size xc:none $ font - $ anchor $ draw miff:- " ;
107
+ $ command .= " | composite -tile - $ source $ destination " ;
89
108
} else {
90
- $ command = "convert { $ this -> getSource ()} $ font -draw \" $ rotate $ anchor $ colorLight text $ offsetLight $ text $ colorDark text $ offsetDark $ text \" $ destination " ;
109
+ $ command = "convert $ source $ font $ draw $ destination " ;
91
110
}
92
111
93
112
return $ command ;
94
113
}
95
114
115
+ public function buildImageMarkCommand ($ marker , $ destination )
116
+ {
117
+ $ source = $ this ->getSource ();
118
+ $ destination = escapeshellarg ($ destination );
119
+ $ marker = escapeshellarg ($ marker );
120
+
121
+ $ anchor = 'gravity ' . $ this ->getPosition ();
122
+ $ rotate = ($ this ->getRotate () == '0 ' )? '' : "rotate {$ this ->getRotate ()}" ;
123
+
124
+ $ offsetArr = $ this ->getOffset ();
125
+ $ offset = "geometry + {$ offsetArr [0 ]}+ {$ offsetArr [1 ]}" ;
126
+ $ tile = $ this ->isTiled () ? '-tile ' : '' ;
127
+ //$opacity = 'dissolve '. ($this->getOpacity() * 100) .'%';
128
+ $ opacity = 'watermark ' . ($ this ->getOpacity () * 100 );
129
+
130
+ // @TODO : Gap/offset between image tiles
131
+ return "composite - $ anchor - $ offset - $ rotate - $ opacity $ tile $ marker $ source $ destination " ;
132
+ }
96
133
97
134
private function ensureExists ($ filePath )
98
135
{
@@ -106,7 +143,7 @@ private function ensureExists($filePath)
106
143
private function ensureWritable ($ dirPath )
107
144
{
108
145
if (! is_writable ($ dirPath )) {
109
- $ message = "The specified destination directory $ dirPath is not writable! " ;
146
+ $ message = "The specified destination $ dirPath is not writable! " ;
110
147
throw new \RuntimeException ($ message );
111
148
// @TODO : Create DestNotWritableException
112
149
}
@@ -192,6 +229,27 @@ public function setTiled($tiled = true)
192
229
return $ this ;
193
230
}
194
231
232
+ /**
233
+ * @param $width
234
+ * @param $height
235
+ *
236
+ * @return Watermark
237
+ */
238
+ public function setTileSize ($ width , $ height )
239
+ {
240
+ $ this ->tileSize = [intval ($ width ), intval ($ height )];
241
+
242
+ return $ this ;
243
+ }
244
+
245
+ /**
246
+ * @return array
247
+ */
248
+ public function getTileSize ()
249
+ {
250
+ return $ this ->tileSize ;
251
+ }
252
+
195
253
/**
196
254
* @return string
197
255
*/
0 commit comments