This repository has been archived by the owner on Aug 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
fly-dynamic-image-resizer.php
executable file
·427 lines (382 loc) · 12.5 KB
/
fly-dynamic-image-resizer.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
<?php
/*
Plugin Name: Fly Dynamic Image Resizer
Description: Dynamically create image sizes on the fly!
Version: 1.0.1
Author: Junaid Bhura
Author URI: http://www.junaidbhura.com
Text Domain: fly-images
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Main Class
*/
class Fly_Images {
/* Variables */
private $_image_sizes = array();
private $_fly_dir = '';
private $_fly_dir_writeable = false;
private $_show_notice = false;
private $_capability = 'manage_options';
/**
* Constructor
*/
public function __construct() {
/* Checks for Fly Images Directory */
$this->_fly_dir = $this->get_fly_dir();
// Check if the Fly Image folder exists and is writeable
if ( ! is_dir( $this->_fly_dir ) ) {
try {
if ( mkdir( $this->_fly_dir, 0755 ) ) {
$this->_fly_dir_writeable = true;
} } catch ( Exception $e ) {}
} elseif ( is_writeable( $this->_fly_dir ) ) {
$this->_fly_dir_writeable = true;
}
/* Initializations */
add_action( 'init', array( $this, 'init' ) );
}
/**
* Initializes Actions
*/
public function init() {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_filter( 'media_row_actions', array( $this, 'media_row_action' ), 10, 2 );
add_action( 'delete_attachment', array( $this, 'delete_attachment' ) );
}
/**
* Admin Menu Item
*/
public function admin_menu() {
/* Capability Filter */
$this->_capability = apply_filters( 'fly_images_user_capability', $this->_capability );
add_management_page( __( 'Fly Images', 'fly-images' ), __( 'Fly Images', 'fly-images' ), $this->_capability, 'fly-images', array( $this, 'options_page' ) );
}
/**
* Adds a new row action to media library items.
*
* @param array $actions
* @param object $post
* @return array
*/
public function media_row_action( $actions, $post ) {
if ( 'image/' != substr( $post->post_mime_type, 0, 6 ) || ! current_user_can( $this->_capability ) ) {
return $actions;
}
$url = wp_nonce_url( admin_url( 'tools.php?page=fly-images&delete-fly-image&ids=' . $post->ID ), 'delete_fly_image', 'fly_nonce' );
$actions['fly-image-delete'] = '<a href="' . esc_url( $url ) . '" title="' . esc_attr( __( 'Delete all cached image sizes for this image', 'fly-images' ) ) . '">' . __( 'Delete Cached Fly Images', 'fly-images' ) . '</a>';
return $actions;
}
/**
* Deletes all fly images when the main image is deleted.
*
* @param integer $post_id
* @return void
*/
public function delete_attachment( $post_id = 0 ) {
$directory = $this->get_fly_dir( $post_id );
if ( is_dir( $directory ) ) {
try {
array_map( 'unlink', ( glob( $directory . '/*' ) ) ?: array() );
rmdir( $directory );
} catch ( Exception $e ) {}
}
}
/**
* The Options Page
*/
public function options_page() {
/* Check for actions */
if ( isset( $_POST ) && isset( $_POST['fly_nonce'] ) && wp_verify_nonce( $_POST['fly_nonce'], 'delete_all_fly_images' ) ) {
// Deletes all cached images
$subdirectories = scandir( $this->get_fly_dir() );
if ( $subdirectories ) {
foreach ( $subdirectories as $subdirectory ) {
if ( '.' != $subdirectory && '..' != $subdirectory ) {
$directory_path = $this->get_fly_dir( $subdirectory );
array_map( 'unlink', ( glob( $directory_path . '/*' ) ) ?: array() );
rmdir( $directory_path );
}
}
}
echo '<div class="updated"><p>' . __( 'All cached images created on the fly have been deleted.', 'fly-images' ) . '</p></div>';
} elseif ( isset( $_GET['delete-fly-image'] ) && isset( $_GET['ids'] ) && isset( $_GET['fly_nonce'] ) && wp_verify_nonce( $_GET['fly_nonce'], 'delete_fly_image' ) ) {
// Deletes cache for a single / few images
$ids = array_map( 'intval', array_map( 'trim', explode( ',', $_GET['ids'] ) ) );
if ( $ids ) {
foreach ( $ids as $id ) {
$directory = $this->get_fly_dir( $id );
if ( is_dir( $directory ) ) {
array_map( 'unlink', ( glob( $directory . '/*' ) ) ?: array() );
rmdir( $directory );
}
}
echo '<div class="updated"><p>' . __( sprintf( 'Deleted all cached sizes for this image. <a href="%s">Click here</a> to go back to the previous page.', 'javascript:history.go(-1)' ), 'fly-images' ) . '</p></div>';
}
}
// Show the template
load_template( dirname( __FILE__ ) . '/templates/options.php' );
}
/**
* Checks if the main cache folder is writeable.
*
* @return boolean
*/
public function fly_dir_writeable() {
return $this->_fly_dir_writeable;
}
/**
* Add image sizes to be created on the fly.
*
* @param string $size_name
* @param integer $width
* @param integer $height
* @param boolean $crop
*/
public function add_image_size( $size_name, $width = 0, $height = 0, $crop = false ) {
if ( '' == $size_name || ! $width || ! $height ) {
return false;
}
$this->_image_sizes[ $size_name ] = array( 'size' => array( $width, $height ), 'crop' => $crop );
return true;
}
/**
* Gets a previously declared image size.
*
* @param string $size_name
* @return array
*/
public function get_image_size( $size_name = '' ) {
if ( '' == $size_name || ! isset( $this->_image_sizes[ $size_name ] ) ) {
return array();
} else {
return $this->_image_sizes[ $size_name ];
}
}
/**
* Gets a dynamically generated image URL from the Fly_Images class.
*
* @param integer $attachment_id
* @param mixed $size
* @param boolean $crop
* @return array
*/
public function get_attachment_image_src( $attachment_id = 0, $size = '', $crop = null ) {
if ( $attachment_id < 1 || '' == $size ) {
return array();
}
// If size is 'full', we don't need a fly image
if ( 'full' == $size ) {
return wp_get_attachment_image_src( $attachment_id, 'full' );
}
// Get the attachment image
$image = wp_get_attachment_metadata( $attachment_id );
if ( false !== $image && $image ) {
// Determine file name based on size
if ( gettype( $size ) == 'string' ) {
/* String Size */
$image_size = $this->get_image_size( $size );
if ( empty( $image_size ) ) {
return array();
}
$width = $image_size['size'][0];
$height = $image_size['size'][1];
$crop = isset( $crop ) ? $crop : $image_size['crop'];
} elseif ( gettype( $size ) == 'array' ) {
/* Array Size */
$width = $size[0];
$height = $size[1];
} else {
return array();
}
$fly_dir = $this->get_fly_dir( $attachment_id );
$fly_file_name = $fly_dir . DIRECTORY_SEPARATOR . $this->get_fly_file_name( basename( $image['file'] ), $width, $height, $crop );
// Check if file exsists
if ( file_exists( $fly_file_name ) ) {
$image_editor = wp_get_image_editor( $fly_file_name );
$image_dimensions = $image_editor->get_size();
return array(
'src' => $this->get_fly_path( $fly_file_name ),
'width' => $image_dimensions['width'],
'height' => $image_dimensions['height'],
);
}
// Check if images directory is writeable
if ( ! $this->_fly_dir_writeable ) {
return array();
}
// File does not exist, lets check if directory exists
if ( ! is_dir( $fly_dir ) ) {
// Directory does not exist, let's create it
try {
if ( ! mkdir( $fly_dir, 0755 ) ) {
return array();
}
} catch ( Exception $e ) {
return array();
}
}
// Get WP Image Editor Instance
$image_path = get_attached_file( $attachment_id );
$image_editor = wp_get_image_editor( $image_path );
if ( ! is_wp_error( $image_editor ) ) {
// Create new image
$image_editor->resize( $width, $height, $crop );
$image_editor->save( $fly_file_name );
// Image created, return its data
$image_dimensions = $image_editor->get_size();
return array(
'src' => $this->get_fly_path( $fly_file_name ),
'width' => $image_dimensions['width'],
'height' => $image_dimensions['height'],
);
}
}
// Something went wrong
return array();
}
/**
* Gets a dynamically generated image HTML from the Fly_Images class.
*
* Based on /wp-includes/media.php -> wp_get_attachment_image()
*
* @param integer $attachment_id
* @param mixed $size
* @param boolean $crop
* @param array $attr
* @return string
*/
public function get_attachment_image( $attachment_id = 0, $size = '', $crop = null, $attr = array() ) {
if ( $attachment_id < 1 || '' == $size ) {
return '';
}
// If size is 'full', we don't need a fly image
if ( 'full' == $size ) {
return wp_get_attachment_image( $attachment_id, $size, $attr );
}
$html = '';
$image = $this->get_attachment_image_src( $attachment_id, $size, $crop );
if ( $image ) {
$hwstring = image_hwstring( $image['width'], $image['height'] );
$size_class = $size;
if ( is_array( $size_class ) ) {
$size_class = join( 'x', $size );
}
$attachment = get_post( $attachment_id );
$default_attr = array(
'src' => $image['src'],
'class' => "attachment-$size_class",
'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
);
if ( empty( $default_attr['alt'] ) ) {
$default_attr['alt'] = trim( strip_tags( $attachment->post_excerpt ) );
}
if ( empty( $default_attr['alt'] ) ) {
$default_attr['alt'] = trim( strip_tags( $attachment->post_title ) );
}
$attr = wp_parse_args( $attr, $default_attr );
$attr = apply_filters( 'fly_get_attachment_image_attributes', $attr, $attachment, $size );
$attr = array_map( 'esc_attr', $attr );
$html = rtrim( "<img $hwstring" );
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
}
return $html;
}
/**
* Gets a file name based on parameters.
*
* @param string $file_name
* @param string $width
* @param string $height
* @param boolean $crop
* @return string
*/
public function get_fly_file_name( $file_name, $width, $height, $crop ) {
$file_name_only = pathinfo( $file_name, PATHINFO_FILENAME );
$file_extension = pathinfo( $file_name, PATHINFO_EXTENSION );
return $file_name_only . '-' . $width . 'x' . $height . ( $crop ? '-c' : '' ) . '.' . $file_extension;
}
/**
* Gets the path to the directory where all Fly images are stored.
*
* @param string $path
* @return string
*/
public function get_fly_dir( $path = '' ) {
if ( '' === $this->_fly_dir ) {
$wp_upload_dir = wp_upload_dir();
return $wp_upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'fly-images' . ( '' !== $path ? DIRECTORY_SEPARATOR . $path : '' );
} else {
return $this->_fly_dir . ( '' !== $path ? DIRECTORY_SEPARATOR . $path : '' );
}
}
/**
* Gets the full path of an image based on it's absolute path.
*
* @param string $absolute_path
* @return string
*/
public function get_fly_path( $absolute_path = '' ) {
$wp_upload_dir = wp_upload_dir();
return $wp_upload_dir['baseurl'] . str_replace( $wp_upload_dir['basedir'], '', $absolute_path );
}
/**
* Gets the absolute path of an image based on it's full path.
*
* @param string $path
* @return string
*/
public function get_fly_absolute_path( $path = '' ) {
$wp_upload_dir = wp_upload_dir();
return $wp_upload_dir['basedir'] . str_replace( $wp_upload_dir['baseurl'], '', $path );
}
}
if ( ! function_exists( 'fly_add_image_size' ) ) {
/**
* Add image sizes to the Fly_Images class.
*
* @param string $size_name
* @param integer $width
* @param integer $height
* @param boolean $crop
*/
function fly_add_image_size( $size_name = '', $width = 0, $height = 0, $crop = false ) {
global $fly_images;
return $fly_images->add_image_size( $size_name, $width, $height, $crop );
}
}
if ( ! function_exists( 'fly_get_attachment_image_src' ) ) {
/**
* Gets a dynamically generated image URL from the Fly_Images class.
*
* @param integer $attachment_id
* @param mixed $size
* @param boolean $cropped
* @return string
*/
function fly_get_attachment_image_src( $attachment_id = 0, $size = '', $crop = null ) {
global $fly_images;
return $fly_images->get_attachment_image_src( $attachment_id, $size, $crop );
}
}
if ( ! function_exists( 'fly_get_attachment_image' ) ) {
/**
* Gets a dynamically generated image HTML from the Fly_Images class.
*
* @param integer $attachment_id
* @param mixed $size
* @param boolean $crop
* @param array $attr
* @return string
*/
function fly_get_attachment_image( $attachment_id = 0, $size = '', $crop = null, $attr = array() ) {
global $fly_images;
return $fly_images->get_attachment_image( $attachment_id, $size, $crop, $attr );
}
}
// Initialize Plugin!
global $fly_images;
$fly_images = new Fly_Images();