|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Helper class for resizing images, returning the correct URL to the image etc |
|
5
|
|
|
*/ |
|
6
|
|
|
class MetaSliderImageHelper |
|
|
|
|
|
|
7
|
|
|
{ |
|
8
|
|
|
private $smart_crop = 'false'; |
|
9
|
|
|
private $container_width; // slideshow width |
|
10
|
|
|
private $container_height; // slideshow height |
|
11
|
|
|
private $id; // slide/attachment ID |
|
12
|
|
|
private $url; |
|
13
|
|
|
private $path; // path to attachment on server |
|
14
|
|
|
private $use_image_editor; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor |
|
18
|
|
|
* |
|
19
|
|
|
* @param integer $slide_id |
|
20
|
|
|
* @param integer $width - required width of image |
|
21
|
|
|
* @param integer $height - required height of image |
|
22
|
|
|
* @param string $smart_crop |
|
23
|
|
|
* @param bool $use_image_editor |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($slide_id, $width, $height, $smart_crop, $use_image_editor = true) |
|
26
|
|
|
{ |
|
27
|
|
|
$upload_dir = wp_upload_dir(); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$this->id = $slide_id; |
|
30
|
|
|
$this->url = $upload_dir['baseurl'] . '/' . get_post_meta($slide_id, '_wp_attached_file', true); |
|
|
|
|
|
|
31
|
|
|
$this->path = get_attached_file($slide_id); |
|
|
|
|
|
|
32
|
|
|
$this->container_width = $width; |
|
33
|
|
|
$this->container_height = $height; |
|
34
|
|
|
$this->smart_crop = $smart_crop; |
|
35
|
|
|
$this->use_image_editor = $use_image_editor; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Return the crop dimensions. |
|
40
|
|
|
* |
|
41
|
|
|
* Smart Crop: If the image is smaller than the container width or height, then return |
|
42
|
|
|
* dimensions that respect the container size ratio. This ensures image displays in a |
|
43
|
|
|
* sane manner in responsive sliders |
|
44
|
|
|
* |
|
45
|
|
|
* @param integer $image_width |
|
46
|
|
|
* @param integer $image_height |
|
47
|
|
|
* @return array image dimensions |
|
48
|
|
|
*/ |
|
49
|
|
|
private function get_crop_dimensions($image_width, $image_height) |
|
50
|
|
|
{ |
|
51
|
|
|
if ('false' === $this->smart_crop) { |
|
52
|
|
|
return array('width' => (int)$this->container_width, 'height' => (int)$this->container_height); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$container_width = $this->container_width; |
|
56
|
|
|
$container_height = $this->container_height; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Slideshow Width == Slide Width |
|
60
|
|
|
*/ |
|
61
|
|
|
if ($image_width == $container_width && $image_height == $container_height) { |
|
62
|
|
|
$new_slide_width = $container_width; |
|
63
|
|
|
$new_slide_height = $container_height; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($image_width == $container_width && $image_height < $container_height) { |
|
67
|
|
|
$new_slide_height = $image_height; |
|
68
|
|
|
$new_slide_width = $container_width / ($container_height / $image_height); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($image_width == $container_width && $image_height > $container_height) { |
|
72
|
|
|
$new_slide_width = $container_width; |
|
73
|
|
|
$new_slide_height = $container_height; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Slideshow Width < Slide Width |
|
78
|
|
|
*/ |
|
79
|
|
|
if ($image_width < $container_width && $image_height == $container_height) { |
|
80
|
|
|
$new_slide_width = $image_width; |
|
81
|
|
|
$new_slide_height = $image_height / ($container_width / $image_width); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Slide is smaller than slidehow - both width and height |
|
86
|
|
|
*/ |
|
87
|
|
|
if ($image_width < $container_width && $image_height < $container_height) { |
|
88
|
|
|
if ($container_width > $container_height) { |
|
89
|
|
|
// wide |
|
90
|
|
|
|
|
91
|
|
|
if ($image_width > $image_height) { |
|
92
|
|
|
// wide |
|
93
|
|
|
$new_slide_height = $image_height; |
|
94
|
|
|
$new_slide_width = $container_width / ($container_height / $image_height); |
|
95
|
|
|
|
|
96
|
|
|
if ($new_slide_width > $image_width) { |
|
97
|
|
|
$new_slide_width = $image_width; |
|
98
|
|
|
$new_slide_height = $container_height / ($container_width / $image_width); |
|
99
|
|
|
} |
|
100
|
|
|
} else { |
|
101
|
|
|
// tall |
|
102
|
|
|
$new_slide_width = $image_width; |
|
103
|
|
|
$new_slide_height = $container_height / ($container_width / $image_width); |
|
104
|
|
|
|
|
105
|
|
|
if ($new_slide_height > $image_height) { |
|
106
|
|
|
$new_slide_height = $image_height; |
|
107
|
|
|
$new_slide_width = $container_width / ($container_height / $image_height); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} else { |
|
111
|
|
|
// tall |
|
112
|
|
|
if ($image_width > $image_height) { |
|
113
|
|
|
// wide |
|
114
|
|
|
$new_slide_height = $image_height; |
|
115
|
|
|
$new_slide_width = $container_width / ($container_height / $image_height); |
|
116
|
|
|
|
|
117
|
|
|
if ($new_slide_width > $image_width) { |
|
118
|
|
|
$new_slide_width = $image_width; |
|
119
|
|
|
$new_slide_height = $container_height / ($container_width / $image_width); |
|
120
|
|
|
} |
|
121
|
|
|
} else { |
|
122
|
|
|
// tall |
|
123
|
|
|
$new_slide_width = $image_width; |
|
124
|
|
|
$new_slide_height = $container_height / ($container_width / $image_width); |
|
125
|
|
|
|
|
126
|
|
|
if ($new_slide_height > $image_height) { |
|
127
|
|
|
$new_slide_height = $image_height; |
|
128
|
|
|
$new_slide_width = $container_width / ($container_height / $image_height); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ($image_width < $container_width && $image_height > $container_height) { |
|
135
|
|
|
$new_slide_width = $image_width; |
|
136
|
|
|
$new_slide_height = $container_height / ($container_width / $image_width); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Slideshow Width > Slide Width |
|
141
|
|
|
*/ |
|
142
|
|
|
if ($image_width > $container_width && $image_height == $container_height) { |
|
143
|
|
|
$new_slide_width = $container_width; |
|
144
|
|
|
$new_slide_height = $container_height; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ($image_width > $container_width && $image_height < $container_height) { |
|
148
|
|
|
$new_slide_height = $image_height; |
|
149
|
|
|
$new_slide_width = $container_width / ($container_height / $image_height); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if ($image_width > $container_width && $image_height > $container_height) { |
|
153
|
|
|
$new_slide_width = $container_width; |
|
154
|
|
|
$new_slide_height = $container_height; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return array('width' => floor($new_slide_width), 'height' => floor($new_slide_height)); |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Return the image URL, crop the image to the correct dimensions if required |
|
162
|
|
|
* |
|
163
|
|
|
* @return string resized image URL |
|
164
|
|
|
*/ |
|
165
|
|
|
public function get_image_url() |
|
166
|
|
|
{ |
|
167
|
|
|
// Get the image file path |
|
168
|
|
|
if (!strlen($this->path)) { |
|
169
|
|
|
return $this->url; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// if the file exists, just return it without going any further |
|
173
|
|
|
$dest_file_name = $this->get_destination_file_name(array( |
|
174
|
|
|
'width' => $this->container_width, |
|
175
|
|
|
'height' => $this->container_height |
|
176
|
|
|
)); |
|
177
|
|
|
|
|
178
|
|
|
if (file_exists($dest_file_name)) { |
|
179
|
|
|
return str_replace(basename($this->url), basename($dest_file_name), $this->url); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// file doesn't exist, detect required size |
|
183
|
|
|
$orig_size = $this->get_original_image_dimensions(); |
|
184
|
|
|
|
|
185
|
|
|
// bail out if we can't find the image dimensions |
|
186
|
|
|
if (false == $orig_size) { |
|
187
|
|
|
return $this->url; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
// required size |
|
191
|
|
|
$dest_size = $this->get_crop_dimensions($orig_size['width'], $orig_size['height']); |
|
192
|
|
|
|
|
193
|
|
|
// check if a resize is needed |
|
194
|
|
|
if ($orig_size['width'] == $dest_size['width'] && $orig_size['height'] == $dest_size['height']) { |
|
195
|
|
|
return $this->url; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$dest_file_name = $this->get_destination_file_name($dest_size); |
|
199
|
|
|
|
|
200
|
|
|
if (file_exists($dest_file_name)) { |
|
201
|
|
|
// good. no need for resize, just return the URL |
|
202
|
|
|
$dest_url = str_replace(basename($this->url), basename($dest_file_name), $this->url); |
|
203
|
|
|
} elseif ($this->use_image_editor) { |
|
204
|
|
|
// resize, assuming we're allowed to use the image editor |
|
205
|
|
|
$dest_url = $this->resize_image($orig_size, $dest_size, $dest_file_name); |
|
206
|
|
|
} else { |
|
207
|
|
|
// fall back to the full URL |
|
208
|
|
|
$dest_url = $this->url; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$dest_url = apply_filters('metaslider_resized_image_url', $dest_url, $this->url); |
|
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
return $dest_url; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get the image dimensions for the original image. |
|
218
|
|
|
* |
|
219
|
|
|
* Fall back to using the WP_Image_Editor if the size is not stored in metadata |
|
220
|
|
|
* |
|
221
|
|
|
* @return array |
|
222
|
|
|
*/ |
|
223
|
|
|
private function get_original_image_dimensions() |
|
224
|
|
|
{ |
|
225
|
|
|
$size = array(); |
|
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
// try and get the image size from metadata |
|
228
|
|
|
$meta = wp_get_attachment_metadata($this->id); |
|
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
if (isset($meta['width'], $meta['height'])) { |
|
231
|
|
|
return $meta; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
if ($this->use_image_editor) { |
|
235
|
|
|
// get the size from the image itself |
|
236
|
|
|
$image = wp_get_image_editor($this->path); |
|
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
if (!is_wp_error($image)) { |
|
|
|
|
|
|
239
|
|
|
$size = $image->get_size(); |
|
240
|
|
|
|
|
241
|
|
|
return $size; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return false; |
|
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Return the file name for the required image size |
|
250
|
|
|
* |
|
251
|
|
|
* @param array $dest_size image dimensions (width/height) in pixels |
|
252
|
|
|
* @return string |
|
253
|
|
|
*/ |
|
254
|
|
|
private function get_destination_file_name($dest_size) |
|
255
|
|
|
{ |
|
256
|
|
|
$info = pathinfo($this->path); |
|
257
|
|
|
$dir = $info['dirname']; |
|
258
|
|
|
$ext = $info['extension']; |
|
259
|
|
|
$name = wp_basename($this->path, ".$ext"); |
|
|
|
|
|
|
260
|
|
|
$dest_file_name = "{$dir}/{$name}-{$dest_size['width']}x{$dest_size['height']}.{$ext}"; |
|
261
|
|
|
|
|
262
|
|
|
return $dest_file_name; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Use WP_Image_Editor to create a resized image and return the URL for that image |
|
267
|
|
|
* |
|
268
|
|
|
* @param array $orig_size |
|
269
|
|
|
* @param array $dest_size |
|
270
|
|
|
* @param $dest_file_name |
|
271
|
|
|
* @return string |
|
272
|
|
|
*/ |
|
273
|
|
|
private function resize_image($orig_size, $dest_size, $dest_file_name) |
|
274
|
|
|
{ |
|
275
|
|
|
// load image |
|
276
|
|
|
$image = wp_get_image_editor($this->path); |
|
|
|
|
|
|
277
|
|
|
|
|
278
|
|
|
// editor will return an error if the path is invalid |
|
279
|
|
|
if (is_wp_error($image)) { |
|
|
|
|
|
|
280
|
|
|
if (is_admin()) { |
|
|
|
|
|
|
281
|
|
|
echo '<div id="message" class="error">'; |
|
282
|
|
|
echo '<p><strong>ERROR</strong> ' . $image->get_error_message() . " Check <a href='http://codex.wordpress.org/Changing_File_Permissions' target='_blank'>file permissions</a></p>"; |
|
283
|
|
|
echo "<button class='toggle'>Show Details</button>"; |
|
284
|
|
|
echo "<div class='message' style='display: none;'><br >Slide ID: {$this->id}<pre>"; |
|
285
|
|
|
var_dump($image); |
|
|
|
|
|
|
286
|
|
|
echo '</pre></div>'; |
|
287
|
|
|
echo '</div>'; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
return $this->url; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
$dims = image_resize_dimensions($orig_size['width'], $orig_size['height'], $dest_size['width'], $dest_size['height'], true); |
|
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
if ($dims) { |
|
296
|
|
|
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; |
|
297
|
|
|
$image->crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
$saved = $image->save($dest_file_name); |
|
301
|
|
|
|
|
302
|
|
|
if (is_wp_error($saved)) { |
|
303
|
|
|
return $this->url; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
// Record the new size so that the file is correctly removed when the media file is deleted. |
|
307
|
|
|
$backup_sizes = get_post_meta($this->id, '_wp_attachment_backup_sizes', true); |
|
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
if (!is_array($backup_sizes)) { |
|
310
|
|
|
$backup_sizes = array(); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
$backup_sizes["resized-{$dest_size['width']}x{$dest_size['height']}"] = $saved; |
|
314
|
|
|
update_post_meta($this->id, '_wp_attachment_backup_sizes', $backup_sizes); |
|
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
$url = str_replace(basename($this->url), basename($saved['path']), $this->url); |
|
317
|
|
|
|
|
318
|
|
|
return $url; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.