forked from humanmade/WPThumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpthumb.watermark.php
442 lines (365 loc) · 11.4 KB
/
wpthumb.watermark.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
class WP_Thumb_Watermark {
private $args = array();
private $editor;
public function __construct( $editor, $args ) {
$this->editor = $editor;
$defaults = array(
'padding' => 0,
'position' => 'top,left',
'mask' => ''
);
$this->args = wp_parse_args( $args['watermark_options'], $defaults );
$this->fill_watermark();
}
public function fill_watermark() {
$image = $this->editor->get_image();
$size = $this->editor->get_size();
list( $mask_width, $mask_height, $mask_type, $mask_attr ) = getimagesize( $this->args['mask'] );
switch ( $mask_type ) {
case 1:
$mask = imagecreatefromgif( $this->args['mask'] );
break;
case 2:
$mask = imagecreatefromjpeg( $this->args['mask'] );
break;
case 3:
$mask = imagecreatefrompng( $this->args['mask'] );
break;
}
imagealphablending( $image, true );
if ( strpos( $this->args['position'], 'left' ) !== false ) {
$left = $this->args['padding'];
} else {
$left = $size['width'] - $mask_width - $this->args['padding'];
}
if ( strpos( $this->args['position'], 'top' ) !== false ) {
$top = $this->args['padding'];
} else {
$top = $size['height'] - $mask_height - $this->args['padding'];
}
imagecopy(
$image,
$mask,
$left,
$top,
0,
0,
$mask_width,
$mask_height
);
$this->editor->update_image( $image );
imagedestroy( $mask );
}
}
function wpthumb_watermark_add_args_to_post_image( $args, $id ) {
if ( wpthumb_wm_image_has_watermark( $id ) ) {
$args['watermark_options'] = wpthumb_wm_get_options( $id );
}
return $args;
}
add_filter( 'wpthumb_post_image_args', 'wpthumb_watermark_add_args_to_post_image', 10, 2 );
/**
* Hook into WP Thumb before it resizes an image to possible apply a watermatk
*
* @param WP_IMageEditor $editor
* @param array $args
*/
function wpthumb_watermark_pre( $editor, $args ) {
// currently only supports GD
if ( ! $editor instanceof \WP_Thumb_Image_Editor_GD || empty( $args['watermark_options'] ) ) {
return $editor;
}
// we only want pre
if ( empty( $args['watermark_options']['pre_resize'] ) ) {
return;
}
new WP_Thumb_Watermark( $editor, $args );
return $editor;
}
add_filter( 'wpthumb_image_pre', 'wpthumb_watermark_pre', 10, 2 );
function wpthumb_watermark_post( $editor, $args ) {
// currently only supports GD
if ( ! $editor instanceof \WP_Thumb_Image_Editor_GD || empty( $args['watermark_options'] ) ) {
return $editor;
}
// we only want pre
if ( isset( $args['watermark_options']['pre_resize'] ) && $args['watermark_options']['pre_resize'] === true ) {
return;
}
new WP_Thumb_Watermark( $editor, $args );
return $editor;
}
add_filter( 'wpthumb_image_post', 'wpthumb_watermark_post', 10, 2 );
/**
* wpthumb_media_form_crop_position function.
*
* Adds a back end for selecting the crop position of images.
*
* @access public
*
* @param array $fields
* @param array $post
*
* @return $post
*/
function wpthumb_media_form_watermark_position( $fields, $post ) {
if ( ! wp_attachment_is_image( $post->ID ) ) {
return $fields;
}
$current_position = get_post_meta( $post->ID, 'wpthumb_wm_position', true );
if ( ! $current_position ) {
$current_position = 'top-left';
}
ob_start();
?>
<style>
#wpthumb_crop_pos {
}
.go-left {
margin-left: -93px;
width: 80px;
display: inline-block;
margin-right: 10px;
text-align: right;
color: #999;
}
#wpthumb_watermark_pos {
margin: 10px 0
}
#wpthumb_watermark_pos input {
margin: 3px;
width: auto;
}
</style>
<div id="wm-options-<?php echo $post->ID ?>">
<p>
<label>
<input class="wm-toggle-watermark" type="checkbox"
style="width: 20px" <?php checked( wpthumb_wm_image_has_watermark( $post->ID ) ) ?>
name="attachments[<?php echo $post->ID ?>][wpthumb_wm_use_watermark]"/> Apply Watermark
</label>
</p>
<div class="wpthumb_watermark_options <?php echo wpthumb_wm_image_has_watermark( $post->ID ) ? '' : 'hidden' ?>">
<div>
<label class="go-left">Mask</label>
<select name="attachments[<?php echo $post->ID ?>][wm_watermark_mask]">
<?php foreach ( wpthumb_wm_get_watermark_masks() as $mask_id => $watermark_mask ) : ?>
<option value="<?php echo $mask_id ?>" <?php selected( wpthumb_wm_mask( $post->ID ) == $mask_id ) ?>><?php echo $watermark_mask['label'] ?></option>
<?php endforeach; ?>
</select>
</div>
<div id="wpthumb_watermark_pos">
<label class="go-left">Position</label>
<input type="radio" name="attachments[<?php echo $post->ID ?>][wpthumb_wm_watermark_position]"
value="top-left" title="Left, Top" <?php checked( 'top-left', $current_position ) ?>/>
<input type="radio" name="attachments[<?php echo $post->ID ?>][wpthumb_wm_watermark_position]"
value="top-right" title="Center, Top" <?php checked( 'top-right', $current_position ) ?> /><br/>
<input type="radio" name="attachments[<?php echo $post->ID ?>][wpthumb_wm_watermark_position]"
value="bottom-left" title="Right, Top" <?php checked( 'bottom-left', $current_position ) ?> />
<input type="radio" name="attachments[<?php echo $post->ID ?>][wpthumb_wm_watermark_position]"
value="bottom-right"
title="Left, Center" <?php checked( 'bottom-right', $current_position ) ?> />
</div>
<div id="">
<label class="go-left">Padding</label>
<input type="number" value="<?php echo wpthumb_wm_padding( $post->ID ) ?>"
name="attachments[<?php echo $post->ID ?>][wpthumb_wm_watermark_padding]" style="width: 40px"/>
</div>
</div>
</div>
<script>
jQuery('#wm-options-<?php echo $post->ID ?>').live('change', '.wm-toggle-watermark', function (e) {
jQuery(e.target).closest('p').next().toggle();
});
</script>
<?php
$html = ob_get_clean();
$fields['watermark-position'] = array(
'label' => __( 'Watermark', 'wpthumb' ),
'input' => 'html',
'html' => $html
);
return $fields;
}
/**
* Only add the watermkaring admin optins if the current theme suports it, as we don;t want to clutter
* for poeple who don't care
*
*/
function wpthumb_add_watermarking_admin_hooks() {
if ( current_theme_supports( 'wpthumb-watermarking' ) ) {
add_filter( 'attachment_fields_to_edit', 'wpthumb_media_form_watermark_position', 10, 2 );
add_filter( 'attachment_fields_to_save', 'wpthumb_media_form_watermark_save', 10, 2 );
}
}
add_action( 'init', 'wpthumb_add_watermarking_admin_hooks' );
/**
* wpthumb_media_form_watermark_save function.
*
* Saves watermark in post meta.
*
* @access public
*
* @param array $post
* @param array $attachment
*
* @return $post
*/
function wpthumb_media_form_watermark_save( $post, $attachment ) {
if ( ! empty( $attachment['wpthumb_wm_use_watermark'] ) ) {
update_post_meta( $post['ID'], 'use_watermark', true );
update_post_meta( $post['ID'], 'wpthumb_wm_position', $attachment['wpthumb_wm_watermark_position'] );
update_post_meta( $post['ID'], 'wpthumb_wm_padding', (int) $attachment['wpthumb_wm_watermark_padding'] );
update_post_meta( $post['ID'], 'wpthumb_wm_pre_resize', '0' );
update_post_meta( $post['ID'], 'wpthumb_wm_mask', $attachment['wm_watermark_mask'] );
} else {
delete_post_meta( $post['ID'], 'use_watermark' );
delete_post_meta( $post['ID'], 'wpthumb_wm_position' );
delete_post_meta( $post['ID'], 'wpthumb_wm_padding' );
delete_post_meta( $post['ID'], 'wpthumb_wm_pre_resize' );
delete_post_meta( $post['ID'], 'wpthumb_wm_mask' );
}
return $post;
}
/**
* wpthumb_wm_get_options function.
*
* @access public
*
* @param mixed $id
*
* @return array
*/
function wpthumb_wm_get_options( $id ) {
if ( ! wpthumb_wm_image_has_watermark( $id ) ) {
return array();
}
$options['mask'] = get_template_directory() . '/images/watermark.png';
$mask = wpthumb_wm_mask( $id );
if ( ! empty( $mask ) ) {
$options['mask'] = wpthumb_wm_get_watermark_mask_file( $mask );
} else {
$mask = wpthumb_wm_get_default_watermark_mask();
$options['mask'] = $mask['file'];
}
$options['padding'] = wpthumb_wm_padding( $id );
$position = wpthumb_wm_position( $id );
$options['position'] = $position;
return $options;
}
/**
* wpthumb_wm_image_has_watermark function.
*
* @access public
*
* @param mixed $image_id
*
* @return null
*/
function wpthumb_wm_image_has_watermark( $image_id ) {
return (bool) get_post_meta( $image_id, 'use_watermark', true );
}
/**
* wpthumb_wm_position function.
*
* @access public
*
* @param mixed $image_id
*
* @return null
*/
function wpthumb_wm_position( $image_id ) {
if ( $pos = get_post_meta( $image_id, 'wpthumb_wm_position', true ) ) {
return $pos;
}
//legacy
if ( $pos = get_post_meta( $image_id, 'wm_position', true ) ) {
return $pos;
}
}
/**
* wpthumb_wm_padding function.
*
* @access public
*
* @param mixed $image_id
*
* @return null
*/
function wpthumb_wm_padding( $image_id ) {
if ( $padding = (int) get_post_meta( $image_id, 'wpthumb_wm_padding', true ) ) {
return $padding;
}
//legacy
if ( $padding = (int) get_post_meta( $image_id, 'wm_padding', true ) ) {
return $padding;
}
}
function wpthumb_wm_pre_resize( $image_id ) {
if ( $pre = (bool) get_post_meta( $image_id, 'wpthumb_wm_pre_resize', true ) ) {
return $pre;
}
//legacy
if ( $pre = (bool) get_post_meta( $image_id, 'wm_pre_resize', true ) ) {
return $pre;
}
}
function wpthumb_wm_mask( $image_id ) {
if ( $pre = (string) get_post_meta( $image_id, 'wpthumb_wm_mask', true ) ) {
return $pre;
}
//legacy
if ( $pre = (string) get_post_meta( $image_id, 'wm_mask', true ) ) {
return $pre;
}
}
/**
* Returns all the watermarks that are registered
*
* @return array
*/
function wpthumb_wm_get_watermark_masks() {
global $_wm_registered_watermarks;
$_wm_registered_watermarks = (array) $_wm_registered_watermarks;
$masks = array(
'default' => array(
'file' => get_stylesheet_directory() . '/images/watermark.png',
'label' => 'Default'
)
);
$masks = array_merge( $masks, $_wm_registered_watermarks );
return $masks;
}
/**
* Returns the watermarking image file for a given watermark name
*
* @param string $mask
*
* @return string
*/
function wpthumb_wm_get_watermark_mask_file( $mask ) {
$masks = wpthumb_wm_get_watermark_masks();
return $masks[ $mask ]['file'];
}
/**
* Registers extra watermark images for the user to select in the admin
*
* @param string $name - sanitized identifier
* @param string $file - full path to the watermarking image
* @param string $label - test to be used for the watermarks name
*/
function wpthumb_wm_register_watermark( $name, $file, $label ) {
global $_wm_registered_watermarks;
$_wm_registered_watermarks = (array) $_wm_registered_watermarks;
$_wm_registered_watermarks[ $name ] = array( 'file' => $file, 'label' => $label );
}
/**
* Returns the default watermask array ( file => string, label => string )
*
* @return array
*/
function wpthumb_wm_get_default_watermark_mask() {
$masks = wpthumb_wm_get_watermark_masks();
return $masks['default'];
}