Easy Watermark - Version 0.1.1

Version Description

  • offset can be now also a percentage, not only pixel value
  • changed code structure
    • separete class responsible only for watermarking (can be used alone)
Download this release

Release Info

Developer szaleq
Plugin Icon Easy Watermark
Version 0.1.1
Comparing to
See all releases

Code changes from version 0.1 to 0.1.1

EasyWatermark.php ADDED
@@ -0,0 +1,589 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package Easy Watermark
4
+ * @version 1.0
5
+ * @license GPL
6
+ * @author Wojtek Szałkiewicz
7
+ * @author url http://szalkiewicz.pl
8
+ * @email wojtek@szalkiewicz.pl
9
+ *
10
+ * EasyWatermark can simply add watermark to any image using GD extension.
11
+ * It supports gif and png with alpha chanel and jpg formats.
12
+ * Watermark location can be simply defined with position (top, middle, bottom, left, center, right)
13
+ * and offset (in pixels or percentage) counted from given position
14
+ * (e.g. position_x = center, offset_x = -200; so it's 200 pixels left from centered position).
15
+ * Mime types (see: setImageMime, setWatermarkMime, setOutputMime) can be only file formats (e.g. 'png')
16
+ * or full mime types ('image/jpeg') like in wordpress plugin (http://wordpress.org/extend/plugins/easy-watermark)
17
+ * where mime types are taken directly from WP database.
18
+ *
19
+ * Examples:
20
+ * $ew = new EasyWatermark(array(
21
+ * 'position_x' => 'left', // can be: left, lft, center, ctr, right, rgt or numeric (respectively 1, 2, 3)
22
+ * 'position_y' => 'bottom', // can be: top, middle, mdl, bottom, btm (respectively 1, 2, 3)
23
+ * 'offset_x' => 100, // numeric value for pixels or string ('100px', '20%')
24
+ * 'offset_y' => '10%', // numeric value for pixels or string ('100px', '20%')
25
+ * 'opacity' => 20 // percentage opacit (0 - non visible, 100 - fully visible)
26
+ * ));
27
+ *
28
+ * $ew->setImagePath('some/path/to/img.jpg')
29
+ * ->setWatermarkPath('path/to/watermark.png')
30
+ * ->printOutput(); // returns image with proper header
31
+ *
32
+ * You can also specify custom output format, and save it as file using saveOutput method.
33
+ * TODO:
34
+ * - watermark scaling
35
+ * - adding text watermark
36
+ * Any other ideas, pleas contact me.
37
+ */
38
+
39
+ class EasyWatermark
40
+ {
41
+ /**
42
+ * @var array default settings
43
+ */
44
+ private $defaultSettings = array(
45
+ 'position_x' => 2,
46
+ 'position_y' => 2,
47
+ 'offset_x' => 0,
48
+ 'offset_y' => 0,
49
+ 'opacity' => 100
50
+ );
51
+
52
+ /**
53
+ * @var array settings
54
+ */
55
+ private $settings = array();
56
+
57
+ /**
58
+ * @var string path to image file
59
+ */
60
+ private $imagePath = '';
61
+
62
+ /**
63
+ * @var string image mime type
64
+ */
65
+ private $imageMime = '';
66
+
67
+ /**
68
+ * @var string path to watermark file
69
+ */
70
+ private $watermarkPath = '';
71
+
72
+ /**
73
+ * @var string watermark mime type
74
+ */
75
+ private $watermarkMime = '';
76
+
77
+ /**
78
+ * @var string output file path
79
+ */
80
+ private $outputFile = '';
81
+
82
+ /**
83
+ * @var string output file mime type
84
+ */
85
+ private $outputMime = '';
86
+
87
+ /**
88
+ * @var string error message
89
+ */
90
+ private $error = '';
91
+
92
+ /**
93
+ * Creates EasyWatermark object
94
+ *
95
+ * @param array settings
96
+ */
97
+ public function __construct($settings = array()){
98
+ $this->set($settings);
99
+ }
100
+
101
+ /**
102
+ * Sets all needed parameters (settings and values of some private fields)
103
+ *
104
+ * @see $defaultSettings
105
+ * @chainable
106
+ * @param string settings key
107
+ * @param string value
108
+ * @return object $this
109
+ */
110
+ public function set($key, $val = null){
111
+ if(is_array($key)){
112
+ // act recursive
113
+ foreach($key as $k => $v){
114
+ $this->set($k, $v);
115
+ }
116
+ return $this;
117
+ }
118
+
119
+ switch($key){
120
+ case 'image_path':
121
+ $this->setImagePath($val);
122
+ break;
123
+ case 'image_mime':
124
+ $this->setImageMime($val);
125
+ break;
126
+ case 'watermark_path':
127
+ $this->setWatermarkPath($val);
128
+ break;
129
+ case 'watermark_mime':
130
+ $this->setWatermarkMime($val);
131
+ break;
132
+ case 'output_file':
133
+ $this->setOutputFile($val);
134
+ break;
135
+ case 'output_mime':
136
+ $this->setOutputMime($val);
137
+ break;
138
+ default:
139
+ $this->settings[$key] = $val;
140
+ break;
141
+ }
142
+
143
+ return $this;
144
+ }
145
+
146
+ /**
147
+ * Sets input image path
148
+ *
149
+ * @chainable
150
+ * @param string image path
151
+ * @return object $this
152
+ */
153
+ public function setImagePath($path){
154
+ $this->imagePath = $path;
155
+
156
+ return $this;
157
+ }
158
+
159
+ /**
160
+ * Sets input image mime type
161
+ *
162
+ * @chainable
163
+ * @param string image mime
164
+ * @return object $this
165
+ */
166
+ public function setImageMime($mime){
167
+ $this->imageMime = $mime;
168
+
169
+ return $this;
170
+ }
171
+
172
+ /**
173
+ * Sets watermark path
174
+ *
175
+ * @chainable
176
+ * @param string watermark path
177
+ * @return object $this
178
+ */
179
+ public function setWatermarkPath($path){
180
+ $this->watermarkPath = $path;
181
+
182
+ return $this;
183
+ }
184
+
185
+ /**
186
+ * Sets watermark mime type
187
+ *
188
+ * @chainable
189
+ * @param string watermark mime
190
+ * @return object $this
191
+ */
192
+ public function setWatermarkMime($mime){
193
+ $this->watermarkMime = $mime;
194
+
195
+ return $this;
196
+ }
197
+
198
+ /**
199
+ * Sets output file path
200
+ *
201
+ * @chainable
202
+ * @param string output file path
203
+ * @return object $this
204
+ */
205
+ public function setOutputFile($path){
206
+ $this->outputFile = $path;
207
+
208
+ return $this;
209
+ }
210
+
211
+ /**
212
+ * Sets output file mime type
213
+ *
214
+ * @chainable
215
+ * @param string output mime
216
+ * @return object $this
217
+ */
218
+ public function setOutputMime($mime){
219
+ $this->outputMime = $mime;
220
+
221
+ return $this;
222
+ }
223
+
224
+ /**
225
+ * Checks if error occurred
226
+ *
227
+ * @return boolean
228
+ */
229
+ public function hasError(){
230
+ return !empty($this->error);
231
+ }
232
+
233
+ /**
234
+ * Gets error message
235
+ *
236
+ * @return string error message
237
+ */
238
+ public function getError(){
239
+ return $this->error;
240
+ }
241
+
242
+ /**
243
+ * Merges settings with defaults,
244
+ * parses offset values to determine if they are pixels or percentage
245
+ *
246
+ * @return array settings
247
+ */
248
+ private function parseSettings(){
249
+ // merge with defaults
250
+ $settings = array_merge($this->defaultSettings, $this->settings);
251
+
252
+ if(strpos($settings['offset_x'], '%') === (strlen($settings['offset_x']) - 1)){
253
+ $settings['offset_x'] = substr($settings['offset_x'], 0, strlen($settings['offset_x']) - 1);
254
+ $settings['offset_x_pc'] = true;
255
+ }
256
+ else {
257
+ if(strpos($settings['offset_x'], 'px') === (strlen($settings['offset_x']) - 2)){
258
+ $settings['offset_x'] = substr($settings['offset_x'], 0, strlen($settings['offset_x']) - 2);
259
+ }
260
+ $settings['offset_x_pc'] = false;
261
+ }
262
+
263
+ if(strpos($settings['offset_y'], '%') === strlen($settings['offset_y']) - 1){
264
+ $settings['offset_y'] = substr($settings['offset_y'], 0, strlen($settings['offset_y']) - 1);
265
+ $settings['offset_y_pc'] = true;
266
+ }
267
+ else {
268
+ if(strpos($settings['offset_y'], 'px') === strlen($settings['offset_x']) - 2){
269
+ $settings['offset_y'] = substr($settings['offset_x'], 0, strlen($settings['offset_y']) - 2);
270
+ }
271
+ $settings['offset_y_pc'] = false;
272
+ }
273
+
274
+ switch($settings['position_x']){
275
+ case 'left':
276
+ case 'lft':
277
+ $settings['position_x'] = 1;
278
+ break;
279
+ case 'center':
280
+ case 'ctr':
281
+ $settings['position_x'] = 2;
282
+ break;
283
+ case 'right':
284
+ case 'rgt':
285
+ $settings['position_x'] = 3;
286
+ break;
287
+ }
288
+
289
+ switch($settings['position_y']){
290
+ case 'top':
291
+ $settings['position_y'] = 1;
292
+ break;
293
+ case 'middle':
294
+ case 'mdl':
295
+ $settings['position_y'] = 2;
296
+ break;
297
+ case 'bottom':
298
+ case 'btm':
299
+ $settings['position_y'] = 3;
300
+ break;
301
+ }
302
+
303
+ return $settings;
304
+ }
305
+
306
+ /**
307
+ * @var resource GD image created from input file
308
+ */
309
+ private $inputImage;
310
+
311
+ /**
312
+ * @var resource GD image created from watermark file
313
+ */
314
+ private $watermarkImage;
315
+
316
+ /**
317
+ * @var resource GD image prepared as output
318
+ */
319
+ private $outputImage;
320
+
321
+ /**
322
+ * @var array allowed image types
323
+ */
324
+ private $allowedTypes = array('jpeg', 'jpg', 'png', 'gif');
325
+
326
+ /**
327
+ * Creates output image with watermarm
328
+ *
329
+ * @chainable
330
+ * @param string input image path
331
+ * @param string input image mime
332
+ * @param string watermark path
333
+ * @param string watermark mime
334
+ * @param string output file path
335
+ * @param string output file mime
336
+ * @return boolean
337
+ */
338
+ public function create($imagePath = null, $imageMime = null,
339
+ $watermarkPath = null, $watermarkMime = null){
340
+
341
+ if(empty($imagePath)) $imagePath = $this->imagePath;
342
+ if(empty($imageMime)) $imageMime = $this->imageMime;
343
+ if(empty($watermarkPath)) $watermarkPath = $this->watermarkPath;
344
+ if(empty($watermarkMime)) $watermarkMime = $this->watermarkMime;
345
+
346
+ if(!empty($imagePath) && !empty($watermarkPath)){
347
+ if($imagePath != $watermarkPath){
348
+ // Create GD image resources
349
+ list($this->inputImage, $inputType) = $this->createImage($imagePath, $imageMime);
350
+ list($this->watermarkImage, $wmType) = $this->createImage($watermarkPath, $watermarkMime);
351
+
352
+ if(!$this->inputImage || !$this->watermarkImage)
353
+ return false;
354
+
355
+ // Prepare settings array
356
+ $settings = $this->parseSettings();
357
+
358
+ // Get image dimensions
359
+ $imageWidth = imagesx($this->inputImage);
360
+ $imageHeight = imagesy($this->inputImage);
361
+
362
+ // Get watermark dimensions
363
+ $watermarkWidth = imagesx($this->watermarkImage);
364
+ $watermarkHeight = imagesy($this->watermarkImage);
365
+
366
+ // Compute watermark offset
367
+ $offsetX = $this->computeOffset($settings['position_x'], $settings['offset_x'],
368
+ $imageWidth, $watermarkWidth, $settings['offset_x_pc']);
369
+ $offsetY = $this->computeOffset($settings['position_y'], $settings['offset_y'],
370
+ $imageHeight, $watermarkHeight, $settings['offset_y_pc']);
371
+
372
+ // Create blank image
373
+ $this->outputImage = imagecreatetruecolor($imageWidth, $imageHeight);
374
+
375
+ imagecopy($this->outputImage, $this->inputImage,
376
+ 0, 0, 0, 0, $imageWidth, $imageHeight
377
+ );
378
+
379
+ // Prepare params for copying function
380
+ $params = array($this->outputImage, $this->watermarkImage,
381
+ $offsetX, $offsetY,
382
+ 0, 0,
383
+ $watermarkWidth, $watermarkHeight
384
+ );
385
+ if($wmType == 'png' && $this->isAlphaPng($watermarkPath)){
386
+ // Watermark is PNG with alpha channel, use imagecopy
387
+ $func = 'imagecopy';
388
+ }
389
+ else {
390
+ // Use imagecopymerge with opacity param for other images
391
+ $func = 'imagecopymerge';
392
+ $params[] = $settings['opacity'];
393
+ }
394
+
395
+ // Copy watermark to output image
396
+ call_user_func_array($func, $params);
397
+
398
+ return true;
399
+ }
400
+ else {
401
+ $this->error = 'same image and watermark paths';
402
+ }
403
+ }
404
+ else {
405
+ $this->error = empty($imagePath) ? 'empty image path' : 'empty watermark path';
406
+ }
407
+
408
+ return false;
409
+ }
410
+
411
+ /**
412
+ * Prints created image directly to the browser
413
+ *
414
+ * @return boolean
415
+ */
416
+ public function printOutput(){
417
+ return $this->prepareOutput();
418
+ }
419
+
420
+ /**
421
+ * Saves created image
422
+ *
423
+ * @param string filename (required if not set earlier)
424
+ * @return boolean
425
+ */
426
+ public function saveOutput($file = null){
427
+ if(empty($file)) $file = $this->outputFile;
428
+
429
+ if(empty($file)){
430
+ $this->error = 'output file not defined';
431
+ return false;
432
+ }
433
+
434
+ return $this->prepareOutput($file);
435
+ }
436
+
437
+ /**
438
+ * Saves or outputs created image depending on $output param
439
+ *
440
+ * @param string output file
441
+ * @return boolean
442
+ */
443
+ private function prepareOutput($output = null){
444
+ $type = !empty($this->outputMime) ? $this->detectMimeType($this->outputMime) : $this->inputImage[1];
445
+
446
+ if(!$type || !in_array($type, $this->allowedTypes)){
447
+ $this->error = 'invalid output type';
448
+ return false;
449
+ }
450
+
451
+ if($output === null)
452
+ // Return image directly to the browser
453
+ header('Content-Type: image/'.$type);
454
+
455
+ if($type == 'jpg') $type = 'jpeg';
456
+ $func = 'image'.$type;
457
+
458
+ if(!@$func($this->outputImage, $output)){
459
+ $this->error = 'could not create output';
460
+ return false;
461
+ }
462
+
463
+ return true;
464
+ }
465
+
466
+ /**
467
+ * Computes offset
468
+ *
469
+ * @param numeric position (1 - left/top, 2 - center/middle, 3 - right/bottom)
470
+ * @param numeric offset
471
+ * @param numeric image dimension (width or height)
472
+ * @param numeric watermark dimension (width or height)
473
+ * @param boolean is offset percentage?
474
+ * @return numeric computed offset
475
+ */
476
+ private function computeOffset($position, $offset, $imgDim, $wDim, $pc){
477
+ if($pc){
478
+ // Percentage offset
479
+ $offset = round(($offset / 100) * $imgDim);
480
+ }
481
+ switch($position){
482
+ case 1:
483
+ break;
484
+ case 2:
485
+ $offset = (($imgDim - $wDim) / 2) + $offset;
486
+ break;
487
+ case 3:
488
+ $offset = $imgDim - $wDim - $offset;
489
+ break;
490
+ }
491
+
492
+ return (int) $offset;
493
+ }
494
+
495
+ /**
496
+ * Creates GD image based on its type
497
+ *
498
+ * @param string path to file
499
+ * @param string mime type
500
+ * @return string mime type
501
+ */
502
+ private function createImage($filePath, $mime){
503
+ $type = $this->detectMimeType($mime, $filePath);
504
+
505
+ if(!in_array($type, $this->allowedTypes)){
506
+ $this->error = 'invalid mime type in file: '.$filePath;
507
+ return false;
508
+ }
509
+
510
+ if($type == 'jpg') $type = 'jpeg';
511
+ $func = 'imagecreatefrom' . $type;
512
+
513
+ $image = $func($filePath);
514
+ if($type == 'png' && $this->isAlphaPng($filePath)){
515
+ imagealphablending($image, false);
516
+ imagesavealpha($image, true);
517
+ }
518
+
519
+ return array($image, $type);
520
+ }
521
+
522
+ /**
523
+ * Detects mime type using php finfo object and extracts what's needed
524
+ *
525
+ * @param string path to file
526
+ * @param string mime type
527
+ * @return string mime type
528
+ */
529
+ private function detectMimeType($mime, $filePath = null){
530
+ if(empty($mime) && $filePath){
531
+ // Get finfo object to detect mime types
532
+ $finfo = $this->getFinfo();
533
+
534
+ $mime = @$finfo->file($filePath);
535
+ }
536
+ if(!$mime)
537
+ return false;
538
+
539
+ if(strpos($mime, 'image/') === 0)
540
+ $mime = substr($mime, 6);
541
+
542
+ return $mime;
543
+ }
544
+
545
+ /**
546
+ * @var object finfo object
547
+ */
548
+ private $finfo;
549
+
550
+ /**
551
+ * Returns finfo object
552
+ *
553
+ * @return object
554
+ */
555
+ private function getFinfo(){
556
+ if(!($this->finfo instanceof finfo)){
557
+ $this->finfo = $finfo = new finfo(FILEINFO_MIME_TYPE);
558
+ }
559
+
560
+ return $this->finfo;
561
+ }
562
+
563
+ /**
564
+ * Verifies if png file has alpha chanel
565
+ *
566
+ * @param string path to png file
567
+ * @return boolean
568
+ */
569
+ public function isAlphaPng($file){
570
+ /* color type of png image stored at 25 byte:
571
+ * 0 - greyscale
572
+ * 2 - RGB
573
+ * 3 - RGB with palette
574
+ * 4 - greyscale + alpha
575
+ * 6 - RGB + alpha
576
+ */
577
+ $colorByte = ord(@file_get_contents($file, false, null, 25, 1));
578
+ return ($colorByte == 6 || $colorByte == 4);
579
+ }
580
+
581
+ /**
582
+ * cleans up everything when object is destructed
583
+ */
584
+ public function __destruct(){
585
+ if($this->inputImage) imagedestroy($this->inputImage);
586
+ if($this->watermarkImage) imagedestroy($this->watermarkImage);
587
+ if($this->outputImage) imagedestroy($this->outputImage);
588
+ }
589
+ }
EasyWatermarkPlugin.php ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ This file is a part of EasyWatermark Wordpress plugin.
4
+ For license informations, author name etc. see readme.txt
5
+ */
6
+
7
+ /**
8
+ * Main plugin class
9
+ */
10
+
11
+ class EasyWatermarkPlugin
12
+ {
13
+ private static $version = '0.1.1';
14
+
15
+ private $settings;
16
+
17
+ private $messages = array();
18
+
19
+ private $allowedMime = array('image/png', 'image/jpeg', 'image/gif');
20
+
21
+ /**
22
+ * Initiate plugin by creating self object,
23
+ * registers activation and uninstall hooks
24
+ *
25
+ * @return void
26
+ */
27
+ public static function init(){
28
+ register_activation_hook(__FILE__, array(self, 'install'));
29
+ register_uninstall_hook(__FILE__, array(self, 'uninstall'));
30
+
31
+ $version = get_option('easy-watermark-version');
32
+ if(!$version || version_compare($version, self::$version, '<')){
33
+ self::upgrade();
34
+ }
35
+
36
+ new self();
37
+ }
38
+
39
+ /**
40
+ * Loads textdomain for translations,
41
+ * adds wordpress actions and creates EasyWatermarkSettings object
42
+ *
43
+ * @return void
44
+ */
45
+ private function __construct(){
46
+ load_plugin_textdomain('easy-watermark', false, '/easy-watermark/languages');
47
+
48
+ add_action('admin_menu', array($this, 'add_media_page'));
49
+ add_filter('media_row_actions', array($this, 'add_media_row_action'), 10, 3);
50
+ add_filter('attachment_fields_to_edit', array($this, 'add_attachment_field'), 10, 2 );
51
+ add_action('add_attachment', array($this, 'add_watermark_after_upload'));
52
+
53
+ $this->settings = new EasyWatermarkSettings();
54
+ }
55
+
56
+ /**
57
+ * Adds wordpress media page
58
+ *
59
+ * @return void
60
+ */
61
+ public function add_media_page(){
62
+ add_media_page( 'Easy Watermark', 'Easy Watermark', 'upload_files', 'easy-watermark', array($this, 'easy_watermark'));
63
+ }
64
+
65
+ /**
66
+ * Creates 'Add watermark' link for each row in media library
67
+ *
68
+ * @return array
69
+ */
70
+ public function add_media_row_action($actions, $post, $detached){
71
+ if($post->post_mime_type == 'image/jpeg' || $post->post_mime_type == 'image/png' || $post->post_mime_type == 'image/gif'){
72
+ // Add link if it's supported image type
73
+ $actions['add_watermark'] = '<a href="' . wp_nonce_url(admin_url('upload.php?page=easy-watermark&attachment_id='.$post->ID)) . '">'.__('Add watermark', 'easy-watermark').'</a>';
74
+ }
75
+
76
+ return $actions;
77
+ }
78
+
79
+ /**
80
+ * Creates 'Add watermark' button in media edit view
81
+ *
82
+ * @return array
83
+ */
84
+ public function add_attachment_field($form_fields, $post){
85
+ if($post->post_mime_type == 'image/jpeg' || $post->post_mime_type == 'image/png' || $post->post_mime_type == 'image/gif'){
86
+ // Add link if it's supported image type
87
+ $form_fields = array_reverse($form_fields);
88
+ $form_fields['easy-watermark'] = array(
89
+ 'label' => '<a href="'.wp_nonce_url(admin_url('upload.php?page=easy-watermark&attachment_id='.$post->ID)).'" class="button-secondary">'.__('Add watermark', 'easy-watermark').'</a>',
90
+ 'input' => 'html',
91
+ 'html' => ' '
92
+ );
93
+ }
94
+
95
+ return array_reverse($form_fields);
96
+ }
97
+
98
+ /**
99
+ * Creates 'Add watermark' button in media edit view
100
+ *
101
+ * @return array
102
+ */
103
+ public function add_watermark_after_upload($id){
104
+ if($this->settings->get('auto_add')){
105
+ $this->add_watermark($id);
106
+ }
107
+ }
108
+
109
+ private function add_watermark($id){
110
+ $watermark = $this->settings->get('image');
111
+ if($watermark['path'] != ''){
112
+ $post = get_post($id);
113
+ $filepath = get_attached_file($id);
114
+
115
+ if(!in_array($post->post_mime_type, $this->allowedMime)){
116
+ $this->add_error('Invalid mime type.');
117
+ return false;
118
+ }
119
+
120
+ return $this->create_watermark($filepath, $post->post_mime_type);
121
+ }
122
+ else {
123
+ $this->add_error('No watermark image selected.');
124
+ return false;
125
+ }
126
+ }
127
+
128
+ private function watermark_all(){
129
+ global $wpdb;
130
+
131
+ $images = $wpdb->get_results("
132
+ SELECT ID, post_mime_type
133
+ FROM $wpdb->posts
134
+ WHERE post_type = 'attachment'
135
+ AND post_mime_type LIKE 'image/%'
136
+ ");
137
+
138
+ $output = '';
139
+ $watermark = $this->settings->get('image');
140
+ if($images && $watermark['path'] != ''){
141
+ foreach($images as $img){
142
+ $filepath = get_attached_file($img->ID);
143
+ if(!in_array($img->post_mime_type, $this->allowedMime)){
144
+ $output .= $filepath.': not supported mime type. Skipping...<br/>';
145
+ continue;
146
+ }
147
+
148
+ if($this->create_watermark($filepath, $img->post_mime_type)){
149
+ $output .= $filepath.': watermark added.<br/>';
150
+ }
151
+ }
152
+ }
153
+
154
+ $output = '<p>'.$output.'</p>';
155
+
156
+ return $output;
157
+ }
158
+
159
+ private function create_watermark($imageFile, $imageType){
160
+ $ew = $this->getEasyWatermark();
161
+ $ew->setImagePath($imageFile)
162
+ ->setImageMime($imageType)
163
+ ->setOutputFile($imageFile)
164
+ ->setOutputMime($imageType);
165
+
166
+ if(!$ew->create() || !$ew->saveOutput()){
167
+ $this->add_error($ew->getError());
168
+ return false;
169
+ }
170
+
171
+ return true;
172
+ }
173
+
174
+ private $ew;
175
+
176
+ public function getEasyWatermark(){
177
+ if(!($ew instanceof EasyWatermark)){
178
+ $settings = $this->settings->get('image');
179
+ $ew = new EasyWatermark();
180
+ $ew->set($settings)
181
+ ->set('watermark_path', $settings['path'])
182
+ ->set('watermark_mime', $settings['mime']);
183
+ }
184
+
185
+ return $ew;
186
+ }
187
+
188
+ public function easy_watermark(){
189
+ ?>
190
+ <div class="wrap easy-watermark">
191
+ <h2><?php _e('Easy Watermark', 'easy-watermark'); ?></h2>
192
+ <?php
193
+ if(isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'])){
194
+ if(isset($_GET['attachment_id'])){
195
+ if($this->add_watermark($_GET['attachment_id'])) :
196
+ ?>
197
+ <div id="message" class="updated below-h2">
198
+ <p><?php _e('Watermark successfully added.', 'easy-watermark'); ?> <a href="javascript:window.history.go(-1)"><?php _e('Go back', 'easy-watermark'); ?></a> or <a href="<?php echo get_edit_post_link($_GET['attachment_id']); ?>"><?php _e('go to edit page', 'easy-watermark'); ?></a></p>
199
+ </div>
200
+ <?php
201
+ else :
202
+ foreach($this->messages as $msg) :
203
+ ?>
204
+ <div id="message" class="<?php echo $msg[0]; ?> below-h2">
205
+ <p><?php _e($msg[1], 'easy-watermark'); ?></p>
206
+ </div>
207
+ <a href="javascript:window.history.go(-1)"><?php _e('Go back', 'easy-watermark'); ?></a> or <a href="<?php echo get_edit_post_link($_GET['attachment_id']); ?>"><?php _e('go to edit page', 'easy-watermark'); ?></a>
208
+ <?php
209
+ endforeach;
210
+ endif;
211
+ }
212
+ elseif(isset($_GET['watermark_all'])){
213
+ if($output = $this->watermark_all()) :
214
+ ?>
215
+ <div id="message" class="updated below-h2">
216
+ <p><?php _e('Watermark successfully added.', 'easy-watermark'); ?> <a href="<?php echo admin_url('upload.php') ?>"><?php _e('Go to Media Library', 'easy-watermark'); ?></a></p>
217
+ </div>
218
+ <?
219
+ echo $output;
220
+ endif;
221
+ }
222
+ }
223
+ else {
224
+ ?>
225
+ <br/><a class="button-primary" href="<?php echo wp_nonce_url(admin_url('/upload.php?page=easy-watermark&watermark_all=1')); ?>"><?php _e('Add watermark to all images', 'easy-watermark'); ?></a><p class="description"><?php _e('Be carefull with that option. If some images alredy has watermark, it will be added though.'); ?></p>
226
+ <?php
227
+ $settings = $this->settings->get('image');
228
+ }
229
+ ?>
230
+ </div>
231
+ <?php
232
+ }
233
+
234
+ public static function install(){
235
+ add_option('easy-watermark-settings', EasyWatermarkSettings::getDefaults());
236
+ update_option('easy-watermark-version', self::$version);
237
+ }
238
+
239
+ public static function uninstall(){
240
+ delete_option('easy-watermark-settings');
241
+ }
242
+
243
+ public static function upgrade(){
244
+ $oldSettings = get_option('easy-watermark-settings');
245
+ $settings = EasyWatermarkSettings::getDefaults();
246
+
247
+ $settings['auto_add'] = $oldSettings['auto_add'];
248
+ $settings['image_types'] = $oldSettings['image_types'];
249
+ $settings['image']['position_x'] = $oldSettings['image']['position-horizontal'];
250
+ $settings['image']['position_y'] = $oldSettings['image']['position-vert'];
251
+ $settings['image']['offset_x'] = $oldSettings['image']['offset-horizontal'];
252
+ $settings['image']['offset_y'] = $oldSettings['image']['offset-vert'];
253
+ $settings['image']['opacity'] = $oldSettings['image']['alpha'];
254
+
255
+ foreach($oldSettings['image'] as $key => $val){
256
+ if(array_key_exists($key, $settings['image']))
257
+ $settings['image'][$key] = $val;
258
+ }
259
+
260
+ update_option('easy-watermark-settings', $settings);
261
+ update_option('easy-watermark-version', self::$version);
262
+ }
263
+
264
+ private function add_error($msg){
265
+ $this->messages[] = array('error', $msg);
266
+ }
267
+
268
+ private function add_info($msg){
269
+ $this->messages[] = array('update', $msg);
270
+ }
271
+ }
easy-watermark-settings.php → EasyWatermarkSettings.php RENAMED
@@ -1,7 +1,5 @@
1
  <?php
2
 
3
- if(!class_exists('EasyWatermarkSettings')) :
4
-
5
  class EasyWatermarkSettings
6
  {
7
  private $tabs = array();
@@ -16,11 +14,11 @@ class EasyWatermarkSettings
16
  'path' => null,
17
  'id' => null,
18
  'mime' => null,
19
- 'position-vert' => 'btm',
20
- 'offset-vert' => 100,
21
- 'position-horizontal' => 'lft',
22
- 'offset-horizontal' => 100,
23
- 'alpha' => 100,
24
  )
25
  );
26
 
@@ -43,7 +41,7 @@ class EasyWatermarkSettings
43
  }
44
 
45
  public function add_options_page(){
46
- add_options_page( 'Easy Watermark Settings', 'Easy Watermark Settings', 'manage_options', 'easy-watermark-settings', array($this, 'settings_page'));
47
  }
48
 
49
  public function register_settings(){
@@ -89,28 +87,28 @@ class EasyWatermarkSettings
89
  }
90
 
91
  public function settings_page(){
92
- wp_enqueue_script('ew-interface', plugin_dir_url(dirname(__FILE__) . '/easy-watermark.php') . 'js/interface.js');
93
  if(function_exists('wp_enqueue_media')){
94
  // load new media manager (since wp 3.5)
95
  wp_enqueue_media();
96
- wp_enqueue_script('ew-media-libraby', plugin_dir_url(dirname(__FILE__) . '/easy-watermark.php') . 'js/media-library.js');
97
  }
98
  else {
99
  // load old-style thiskbox
100
  wp_enqueue_script('thickbox');
101
  wp_enqueue_style('thickbox');
102
- wp_enqueue_script('ew-media-libraby', plugin_dir_url(dirname(__FILE__) . '/easy-watermark.php') . 'js/old-media-library.js');
103
  }
104
 
105
  $options = $this->options;
106
- include dirname(__FILE__) . '/settings-form.php';
107
  }
108
 
109
  function settings_link($links, $file){
110
  static $this_plugin;
111
 
112
  if (!$this_plugin) {
113
- $this_plugin = plugin_basename(dirname(__FILE__) . '/easy-watermark.php');
114
  }
115
  if ($file == $this_plugin) {
116
  $settings_link = '<a href="options-general.php?page=easy-watermark-settings">'.__('Settings').'</a>';
@@ -119,5 +117,3 @@ class EasyWatermarkSettings
119
  return $links;
120
  }
121
  }
122
-
123
- endif;
1
  <?php
2
 
 
 
3
  class EasyWatermarkSettings
4
  {
5
  private $tabs = array();
14
  'path' => null,
15
  'id' => null,
16
  'mime' => null,
17
+ 'position_x' => 'ctr',
18
+ 'position_y' => 'mdl',
19
+ 'offset_x' => 100,
20
+ 'offset_y' => 100,
21
+ 'opacity' => 100,
22
  )
23
  );
24
 
41
  }
42
 
43
  public function add_options_page(){
44
+ add_options_page(__('Easy Watermark Settings', 'easy-watermark'), __('Easy Watermark Settings', 'easy-watermark'), 'manage_options', 'easy-watermark-settings', array($this, 'settings_page'));
45
  }
46
 
47
  public function register_settings(){
87
  }
88
 
89
  public function settings_page(){
90
+ wp_enqueue_script('ew-interface', plugin_dir_url(dirname(__File__) . '/index.php') . 'js/interface.js');
91
  if(function_exists('wp_enqueue_media')){
92
  // load new media manager (since wp 3.5)
93
  wp_enqueue_media();
94
+ wp_enqueue_script('ew-media-libraby', plugin_dir_url(dirname(__File__) . '/index.php') . 'js/media-library.js');
95
  }
96
  else {
97
  // load old-style thiskbox
98
  wp_enqueue_script('thickbox');
99
  wp_enqueue_style('thickbox');
100
+ wp_enqueue_script('ew-media-libraby', plugin_dir_url(dirname(__File__) . '/index.php') . 'js/old-media-library.js');
101
  }
102
 
103
  $options = $this->options;
104
+ include dirname(__File__) . '/settings-form.php';
105
  }
106
 
107
  function settings_link($links, $file){
108
  static $this_plugin;
109
 
110
  if (!$this_plugin) {
111
+ $this_plugin = plugin_basename(dirname(__FILE__) . '/index.php');
112
  }
113
  if ($file == $this_plugin) {
114
  $settings_link = '<a href="options-general.php?page=easy-watermark-settings">'.__('Settings').'</a>';
117
  return $links;
118
  }
119
  }
 
 
easy-watermark.php DELETED
@@ -1,281 +0,0 @@
1
- <?php
2
- /*
3
- Plugin Name: Easy Watermark
4
- Description: This plugin allows you to add image watermark to every single image or all images from library at once. It can also be configured to automatically watermark new images as they are uploaded. Watermark can be a png, gif (alpha channel supported in both cases) or jpg image. It's also possibile to set watermark opacity (doesn't apply to png with alpha channel).
5
- Version: 0.1
6
- Author: Wojtek Szałkiewicz
7
- Author URI: http://szalkiewicz.pl/
8
- */
9
- include dirname(__FILE__) . '/easy-watermark-settings.php';
10
-
11
- if(!class_exists('EasyWatermark')) :
12
-
13
- class EasyWatermark
14
- {
15
- private $settings;
16
-
17
- private $messages = array();
18
-
19
- public function __construct(){
20
- load_plugin_textdomain('easy-watermark', false, '/easy-watermark/languages');
21
-
22
- add_action('admin_menu', array($this, 'add_media_page'));
23
- add_action('add_attachment', array($this, 'add_watermark_after_upload'));
24
- add_filter('media_row_actions', array($this, 'add_media_row_action'), 10, 3);
25
- add_filter( 'attachment_fields_to_edit', array($this, 'add_attachment_field'), 10, 2 );
26
-
27
- $this->settings = new EasyWatermarkSettings();
28
- }
29
-
30
- public function add_attachment_field($form_fields, $post){
31
- $form_fields = array_reverse($form_fields);
32
- $form_fields['easy-watermark'] = array(
33
- 'label' => '<a href="'.wp_nonce_url(admin_url('upload.php?page=easy-watermark&attachment_id='.$post->ID.'&')).'" class="button-secondary">'.__('Add watermark').'</a>',
34
- 'input' => 'html',
35
- 'html' => ' '
36
- );
37
- return array_reverse($form_fields);
38
- }
39
-
40
- public function add_watermark_after_upload($id){
41
- if($this->settings->get('auto_add')){
42
- $this->add_watermark($id, true);
43
- }
44
- }
45
-
46
- private function add_watermark($id, $mimecheck = false){
47
- $watermark = $this->settings->get('image');
48
- if($watermark['path'] != ''){
49
- $post = get_post($id);
50
- $filepath = get_attached_file($id);
51
-
52
- if(strpos($post->post_mime_type, 'image/') !== 0 || in_array($post->post_mime_type, array('png', 'jpeg', 'gif'))){
53
- $this->add_error('Invalid mime type.');
54
- return false;
55
- }
56
-
57
- $imgMime = substr($post->post_mime_type, 6);
58
- $wmMime = substr($watermark['mime'], 6);
59
-
60
- if($mimecheck && !in_array($imgMime, $this->settings->get('image_types'))){
61
- return false;
62
- }
63
-
64
- return $this->create_watermark($filepath, $imgMime, $watermark['path'], $wmMime, $filepath);
65
- }
66
- else {
67
- $this->add_error('No watermark image selected.');
68
- return false;
69
- }
70
- }
71
-
72
- private function watermark_all(){
73
- global $wpdb;
74
-
75
- $images = $wpdb->get_results("
76
- SELECT ID, post_mime_type
77
- FROM $wpdb->posts
78
- WHERE post_type = 'attachment'
79
- AND post_mime_type LIKE 'image/%'
80
- ");
81
-
82
- $output = '';
83
- $watermark = $this->settings->get('image');
84
- if($images && $watermark['path'] != ''){
85
- $wmMime = substr($watermark['mime'], 6);
86
- foreach($images as $img){
87
- $filepath = get_attached_file($img->ID);
88
- $imgMime = substr($img->post_mime_type, 6);
89
- if(!in_array($imgMime, $this->settings->get('image_types'))){
90
- $output .= $filepath.': wrong mime type. Skipping...<br/>';
91
- continue;
92
- }
93
-
94
- if($this->create_watermark($filepath, $imgMime, $watermark['path'], $wmMime, $filepath)){
95
- $output .= $filepath.': watermark added.<br/>';
96
- }
97
- }
98
- }
99
-
100
- $output = '<p>'.$output.'</p>';
101
-
102
- return $output;
103
- }
104
-
105
- private function create_watermark($imageFile, $imageType, $watermarkFile, $watermarkType, $outputFile){
106
- if($imageFile != $watermarkFile){
107
- $settings = $this->settings->get('image');
108
- $alpha = $settings['alpha'];
109
-
110
- $imgfunc = 'imagecreatefrom' . $imageType;
111
- $watermarkfunc = 'imagecreatefrom' . $watermarkType;
112
-
113
- $image = $imgfunc($imageFile);
114
- if($imageType == 'png'){
115
- imagealphablending($image, false);
116
- imagesavealpha($image, true);
117
- }
118
- $watermark = $watermarkfunc($watermarkFile);
119
-
120
- $imageWidth = imagesx($image);
121
- $imageHeight = imagesy($image);
122
-
123
- $watermarkWidth = imagesx($watermark);
124
- $watermarkHeight = imagesy($watermark);
125
-
126
- $offsetX = 0;
127
- $offsetY = 0;
128
- switch($settings['position-horizontal']){
129
- case 'lft':
130
- $offsetX = $settings['offset-horizontal'];
131
- break;
132
- case 'ctr':
133
- $offsetX = ($imageWidth - $watermarkWidth) / 2;
134
- break;
135
- case 'rgt':
136
- $offsetX = $imageWidth - $watermarkWidth - $settings['offset-horizontal'];
137
- break;
138
- }
139
- switch($settings['position-vert']){
140
- case 'top':
141
- $offsetY = $settings['offset-vert'];
142
- break;
143
- case 'mdl':
144
- $offsetY = ($imageHeight - $watermarkHeight) / 2;
145
- break;
146
- case 'btm':
147
- $offsetY = $imageHeight - $watermarkHeight - $settings['offset-vert'];
148
- break;
149
- }
150
-
151
- $params = array(
152
- $image,
153
- $watermark,
154
- $offsetX, $offsetY,
155
- 0, 0,
156
- $watermarkWidth, $watermarkHeight
157
- );
158
-
159
- if($watermarkType == 'png' && $this->is_alpha_png($watermarkFile)){
160
- $imgcopyfunc = 'imagecopy';
161
- }
162
- else {
163
- $imgcopyfunc = 'imagecopymerge';
164
- $params[] = $alpha;
165
- }
166
- call_user_func_array($imgcopyfunc, $params);
167
-
168
- $createfunc = 'image'.$imageType;
169
- $createfunc($image, $outputFile);
170
-
171
- imageDestroy($image);
172
- imageDestroy($watermark);
173
-
174
- return true;
175
- }
176
- else {
177
- $this->add_error('Can\'t add watermark to itself!');
178
-
179
- return false;
180
- }
181
- }
182
-
183
- public function add_media_page(){
184
- add_media_page( 'Easy Watermark', 'Easy Watermark', 'upload_files', 'easy-watermark', array($this, 'easy_watermark'));
185
- }
186
-
187
- public function easy_watermark(){
188
- ?>
189
- <div class="wrap easy-watermark">
190
- <h2><?php _e('Easy Watermark', 'easy-watermark'); ?></h2>
191
- <?php
192
- if(isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'])){
193
- if(isset($_GET['attachment_id'])){
194
- if($this->add_watermark($_GET['attachment_id'])) :
195
- ?>
196
- <div id="message" class="updated below-h2">
197
- <p><?php _e('Watermark successfully added.', 'easy-watermark'); ?> <a href="javascript:window.history.go(-1)"><?php _e('Go back', 'easy-watermark'); ?></a> or <a href="<?php echo get_edit_post_link($_GET['attachment_id']); ?>"><?php _e('go to edit page', 'easy-watermark'); ?></a></p>
198
- </div>
199
- <?php
200
- else :
201
- foreach($this->messages as $msg) :
202
- ?>
203
- <div id="message" class="<?php echo $msg[0]; ?> below-h2">
204
- <p><?php _e($msg[1], 'easy-watermark'); ?></p>
205
- </div>
206
- <a href="javascript:window.history.go(-1)"><?php _e('Go back', 'easy-watermark'); ?></a> or <a href="<?php echo get_edit_post_link($_GET['attachment_id']); ?>"><?php _e('go to edit page', 'easy-watermark'); ?></a>
207
- <?php
208
- endforeach;
209
- endif;
210
- }
211
- elseif(isset($_GET['watermark_all'])){
212
- if($output = $this->watermark_all()) :
213
- ?>
214
- <div id="message" class="updated below-h2">
215
- <p><?php _e('Watermark successfully added.', 'easy-watermark'); ?> <a href="<?php echo admin_url('upload.php') ?>"><?php _e('Go to Media Library', 'easy-watermark'); ?></a></p>
216
- </div>
217
- <?
218
- echo $output;
219
- endif;
220
- }
221
- }
222
- else {
223
- ?>
224
- <br/><a class="button-primary" href="<?php echo wp_nonce_url(admin_url('/upload.php?page=easy-watermark&watermark_all=1')); ?>"><?php _e('Add watermark to all images', 'easy-watermark'); ?></a><p class="description"><?php _e('Be carefull with that option. If some images alredy has watermark, it will be added though.'); ?></p>
225
- <?php
226
- $settings = $this->settings->get('image');
227
- }
228
- ?>
229
- </div>
230
- <?php
231
- }
232
-
233
- public function add_media_row_action($actions, $post, $detached){
234
- $actions['add_watermark'] = '<a href="' . wp_nonce_url(admin_url('upload.php?page=easy-watermark&attachment_id='.$post->ID)) . '">Add Watermark</a>';
235
- return $actions;
236
- }
237
-
238
- public static function install(){
239
- add_option('easy-watermark-settings', EasyWatermarkSettings::getDefaults());
240
- }
241
-
242
- public static function uninstall(){
243
- delete_option('easy-watermark-settings');
244
- }
245
-
246
- private function add_error($msg){
247
- $this->messages[] = array('error', $msg);
248
- }
249
-
250
- private function add_info($msg){
251
- $this->messages[] = array('update', $msg);
252
- }
253
-
254
- public function is_alpha_png($file){
255
- /* color type of png image stored at 25 byte:
256
- * 0 - greyscale
257
- * 2 - RGB
258
- * 3 - RGB with palette
259
- * 4 - greyscale + alpha
260
- * 6 - RGB + alpha
261
- */
262
- $colorByte = ord(@file_get_contents($file, false, null, 25, 1));
263
- return ($colorByte == 6 || $colorByte == 4);
264
- }
265
- }
266
-
267
- register_activation_hook(__FILE__, array('EasyWatermark', 'install'));
268
- register_uninstall_hook(__FILE__, array('EasyWatermark', 'uninstall'));
269
-
270
- endif;
271
-
272
- if(!function_exists('initEasyWatermark')) :
273
-
274
- function initEasyWatermark(){
275
- if(is_admin())
276
- $easyWatermark = new EasyWatermark();
277
- }
278
- add_action('init', 'initEasyWatermark');
279
-
280
- endif;
281
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
index.php ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Easy Watermark
4
+ Description: This plugin allows you to add image watermark to every single image or all images from library at once. It can also be configured to automatically watermark new images as they are uploaded. Watermark can be a png, gif (alpha channel supported in both cases) or jpg image. It's also possibile to set watermark opacity (doesn't apply to png with alpha channel).
5
+ Version: 0.1.1
6
+ Author: Wojtek Szałkiewicz
7
+ Author URI: http://szalkiewicz.pl/
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+ */
11
+ require_once dirname(__FILE__) . '/EasyWatermark.php';
12
+ require_once dirname(__FILE__) . '/EasyWatermarkPlugin.php';
13
+ require_once dirname(__FILE__) . '/EasyWatermarkSettings.php';
14
+
15
+ if(is_admin())
16
+ // init plugin only in admin panel
17
+ EasyWatermarkPlugin::init();
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: szaleq
3
  Tags: watermark, image, picture, photo, media, gallery, signature, transparent, upload, admin
4
  Requires at least: 3.3
5
  Tested up to: 3.5.1
6
- Stable tag: 0.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -43,9 +43,14 @@ If you have any other questions, please contact me.
43
 
44
  1. Settings page
45
  2. Easy Watermark Tool
46
- 3. "Add Watermark" link in media library
47
 
48
  == Changelog ==
49
 
 
 
 
 
 
50
  = 0.1 =
51
  * Initial release
3
  Tags: watermark, image, picture, photo, media, gallery, signature, transparent, upload, admin
4
  Requires at least: 3.3
5
  Tested up to: 3.5.1
6
+ Stable tag: 0.1.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
43
 
44
  1. Settings page
45
  2. Easy Watermark Tool
46
+ 3. `Add Watermark` link in media library
47
 
48
  == Changelog ==
49
 
50
+ = 0.1.1 =
51
+ * offset can be now also a percentage, not only pixel value
52
+ * changed code structure
53
+ * separete class responsible only for watermarking (can be used alone)
54
+
55
  = 0.1 =
56
  * Initial release
settings-form.php CHANGED
@@ -3,7 +3,8 @@
3
  $image_types = $options['image_types'];
4
  ?>
5
  <div class="wrap easy-watermark">
6
- <h2><?php _e('Easy Watermark Settings', 'easy-watermark'); ?></h2><a href="<?php echo admin_url('/upload.php?page=easy-watermark'); ?>"><?php _e('Go to Easy Watermark Tool', 'easy-watermark'); ?></a>
 
7
  <form method="post" action="options.php" id="easy-watermark-settings-form">
8
  <?php settings_fields('easy-watermark-settings'); ?>
9
  <h3><?php _e('General settings', 'easy-watermark'); ?></h3>
@@ -13,7 +14,7 @@
13
  </tr>
14
  <tr valign="top" class="auto-add-options" style="display:none;"><th scope="row"><?php _e('Image types', 'easy-watermark'); ?></th>
15
  <td>
16
- <label for="image-type-jpg"><input id="image-type-jpg" type="checkbox" name="easy-watermark-settings[image_types][jpeg]" value="jpeg" <?php checked('1', isset($image_types['jpeg'])); ?> /> jpeg</label><br/></fieldset>
17
  <label for="image-type-png"><input id="image-type-png" type="checkbox" name="easy-watermark-settings[image_types][png]" value="png" <?php checked('1', isset($image_types['png'])); ?> /> png</label><br/>
18
  <label for="image-type-gif"><input id="image-type-gif" type="checkbox" name="easy-watermark-settings[image_types][gif]" value="gif" <?php checked('1', isset($image_types['gif'])); ?> /> gif</label><p class="description"><?php _e('Select image types which should be watermarked', 'easy-watermark'); ?></p></td>
19
  </tr>
@@ -23,19 +24,15 @@
23
  <tr valign="top"><th scope="row"><label for="easy-watermark-url"><?php _e('Watermark image', 'easy-watermark'); ?></label></th>
24
  <td><input id="easy-watermark-url" class="regular-text" name="easy-watermark-settings[image][url]" type="hidden" value="<?php echo $options['image']['url']; ?>" /><input id="easy-watermark-id" name="easy-watermark-settings[image][id]" type="hidden" value="<?php echo $options['image']['id']; ?>" /><input id="easy-watermark-mime" name="easy-watermark-settings[image][mime]" type="hidden" value="<?php echo $options['image']['mime']; ?>" />
25
  <?php
26
- $modal_update_href = esc_url( add_query_arg( array(
27
- 'page' => 'easy-watermark',
28
- '_wpnonce' => wp_create_nonce('easy-watermark'),
29
- ), admin_url('upload.php') ) );
30
  if(empty($options['image']['url'])) :
31
  ?>
32
  <a class="button-secondary" id="select-image-button" data-choose="<?php _e('Choose Watermark Image', 'easy-watermark'); ?>" data-button-label="<?php _e('Set as Watermark Image', 'easy-watermark'); ?>" href="#"><?php _e('Select/Upload Image', 'easy-watermark'); ?></a>
33
  <br/>
34
- <input id="easy-watermark-position-vert" name="easy-watermark-settings[image][position-vert]" type="hidden" value="<?php echo $options['image']['position-vert']; ?>" />
35
- <input id="easy-watermark-position-horizontal" name="easy-watermark-settings[image][position-horizontal]" type="hidden" value="<?php echo $options['image']['position-horizontal']; ?>" />
36
- <input id="easy-watermark-position-vert" name="easy-watermark-settings[image][offset-vert]" type="hidden" value="<?php echo $options['image']['offset-vert']; ?>" />
37
- <input id="easy-watermark-position-horizontal" name="easy-watermark-settings[image][offset-horizontal]" type="hidden" value="<?php echo $options['image']['offset-horizontal']; ?>" />
38
- <input id="easy-watermark-alpha" name="easy-watermark-settings[image][alpha]" type="hidden" value="<?php echo $options['image']['alpha']; ?>" />
39
  <?php else : ?>
40
  <img id="watermark-preview" style="max-height:200px;width:auto;cursor:pointer;" src="<?php echo $options['image']['url']; ?>" /><span style="display:none;" id="loading-preview"><?php _e('Loading preview...', 'easy-watermark'); ?></span>
41
  <p class="description"><?php _e('Click on image to change it.', 'easy-watermark'); ?> <a href="#" class="remove-image"><?php _e('Remove image', 'easy-watermark'); ?></a></p>
@@ -45,19 +42,19 @@ if(empty($options['image']['url'])) :
45
 
46
  <tr valign="top" class="watermark-options"><th scope="row"><?php _e('Watermark position', 'easy-watermark'); ?></th>
47
  <td><strong> <?php _e('vertical', 'easy-watermark'); ?>: </strong>
48
- <label for="easy-watermark-position-top"><input id="easy-watermark-position-top" name="easy-watermark-settings[image][position-vert]" type="radio" value="top" <?php echo $options['image']['position-vert'] == 'top' ? 'checked' : null; ?>/> <?php _e('top', 'easy-watermark'); ?></label>
49
- <label for="easy-watermark-position-mdl"><input id="easy-watermark-position-mdl" name="easy-watermark-settings[image][position-vert]" type="radio" value="mdl" <?php echo $options['image']['position-vert'] == 'mdl' ? 'checked' : null; ?> /> <?php _e('middle', 'easy-watermark'); ?></label>
50
- <label for="easy-watermark-position-btm"><input id="easy-watermark-position-btm" name="easy-watermark-settings[image][position-vert]" type="radio" value="btm" <?php echo $options['image']['position-vert'] == 'btm' ? 'checked' : null; ?> /> <?php _e('bottom', 'easy-watermark'); ?></label><br />
51
- <label for="easy-watermark-position-offset-vert"> <?php _e('offset', 'easy-watermark'); ?>: </label>
52
- <input style="width:60px;" type="text" id="easy-watermark-position-offset-vert" name="easy-watermark-settings[image][offset-vert]" value="<?php echo $options['image']['offset-vert']; ?>" /> px<br /><br /><strong> <?php _e('horizontal', 'easy-watermark'); ?>: </strong>
53
- <label for="easy-watermark-position-lft"><input id="easy-watermark-position-lft" name="easy-watermark-settings[image][position-horizontal]" type="radio" value="lft" <?php echo $options['image']['position-horizontal'] == 'lft' ? 'checked' : null; ?>/> <?php _e('left', 'easy-watermark'); ?></label>
54
- <label for="easy-watermark-position-ctr"><input id="easy-watermark-position-ctr" name="easy-watermark-settings[image][position-horizontal]" type="radio" value="ctr" <?php echo $options['image']['position-horizontal'] == 'ctr' ? 'checked' : null; ?> /> <?php _e('center', 'easy-watermark'); ?></label>
55
- <label for="easy-watermark-position-rgt"><input id="easy-watermark-position-rgt" name="easy-watermark-settings[image][position-horizontal]" type="radio" value="rgt" <?php echo $options['image']['position-horizontal'] == 'rgt' ? 'checked' : null; ?> /> <?php _e('right', 'easy-watermark'); ?></label><br />
56
- <label for="easy-watermark-position-offset-vert">offset: </label>
57
- <input type="text" style="width:60px;" id="easy-watermark-position-offset-horizontal" name="easy-watermark-settings[image][offset-horizontal]" value="<?php echo $options['image']['offset-horizontal']; ?>"/> px
58
  </td>
59
  </tr>
60
- <tr valign="top" class="watermark-options"><th scope="row"><?php _e('Opacity', 'easy-watermark'); ?></th><td><input id="easy-watermark-alpha" name="easy-watermark-settings[image][alpha]" style="width:60px;" type="text" value="<?php echo $options['image']['alpha']; ?>" /> %<p class="description"><?php _e('Opacity does not affect the png images with alpha chanel.', 'easy-watermark'); ?></p>
61
  <?php endif; ?>
62
  </td>
63
  </tr>
3
  $image_types = $options['image_types'];
4
  ?>
5
  <div class="wrap easy-watermark">
6
+ <h2><?php _e('Easy Watermark Settings', 'easy-watermark'); ?></h2>
7
+ <a href="<?php echo admin_url('/upload.php?page=easy-watermark'); ?>"><?php _e('Go to Easy Watermark Tool', 'easy-watermark'); ?></a>
8
  <form method="post" action="options.php" id="easy-watermark-settings-form">
9
  <?php settings_fields('easy-watermark-settings'); ?>
10
  <h3><?php _e('General settings', 'easy-watermark'); ?></h3>
14
  </tr>
15
  <tr valign="top" class="auto-add-options" style="display:none;"><th scope="row"><?php _e('Image types', 'easy-watermark'); ?></th>
16
  <td>
17
+ <label for="image-type-jpg"><input id="image-type-jpg" type="checkbox" name="easy-watermark-settings[image_types][jpeg]" value="jpeg" <?php checked('1', isset($image_types['jpeg'])); ?> /> jpg/jpeg</label><br/></fieldset>
18
  <label for="image-type-png"><input id="image-type-png" type="checkbox" name="easy-watermark-settings[image_types][png]" value="png" <?php checked('1', isset($image_types['png'])); ?> /> png</label><br/>
19
  <label for="image-type-gif"><input id="image-type-gif" type="checkbox" name="easy-watermark-settings[image_types][gif]" value="gif" <?php checked('1', isset($image_types['gif'])); ?> /> gif</label><p class="description"><?php _e('Select image types which should be watermarked', 'easy-watermark'); ?></p></td>
20
  </tr>
24
  <tr valign="top"><th scope="row"><label for="easy-watermark-url"><?php _e('Watermark image', 'easy-watermark'); ?></label></th>
25
  <td><input id="easy-watermark-url" class="regular-text" name="easy-watermark-settings[image][url]" type="hidden" value="<?php echo $options['image']['url']; ?>" /><input id="easy-watermark-id" name="easy-watermark-settings[image][id]" type="hidden" value="<?php echo $options['image']['id']; ?>" /><input id="easy-watermark-mime" name="easy-watermark-settings[image][mime]" type="hidden" value="<?php echo $options['image']['mime']; ?>" />
26
  <?php
 
 
 
 
27
  if(empty($options['image']['url'])) :
28
  ?>
29
  <a class="button-secondary" id="select-image-button" data-choose="<?php _e('Choose Watermark Image', 'easy-watermark'); ?>" data-button-label="<?php _e('Set as Watermark Image', 'easy-watermark'); ?>" href="#"><?php _e('Select/Upload Image', 'easy-watermark'); ?></a>
30
  <br/>
31
+ <input id="easy-watermark-position_y" name="easy-watermark-settings[image][position_y]" type="hidden" value="<?php echo $options['image']['position_y']; ?>" />
32
+ <input id="easy-watermark-position_x" name="easy-watermark-settings[image][position_x]" type="hidden" value="<?php echo $options['image']['position_x']; ?>" />
33
+ <input id="easy-watermark-position_y" name="easy-watermark-settings[image][offset_y]" type="hidden" value="<?php echo $options['image']['offset_y']; ?>" />
34
+ <input id="easy-watermark-position_x" name="easy-watermark-settings[image][offset_x]" type="hidden" value="<?php echo $options['image']['offset_x']; ?>" />
35
+ <input id="easy-watermark-opacity" name="easy-watermark-settings[image][opacity]" type="hidden" value="<?php echo $options['image']['opacity']; ?>" />
36
  <?php else : ?>
37
  <img id="watermark-preview" style="max-height:200px;width:auto;cursor:pointer;" src="<?php echo $options['image']['url']; ?>" /><span style="display:none;" id="loading-preview"><?php _e('Loading preview...', 'easy-watermark'); ?></span>
38
  <p class="description"><?php _e('Click on image to change it.', 'easy-watermark'); ?> <a href="#" class="remove-image"><?php _e('Remove image', 'easy-watermark'); ?></a></p>
42
 
43
  <tr valign="top" class="watermark-options"><th scope="row"><?php _e('Watermark position', 'easy-watermark'); ?></th>
44
  <td><strong> <?php _e('vertical', 'easy-watermark'); ?>: </strong>
45
+ <label for="easy-watermark-position-top"><input id="easy-watermark-position-top" name="easy-watermark-settings[image][position_y]" type="radio" value="top" <?php echo $options['image']['position_y'] == 'top' ? 'checked' : null; ?>/> <?php _e('top', 'easy-watermark'); ?></label>
46
+ <label for="easy-watermark-position-mdl"><input id="easy-watermark-position-mdl" name="easy-watermark-settings[image][position_y]" type="radio" value="mdl" <?php echo $options['image']['position_y'] == 'mdl' ? 'checked' : null; ?> /> <?php _e('middle', 'easy-watermark'); ?></label>
47
+ <label for="easy-watermark-position-btm"><input id="easy-watermark-position-btm" name="easy-watermark-settings[image][position_y]" type="radio" value="btm" <?php echo $options['image']['position_y'] == 'btm' ? 'checked' : null; ?> /> <?php _e('bottom', 'easy-watermark'); ?></label><br />
48
+ <label for="easy-watermark-position-offset_y"> <?php _e('offset', 'easy-watermark'); ?>: </label>
49
+ <input style="width:60px;" type="text" id="easy-watermark-position-offset_y" name="easy-watermark-settings[image][offset_y]" value="<?php echo $options['image']['offset_y']; ?>" /><br /><br /><strong> <?php _e('horizontal', 'easy-watermark'); ?>: </strong>
50
+ <label for="easy-watermark-position-lft"><input id="easy-watermark-position-lft" name="easy-watermark-settings[image][position_x]" type="radio" value="lft" <?php echo $options['image']['position_x'] == 'lft' ? 'checked' : null; ?>/> <?php _e('left', 'easy-watermark'); ?></label>
51
+ <label for="easy-watermark-position-ctr"><input id="easy-watermark-position-ctr" name="easy-watermark-settings[image][position_x]" type="radio" value="ctr" <?php echo $options['image']['position_x'] == 'ctr' ? 'checked' : null; ?> /> <?php _e('center', 'easy-watermark'); ?></label>
52
+ <label for="easy-watermark-position-rgt"><input id="easy-watermark-position-rgt" name="easy-watermark-settings[image][position_x]" type="radio" value="rgt" <?php echo $options['image']['position_x'] == 'rgt' ? 'checked' : null; ?> /> <?php _e('right', 'easy-watermark'); ?></label><br />
53
+ <label for="easy-watermark-position-offset_x">offset: </label>
54
+ <input type="text" style="width:60px;" id="easy-watermark-position-offset_x" name="easy-watermark-settings[image][offset_x]" value="<?php echo $options['image']['offset_x']; ?>"/><p class="description"><?php _e('Offset can be defined in pixels (just numeric value) or as percentage (e.g. \'33%\')', 'easy-watermark'); ?></p>
55
  </td>
56
  </tr>
57
+ <tr valign="top" class="watermark-options"><th scope="row"><?php _e('Opacity', 'easy-watermark'); ?></th><td><input id="easy-watermark-opacity" name="easy-watermark-settings[image][opacity]" style="width:60px;" type="text" value="<?php echo $options['image']['opacity']; ?>" /> %<p class="description"><?php _e('Opacity does not affect the png images with opacity chanel.', 'easy-watermark'); ?></p>
58
  <?php endif; ?>
59
  </td>
60
  </tr>