-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-post-image-watermarks.php
319 lines (271 loc) · 10.9 KB
/
wp-post-image-watermarks.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
<?php
/*
Plugin Name: WP Post Image Watermarks
Plugin URI:
Description:
Version: 0.0.3
Author: Jonathan Stegall
Author URI: https://code.minnpost.com
License: GPL2+
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wp-post-image-watermarks
*/
class WP_Post_Image_Watermarks {
public $option_prefix;
public $version;
public $slug;
public $thumbnail_image_field;
public $watermarked_thumbnail_sizes;
public $watermark_field;
public $watermark_folder_url;
public $watermark_extension;
public $watermark_position_x;
public $watermark_position_y;
public $watermark_width_percent;
public $watermark_all_sizes;
public $watermark_original;
public $save_temp;
/**
* @var object
* Static property to hold an instance of the class; this seems to make it reusable
*
*/
static $instance = null;
/**
* Load the static $instance property that holds the instance of the class.
* This instance makes the class reusable by other plugins
*
* @return object
* Instance of the plugin object
*
*/
static public function get_instance() {
if ( null === self::$instance ) {
self::$instance = new WP_Post_Image_Watermarks();
}
return self::$instance;
}
public function __construct() {
$this->option_prefix = 'wp_post_image_watermark';
$this->version = '0.0.3';
$this->slug = 'wp-post-image-watermarks';
// this needs to be a wp_postmeta field
$this->thumbnail_image_field = '_mp_post_thumbnail_image';
// this needs to be a wp_postmeta field
$this->thumbnail_image_field_id = '_mp_post_thumbnail_image_id';
// this needs to be an array of theme image sizes
$this->watermarked_thumbnail_sizes = array( 'feature', 'feature-large', 'feature-medium', 'newsletter-thumbnail', 'author-thumbnail', 'thumbnail' );
// this is a wp_postmetafield. the value should represent a watermark filename in the folder path.
$this->watermark_field = '_mp_plus_icon_style';
$this->watermark_folder_url = get_theme_file_path() . '/assets/img/icons/';
$this->watermark_extension = '.png';
$this->watermark_position_x = 'right';
$this->watermark_position_y = 'top';
$this->watermark_width_percent = 25;
$this->watermark_original = true;
$this->watermark_all_sizes = false;
$this->save_temp = true;
$this->add_actions();
}
/**
* Actions and filters
*
*/
private function add_actions() {
// add the image editors
add_filter( 'wp_image_editors', array( $this, 'wp_image_editors' ), 10, 1 );
// save watermark on post save
add_action( 'save_post', array( $this, 'save_watermark_image' ), 11, 1 );
}
/**
* Use the Improved Image Editor and Watermark Image Editor classes
*
* @param array $editors
* @return array $editors
* These are the available image editors to WordPress
*/
public function wp_image_editors( $editors ) {
require_once 'editors/gd.php';
require_once 'editors/imagick.php';
require_once 'editors/gmagick.php';
require_once __DIR__ . '/class-watermark-image-editor-gmagick.php';
require_once __DIR__ . '/class-watermark-image-editor-imagick.php';
require_once __DIR__ . '/class-watermark-image-editor-gd.php';
$new_editors = array(
'Watermark_Image_Editor_Gmagick',
'Watermark_Image_Editor_Imagick',
'Watermark_Image_Editor_GD',
'Improved_Image_Editor_Gmagick',
'Improved_Image_Editor_Imagick',
'Improved_Image_Editor_GD',
);
$editors = array_merge( $new_editors, $editors );
return $editors;
}
/**
* Save watermark when post is saved
*
* @param int $post_id
*/
public function save_watermark_image( $post_id ) {
// Make sure we should be doing this action
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] && ! current_user_can( 'edit_page', $post_id ) ) || ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// get the necessary dimensions for the sizes we allow to pass to the editor.
$image_sizes = $this->get_image_sizes( $this->watermarked_thumbnail_sizes );
// hook to set whether or not this attachment needs to be watermarked
$watermarked = apply_filters( 'image_watermark_allowed', false, $post_id );
if ( false === $watermarked ) {
return;
}
$post_meta = get_post_meta( $post_id );
// see if there is a value on the post for the watermark field
if ( isset( $post_meta[ $this->watermark_field ][0] ) ) {
$watermark_file_name = $post_meta[ $this->watermark_field ][0];
} else {
return;
}
// see if the post has a value for the given thumbnail image field, and if it is a WordPress uploaded file
if ( isset( $post_meta[ $this->thumbnail_image_field ][0] ) && 0 === strpos( $post_meta[ $this->thumbnail_image_field ][0], get_site_url() ) ) {
if ( true === WPCOM_IS_VIP_ENV ) {
$thumbnail_url = str_replace( home_url( '/' ), 'vip://', $post_meta[ $this->thumbnail_image_field ][0] ); // => vip://wp-content/uploads/2019/05/…-2.jpg
} else {
$thumbnail_url = str_replace( get_site_url() . '/', get_home_path(), $post_meta[ $this->thumbnail_image_field ][0] );
}
} elseif ( isset( $post_meta[ $this->thumbnail_image_field ][0] ) ) {
$thumbnail_id = $post_meta[ $this->thumbnail_image_field_id ][0];
$thumbnail_url = wp_get_attachment_url( $thumbnail_id );
} else {
return;
}
$watermark_url = $this->watermark_folder_url . $watermark_file_name . $this->watermark_extension;
// edit the thumbnail image
$image = wp_get_image_editor( $thumbnail_url );
if ( ! is_wp_error( $image ) ) {
$original_size = $image->get_size();
}
// by putting the watermark on it
if ( ! is_wp_error( $image ) && is_callable( [ $image, 'stamp_watermark' ] ) ) {
// if true, this saves a watermarked version of the original uploaded image
if ( true === $this->watermark_original ) {
$original_image = wp_get_image_editor( $thumbnail_url );
$watermark_image = wp_get_image_editor( $watermark_url );
if ( ! is_wp_error( $watermark_image ) && ! is_wp_error( $original_image ) ) {
$original_size = $original_image->get_size();
$is_resized = $watermark_image->resize_get_resource( ( $this->watermark_width_percent / 100 ) * $original_size['width'], null );
// put the watermark on top of the generated image and save it
$success = $original_image->stamp_watermark( $watermark_image, $this->watermark_position_x, $this->watermark_position_y );
$this->save_or_sideload( $thumbnail_url, $original_image, $post_id );
}
}
// this saves a watermarked version of each of the plugin's specified watermarked thumbnails.
if ( true === $this->watermark_all_sizes ) {
foreach ( $image_sizes as $key => $size ) {
$thumbnail_editor = wp_get_image_editor( $thumbnail_url );
$watermark_image = wp_get_image_editor( $watermark_url );
if ( ! is_wp_error( $thumbnail_editor ) && ! is_wp_error( $watermark_image ) ) {
// set the dimensions that WordPress should generate for the image
$thumbnail_editor->resize( $size['width'], $size['height'], $size['crop'] );
$is_resized = $watermark_image->resize_get_resource( ( $this->watermark_width_percent / 100 ) * $size['width'], null );
// put the watermark on top of the generated image and save it
$success = $thumbnail_editor->stamp_watermark( $watermark_image, $this->watermark_position_x, $this->watermark_position_y );
$this->save_or_sideload( $thumbnail_url, $thumbnail_editor, $post_id, $key );
}
}
}
}
}
/**
* Save the file to the directory, or save it to a temporary directory and then sideload it into the Media Library
*
* @param string $image_url
* @param object $image_editor
* @param int $post_id
* @param string $size_name
*/
private function save_or_sideload( $image_url, $image_editor, $post_id, $size_name = '' ) {
// save to temp directory
if ( true === $this->save_temp ) {
$filename = wp_basename( $image_url );
$temp_file = get_temp_dir() . $filename;
$resized_file = $image_editor->save( $temp_file );
$filename_parts = pathinfo( $image_url );
if ( '' !== $size_name ) {
$filename = $filename_parts['filename'] . '-' . $resized_file['width'] . 'x' . $resized_file['height'] . '.' . $filename_parts['extension'];
}
$resized_file_array = array( // array to mimic $_FILES
'name' => $filename,
'type' => $resized_file['mime-type'],
'tmp_name' => $temp_file, //this field passes the actual path to the image
'error' => 0,
'size' => filesize( $temp_file ),
);
$sideload_id = media_handle_sideload( $resized_file_array, $post_id );
if ( ! is_wp_error( $sideload_id ) ) {
if ( file_exists( $temp_file ) ) {
unlink( $temp_file );
}
if ( '' === $size_name ) { // this is an original file upload
if ( '' !== $this->thumbnail_image_field ) {
$media_url = wp_get_attachment_url( $sideload_id );
update_post_meta( $post_id, $this->thumbnail_image_field, $media_url );
}
if ( '' !== $this->thumbnail_image_field_id ) {
update_post_meta( $post_id, $this->thumbnail_image_field_id, $sideload_id );
}
} else {
// this is a thumbnail size
if ( '' !== $this->thumbnail_image_field_id ) {
$post_meta = get_post_meta( $post_id );
// see if there is an image id on the post
if ( isset( $post_meta[ $this->thumbnail_image_field_id ][0] ) ) {
$attachment_id = $post_meta[ $this->thumbnail_image_field_id ][0];
$metadata = wp_get_attachment_metadata( $attachment_id );
$data['sizes'][ $size_name ] = array(
'file' => $filename,
'width' => $resized_file['width'],
'height' => $resized_file['height'],
'mime-type' => $resized_file['mime-type'],
);
wp_update_attachment_metadata( $attachment_id, $data );
$updated_metadata = wp_get_attachment_metadata( $attachment_id );
} else {
return;
}
}
}
}
} else {
$resized_file = $image_editor->save();
unset( $resized_file['path'] );
}
}
/**
* Get size information for all currently-registered image sizes.
*
* @global $_wp_additional_image_sizes
* @uses get_intermediate_image_sizes()
* @return array $sizes Data for all currently-registered image sizes.
*/
public function get_image_sizes( $allowed_sizes ) {
global $_wp_additional_image_sizes;
$sizes = array();
foreach ( $allowed_sizes as $_size ) {
if ( in_array( $_size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
$sizes[ $_size ]['width'] = get_option( "{$_size}_size_w" );
$sizes[ $_size ]['height'] = get_option( "{$_size}_size_h" );
$sizes[ $_size ]['crop'] = (bool) get_option( "{$_size}_crop" );
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
$sizes[ $_size ] = array(
'width' => $_wp_additional_image_sizes[ $_size ]['width'],
'height' => $_wp_additional_image_sizes[ $_size ]['height'],
'crop' => $_wp_additional_image_sizes[ $_size ]['crop'],
);
}
}
return $sizes;
}
}
// Instantiate our class
$wp_post_image_watermarks = WP_Post_Image_Watermarks::get_instance();