Imsanity - Version 2.6.0

Version Description

  • added: wp-cli command 'wp help imsanity resize'
  • fixed: adding an image to a post in pre-draft status uses wrong settings/dimensions
Download this release

Release Info

Developer nosilver4u
Plugin Icon 128x128 Imsanity
Version 2.6.0
Comparing to
See all releases

Code changes from version 2.5.0 to 2.6.0

Files changed (7) hide show
  1. ajax.php +3 -127
  2. changelog.txt +4 -0
  3. class-imsanity-cli.php +115 -0
  4. imsanity.php +46 -2
  5. libs/utils.php +135 -0
  6. readme.txt +9 -3
  7. settings.php +2 -1
ajax.php CHANGED
@@ -6,7 +6,7 @@
6
  */
7
 
8
  add_action( 'wp_ajax_imsanity_get_images', 'imsanity_get_images' );
9
- add_action( 'wp_ajax_imsanity_resize_image', 'imsanity_resize_image' );
10
 
11
  /**
12
  * Verifies that the current user has administrator permission and, if not,
@@ -114,11 +114,9 @@ function imsanity_get_images() {
114
  * Resizes the image with the given id according to the configured max width and height settings
115
  * renders a json response indicating success/failure and dies
116
  */
117
- function imsanity_resize_image() {
118
  imsanity_verify_permission();
119
 
120
- global $wpdb;
121
-
122
  $id = (int) $_POST['id'];
123
 
124
  if ( ! $id ) {
@@ -131,129 +129,7 @@ function imsanity_resize_image() {
131
  )
132
  );
133
  }
134
-
135
- $meta = wp_get_attachment_metadata( $id );
136
-
137
- if ( $meta && is_array( $meta ) ) {
138
- $uploads = wp_upload_dir();
139
- $oldpath = imsanity_attachment_path( $meta['file'], $id, '', false );
140
- if ( empty( $oldpath ) || ! is_writable( $oldpath ) ) {
141
- /* translators: %s: File-name of the image */
142
- $msg = sprintf( esc_html__( '%s is not writable', 'imsanity' ), $meta['file'] );
143
- die(
144
- json_encode(
145
- array(
146
- 'success' => false,
147
- 'message' => $msg,
148
- )
149
- )
150
- );
151
- }
152
-
153
- $maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
154
- $maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
155
-
156
- // method one - slow but accurate, get file size from file itself.
157
- list( $oldw, $oldh ) = getimagesize( $oldpath );
158
- // method two - get file size from meta, fast but resize will fail if meta is out of sync.
159
- if ( ! $oldw || ! $oldh ) {
160
- $oldw = $meta['width'];
161
- $oldh = $meta['height'];
162
- }
163
-
164
- if ( ( $oldw > $maxw && $maxw > 0 ) || ( $oldh > $maxh && $maxh > 0 ) ) {
165
- $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
166
-
167
- if ( $maxw > 0 && $maxh > 0 && $oldw >= $maxw && $oldh >= $maxh && ( $oldh > $maxh || $oldw > $maxw ) && apply_filters( 'imsanity_crop_image', false ) ) {
168
- $neww = $maxw;
169
- $newh = $maxh;
170
- } else {
171
- list( $neww, $newh ) = wp_constrain_dimensions( $oldw, $oldh, $maxw, $maxh );
172
- }
173
-
174
- $resizeresult = imsanity_image_resize( $oldpath, $neww, $newh, apply_filters( 'imsanity_crop_image', false ), null, null, $quality );
175
-
176
- if ( $resizeresult && ! is_wp_error( $resizeresult ) ) {
177
- $newpath = $resizeresult;
178
-
179
- if ( $newpath !== $oldpath && is_file( $newpath ) && filesize( $newpath ) < filesize( $oldpath ) ) {
180
- // we saved some file space. remove original and replace with resized image.
181
- unlink( $oldpath );
182
- rename( $newpath, $oldpath );
183
- $meta['width'] = $neww;
184
- $meta['height'] = $newh;
185
-
186
- wp_update_attachment_metadata( $id, $meta );
187
-
188
- $results = array(
189
- 'success' => true,
190
- 'id' => $id,
191
- /* translators: %s: File-name of the image */
192
- 'message' => sprintf( esc_html__( 'OK: %s', 'imsanity' ), $oldpath ),
193
- );
194
- } elseif ( $newpath !== $oldpath ) {
195
- // the resized image is actually bigger in filesize (most likely due to jpg quality).
196
- // keep the old one and just get rid of the resized image.
197
- if ( is_file( $newpath ) ) {
198
- unlink( $newpath );
199
- }
200
- $results = array(
201
- 'success' => false,
202
- 'id' => $id,
203
- /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
204
- 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, esc_html__( 'Resized image was larger than the original', 'imsanity' ) ),
205
- );
206
- } else {
207
- $results = array(
208
- 'success' => false,
209
- 'id' => $id,
210
- /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
211
- 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, esc_html__( 'Unknown error, resizing function returned the same filename', 'imsanity' ) ),
212
- );
213
- }
214
- } elseif ( false === $resizeresult ) {
215
- $results = array(
216
- 'success' => false,
217
- 'id' => $id,
218
- /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
219
- 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, esc_html__( 'wp_get_image_editor missing', 'imsanity' ) ),
220
- );
221
- } else {
222
- $results = array(
223
- 'success' => false,
224
- 'id' => $id,
225
- /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
226
- 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, htmlentities( $resizeresult->get_error_message() ) ),
227
- );
228
- }
229
- } else {
230
- $results = array(
231
- 'success' => true,
232
- 'id' => $id,
233
- /* translators: %s: File-name of the image */
234
- 'message' => sprintf( esc_html__( 'SKIPPED: %s (Resize not required)', 'imsanity' ), $oldpath ) . " -- $oldw x $oldh",
235
- );
236
- if ( empty( $meta['width'] ) || empty( $meta['height'] ) ) {
237
- if ( empty( $meta['width'] ) || $meta['width'] > $oldw ) {
238
- $meta['width'] = $oldw;
239
- }
240
- if ( empty( $meta['height'] ) || $meta['height'] > $oldh ) {
241
- $meta['height'] = $oldh;
242
- }
243
- wp_update_attachment_metadata( $id, $meta );
244
- }
245
- }
246
- } else {
247
- $results = array(
248
- 'success' => false,
249
- 'id' => $id,
250
- /* translators: %s: ID number of the image */
251
- 'message' => sprintf( esc_html__( 'ERROR: Attachment with ID of %d not found', 'imsanity' ), intval( $id ) ),
252
- );
253
- }
254
-
255
- // If there is a quota we need to reset the directory size cache so it will re-calculate.
256
- delete_transient( 'dirsize_cache' );
257
 
258
  die( json_encode( $results ) );
259
  }
6
  */
7
 
8
  add_action( 'wp_ajax_imsanity_get_images', 'imsanity_get_images' );
9
+ add_action( 'wp_ajax_imsanity_resize_image', 'imsanity_ajax_resize' );
10
 
11
  /**
12
  * Verifies that the current user has administrator permission and, if not,
114
  * Resizes the image with the given id according to the configured max width and height settings
115
  * renders a json response indicating success/failure and dies
116
  */
117
+ function imsanity_ajax_resize() {
118
  imsanity_verify_permission();
119
 
 
 
120
  $id = (int) $_POST['id'];
121
 
122
  if ( ! $id ) {
129
  )
130
  );
131
  }
132
+ $results = imsanity_resize_from_id( $id );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  die( json_encode( $results ) );
135
  }
changelog.txt CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  = 2.5.0 =
2
  * added: imsanity_allowed_mimes filter to override the default list of image formats allowed
3
  * added: imsanity_orientation filter to modify auto-rotation behavior, return 1 to bypass
1
+ = 2.6.0 =
2
+ * added: wp-cli command 'wp help imsanity resize'
3
+ * fixed: adding an image to a post in pre-draft status uses wrong settings/dimensions
4
+
5
  = 2.5.0 =
6
  * added: imsanity_allowed_mimes filter to override the default list of image formats allowed
7
  * added: imsanity_orientation filter to modify auto-rotation behavior, return 1 to bypass
class-imsanity-cli.php ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Class file for Imsanity_CLI
4
+ *
5
+ * Imsanity_CLI contains an extension for WP-CLI to enable bulk resizing of images via command line.
6
+ *
7
+ * @package Imsanity
8
+ */
9
+
10
+ if ( ! defined( 'ABSPATH' ) ) {
11
+ exit;
12
+ }
13
+ /**
14
+ * Implements wp-cli extension for bulk resizing.
15
+ */
16
+ class Imsanity_CLI extends WP_CLI_Command {
17
+ /**
18
+ * Bulk Resize Images
19
+ *
20
+ * ## OPTIONS
21
+ *
22
+ * <noprompt>
23
+ * : do not prompt, just resize everything
24
+ *
25
+ * ## EXAMPLES
26
+ *
27
+ * wp-cli imsanity resize --noprompt
28
+ *
29
+ * @synopsis [--noprompt]
30
+ *
31
+ * @param array $args A numbered array of arguments provided via WP-CLI without option names.
32
+ * @param array $assoc_args An array of named arguments provided via WP-CLI.
33
+ */
34
+ function resize( $args, $assoc_args ) {
35
+
36
+ // let's get started, shall we?
37
+ // imsanity_init();.
38
+ $maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
39
+ $maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
40
+
41
+ if ( empty( $assoc_args['noprompt'] ) ) {
42
+ WP_CLI::warning(
43
+ __( 'Bulk Resize will alter your original images and cannot be undone!', 'imsanity' ) . "\n" .
44
+ __( 'It is HIGHLY recommended that you backup your wp-content/uploads folder before proceeding. You will be prompted before resizing each image.', 'imsanity' ) . "\n" .
45
+ __( 'It is also recommended that you initially resize only 1 or 2 images and verify that everything is working properly before processing your entire library.', 'imsanity' )
46
+ );
47
+ }
48
+
49
+ /* translators: 1: width in pixels, 2: height in pixels */
50
+ WP_CLI::line( sprintf( __( 'Resizing images to %1$d x %2$d', 'imsanity' ), $maxw, $maxh ) );
51
+
52
+ global $wpdb;
53
+ $attachments = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE (post_type = 'attachment' OR post_type = 'ims_image') AND post_mime_type LIKE '%%image%%' ORDER BY ID DESC" );
54
+
55
+ $image_count = count( $attachments );
56
+ if ( ! $image_count ) {
57
+ WP_CLI::success( __( 'There are no images to resize.', 'imsanity' ) );
58
+ return;
59
+ } elseif ( empty( $assoc_args['noprompt'] ) ) {
60
+ WP_CLI::confirm(
61
+ /* translators: %d: number of images */
62
+ sprintf( __( 'There are %d images to check.', 'imsanity' ), $image_count ) .
63
+ ' ' . __( 'Continue?', 'imsanity' )
64
+ );
65
+ } else {
66
+ /* translators: %d: number of images */
67
+ WP_CLI::line( sprintf( __( 'There are %d images to check.', 'imsanity' ), $image_count ) );
68
+ }
69
+
70
+ $images_finished = 0;
71
+ foreach ( $attachments as $id ) {
72
+ $imagew = false;
73
+ $imageh = false;
74
+ $images_finished++;
75
+
76
+ $path = get_attached_file( $id );
77
+ if ( $path ) {
78
+ list( $imagew, $imageh ) = getimagesize( $path );
79
+ }
80
+ if ( empty( $imagew ) || empty( $imageh ) ) {
81
+ continue;
82
+ }
83
+
84
+ if ( $imagew <= $maxw && $imageh <= $maxh ) {
85
+ /* translators: %s: File-name of the image */
86
+ WP_CLI::line( sprintf( esc_html__( 'SKIPPED: %s (Resize not required)', 'imsanity' ), $path ) . " -- $imagew x $imageh" );
87
+ continue;
88
+ }
89
+
90
+ $confirm = '';
91
+ if ( empty( $assoc_args['noprompt'] ) ) {
92
+ $confirm = \cli\prompt(
93
+ $path . ': ' . $imagew . 'x' . $imageh .
94
+ "\n" . __( 'Resize (Y/n)?', 'imsanity' )
95
+ );
96
+ }
97
+ if ( 'n' === $confirm ) {
98
+ continue;
99
+ }
100
+
101
+ $result = imsanity_resize_from_id( $id );
102
+
103
+ if ( $result['success'] ) {
104
+ WP_CLI::line( $result['message'] . " $images_finished / $image_count" );
105
+ } else {
106
+ WP_CLI::warning( $result['message'] . " $images_finished / $image_count" );
107
+ }
108
+ }
109
+
110
+ // and let the user know we are done.
111
+ WP_CLI::success( __( 'Finished Resizing!', 'imsanity' ) );
112
+ }
113
+ }
114
+
115
+ WP_CLI::add_command( 'imsanity', 'Imsanity_CLI' );
imsanity.php CHANGED
@@ -15,7 +15,7 @@ Description: Imsanity stops insanely huge image uploads
15
  Author: Exactly WWW
16
  Text Domain: imsanity
17
  Domain Path: /languages
18
- Version: 2.5.0
19
  Author URI: https://ewww.io/
20
  License: GPLv3
21
  */
@@ -24,7 +24,7 @@ if ( ! defined( 'ABSPATH' ) ) {
24
  exit;
25
  }
26
 
27
- define( 'IMSANITY_VERSION', '2.5.0' );
28
  define( 'IMSANITY_SCHEMA_VERSION', '1.1' );
29
 
30
  define( 'IMSANITY_DEFAULT_MAX_WIDTH', 1920 );
@@ -67,6 +67,30 @@ function imsanity_init() {
67
  require_once( plugin_dir_path( __FILE__ ) . 'libs/utils.php' );
68
  require_once( plugin_dir_path( __FILE__ ) . 'settings.php' );
69
  require_once( plugin_dir_path( __FILE__ ) . 'ajax.php' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  /**
72
  * Inspects the request and determines where the upload came from.
@@ -74,25 +98,45 @@ require_once( plugin_dir_path( __FILE__ ) . 'ajax.php' );
74
  * @return IMSANITY_SOURCE_POST | IMSANITY_SOURCE_LIBRARY | IMSANITY_SOURCE_OTHER
75
  */
76
  function imsanity_get_source() {
 
77
  $id = array_key_exists( 'post_id', $_REQUEST ) ? (int) $_REQUEST['post_id'] : '';
78
  $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : '';
 
79
 
 
 
 
 
 
 
 
 
 
80
  // A post_id indicates image is attached to a post.
81
  if ( $id > 0 ) {
 
82
  return IMSANITY_SOURCE_POST;
83
  }
84
 
85
  // If the referrer is the post editor, that's a good indication the image is attached to a post.
86
  if ( ! empty( $_SERVER['HTTP_REFERER'] ) && strpos( $_SERVER['HTTP_REFERER'], '/post.php' ) ) {
 
 
 
 
 
 
87
  return IMSANITY_SOURCE_POST;
88
  }
89
 
90
  // Post_id of 0 is 3.x otherwise use the action parameter.
91
  if ( 0 === $id || 'upload-attachment' === $action ) {
 
92
  return IMSANITY_SOURCE_LIBRARY;
93
  }
94
 
95
  // We don't know where this one came from but $_REQUEST['_wp_http_referer'] may contain info.
 
96
  return IMSANITY_SOURCE_OTHER;
97
  }
98
 
15
  Author: Exactly WWW
16
  Text Domain: imsanity
17
  Domain Path: /languages
18
+ Version: 2.6.0
19
  Author URI: https://ewww.io/
20
  License: GPLv3
21
  */
24
  exit;
25
  }
26
 
27
+ define( 'IMSANITY_VERSION', '2.6.0' );
28
  define( 'IMSANITY_SCHEMA_VERSION', '1.1' );
29
 
30
  define( 'IMSANITY_DEFAULT_MAX_WIDTH', 1920 );
67
  require_once( plugin_dir_path( __FILE__ ) . 'libs/utils.php' );
68
  require_once( plugin_dir_path( __FILE__ ) . 'settings.php' );
69
  require_once( plugin_dir_path( __FILE__ ) . 'ajax.php' );
70
+ if ( defined( 'WP_CLI' ) && WP_CLI ) {
71
+ require_once( plugin_dir_path( __FILE__ ) . 'class-imsanity-cli.php' );
72
+ }
73
+
74
+ /**
75
+ * Use the EWWW IO debugging functions.
76
+ *
77
+ * @param string $message A message to send to the debugger.
78
+ */
79
+ function imsanity_debug( $message ) {
80
+ if ( function_exists( 'ewwwio_debug_message' ) ) {
81
+ if ( ! is_string( $message ) ) {
82
+ if ( function_exists( 'print_r' ) ) {
83
+ $message = print_r( $message, true );
84
+ } else {
85
+ $message = 'not a string, print_r disabled';
86
+ }
87
+ }
88
+ ewwwio_debug_message( $message );
89
+ if ( function_exists( 'ewww_image_optimizer_debug_log' ) ) {
90
+ ewww_image_optimizer_debug_log();
91
+ }
92
+ }
93
+ }
94
 
95
  /**
96
  * Inspects the request and determines where the upload came from.
98
  * @return IMSANITY_SOURCE_POST | IMSANITY_SOURCE_LIBRARY | IMSANITY_SOURCE_OTHER
99
  */
100
  function imsanity_get_source() {
101
+ imsanity_debug( __FUNCTION__ );
102
  $id = array_key_exists( 'post_id', $_REQUEST ) ? (int) $_REQUEST['post_id'] : '';
103
  $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : '';
104
+ imsanity_debug( "getting source for id=$id and action=$action" );
105
 
106
+ imsanity_debug( $_SERVER );
107
+ if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
108
+ imsanity_debug( '_wp_http_referer:' );
109
+ imsanity_debug( $_REQUEST['_wp_http_referer'] );
110
+ }
111
+ if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
112
+ imsanity_debug( 'http_referer:' );
113
+ imsanity_debug( $_SERVER['HTTP_REFERER'] );
114
+ }
115
  // A post_id indicates image is attached to a post.
116
  if ( $id > 0 ) {
117
+ imsanity_debug( 'from a post (id)' );
118
  return IMSANITY_SOURCE_POST;
119
  }
120
 
121
  // If the referrer is the post editor, that's a good indication the image is attached to a post.
122
  if ( ! empty( $_SERVER['HTTP_REFERER'] ) && strpos( $_SERVER['HTTP_REFERER'], '/post.php' ) ) {
123
+ imsanity_debug( 'from a post.php' );
124
+ return IMSANITY_SOURCE_POST;
125
+ }
126
+ // If the referrer is the (new) post editor, that's a good indication the image is attached to a post.
127
+ if ( ! empty( $_SERVER['HTTP_REFERER'] ) && strpos( $_SERVER['HTTP_REFERER'], '/post-new.php' ) ) {
128
+ imsanity_debug( 'from a new post' );
129
  return IMSANITY_SOURCE_POST;
130
  }
131
 
132
  // Post_id of 0 is 3.x otherwise use the action parameter.
133
  if ( 0 === $id || 'upload-attachment' === $action ) {
134
+ imsanity_debug( 'from the library' );
135
  return IMSANITY_SOURCE_LIBRARY;
136
  }
137
 
138
  // We don't know where this one came from but $_REQUEST['_wp_http_referer'] may contain info.
139
+ imsanity_debug( 'unknown source' );
140
  return IMSANITY_SOURCE_OTHER;
141
  }
142
 
libs/utils.php CHANGED
@@ -119,6 +119,141 @@ function imsanity_fatal( $message, $title = '', $die = false ) {
119
  }
120
  }
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  /**
123
  * Replacement for deprecated image_resize function
124
  *
119
  }
120
  }
121
 
122
+ /**
123
+ * Resizes the image with the given id according to the configured max width and height settings.
124
+ *
125
+ * @param int $id The attachment ID of the image to process.
126
+ * @return array The success status (bool) and a message to display.
127
+ */
128
+ function imsanity_resize_from_id( $id = 0 ) {
129
+
130
+ $id = (int) $id;
131
+
132
+ if ( ! $id ) {
133
+ return;
134
+ }
135
+
136
+ $meta = wp_get_attachment_metadata( $id );
137
+
138
+ if ( $meta && is_array( $meta ) ) {
139
+ $uploads = wp_upload_dir();
140
+ $oldpath = imsanity_attachment_path( $meta['file'], $id, '', false );
141
+ if ( empty( $oldpath ) || ! is_writable( $oldpath ) ) {
142
+ /* translators: %s: File-name of the image */
143
+ $msg = sprintf( esc_html__( '%s is not writable', 'imsanity' ), $meta['file'] );
144
+ return array(
145
+ 'success' => false,
146
+ 'message' => $msg,
147
+ );
148
+ }
149
+
150
+ $maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
151
+ $maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
152
+
153
+ // method one - slow but accurate, get file size from file itself.
154
+ list( $oldw, $oldh ) = getimagesize( $oldpath );
155
+ // method two - get file size from meta, fast but resize will fail if meta is out of sync.
156
+ if ( ! $oldw || ! $oldh ) {
157
+ $oldw = $meta['width'];
158
+ $oldh = $meta['height'];
159
+ }
160
+
161
+ if ( ( $oldw > $maxw && $maxw > 0 ) || ( $oldh > $maxh && $maxh > 0 ) ) {
162
+ $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
163
+
164
+ if ( $maxw > 0 && $maxh > 0 && $oldw >= $maxw && $oldh >= $maxh && ( $oldh > $maxh || $oldw > $maxw ) && apply_filters( 'imsanity_crop_image', false ) ) {
165
+ $neww = $maxw;
166
+ $newh = $maxh;
167
+ } else {
168
+ list( $neww, $newh ) = wp_constrain_dimensions( $oldw, $oldh, $maxw, $maxh );
169
+ }
170
+
171
+ $resizeresult = imsanity_image_resize( $oldpath, $neww, $newh, apply_filters( 'imsanity_crop_image', false ), null, null, $quality );
172
+
173
+ if ( $resizeresult && ! is_wp_error( $resizeresult ) ) {
174
+ $newpath = $resizeresult;
175
+
176
+ if ( $newpath !== $oldpath && is_file( $newpath ) && filesize( $newpath ) < filesize( $oldpath ) ) {
177
+ // we saved some file space. remove original and replace with resized image.
178
+ unlink( $oldpath );
179
+ rename( $newpath, $oldpath );
180
+ $meta['width'] = $neww;
181
+ $meta['height'] = $newh;
182
+
183
+ wp_update_attachment_metadata( $id, $meta );
184
+
185
+ $results = array(
186
+ 'success' => true,
187
+ 'id' => $id,
188
+ /* translators: %s: File-name of the image */
189
+ 'message' => sprintf( esc_html__( 'OK: %s', 'imsanity' ), $oldpath ),
190
+ );
191
+ } elseif ( $newpath !== $oldpath ) {
192
+ // the resized image is actually bigger in filesize (most likely due to jpg quality).
193
+ // keep the old one and just get rid of the resized image.
194
+ if ( is_file( $newpath ) ) {
195
+ unlink( $newpath );
196
+ }
197
+ $results = array(
198
+ 'success' => false,
199
+ 'id' => $id,
200
+ /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
201
+ 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, esc_html__( 'Resized image was larger than the original', 'imsanity' ) ),
202
+ );
203
+ } else {
204
+ $results = array(
205
+ 'success' => false,
206
+ 'id' => $id,
207
+ /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
208
+ 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, esc_html__( 'Unknown error, resizing function returned the same filename', 'imsanity' ) ),
209
+ );
210
+ }
211
+ } elseif ( false === $resizeresult ) {
212
+ $results = array(
213
+ 'success' => false,
214
+ 'id' => $id,
215
+ /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
216
+ 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, esc_html__( 'wp_get_image_editor missing', 'imsanity' ) ),
217
+ );
218
+ } else {
219
+ $results = array(
220
+ 'success' => false,
221
+ 'id' => $id,
222
+ /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
223
+ 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, htmlentities( $resizeresult->get_error_message() ) ),
224
+ );
225
+ }
226
+ } else {
227
+ $results = array(
228
+ 'success' => true,
229
+ 'id' => $id,
230
+ /* translators: %s: File-name of the image */
231
+ 'message' => sprintf( esc_html__( 'SKIPPED: %s (Resize not required)', 'imsanity' ), $oldpath ) . " -- $oldw x $oldh",
232
+ );
233
+ if ( empty( $meta['width'] ) || empty( $meta['height'] ) ) {
234
+ if ( empty( $meta['width'] ) || $meta['width'] > $oldw ) {
235
+ $meta['width'] = $oldw;
236
+ }
237
+ if ( empty( $meta['height'] ) || $meta['height'] > $oldh ) {
238
+ $meta['height'] = $oldh;
239
+ }
240
+ wp_update_attachment_metadata( $id, $meta );
241
+ }
242
+ }
243
+ } else {
244
+ $results = array(
245
+ 'success' => false,
246
+ 'id' => $id,
247
+ /* translators: %s: ID number of the image */
248
+ 'message' => sprintf( esc_html__( 'ERROR: Attachment with ID of %d not found', 'imsanity' ), intval( $id ) ),
249
+ );
250
+ }
251
+
252
+ // If there is a quota we need to reset the directory size cache so it will re-calculate.
253
+ delete_transient( 'dirsize_cache' );
254
+
255
+ return $results;
256
+ }
257
  /**
258
  * Replacement for deprecated image_resize function
259
  *
readme.txt CHANGED
@@ -1,11 +1,11 @@
1
  === Imsanity ===
2
  Contributors: nosilver4u
3
- Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MKMQKCBFFG3WW
4
  Tags: image, scale, resize, space saver, quality
5
  Requires at least: 5.0
6
- Tested up to: 5.3
7
  Requires PHP: 5.6
8
- Stable tag: 2.5.0
9
  License: GPLv3
10
 
11
  Imsanity automatically resizes huge image uploads. Are contributors uploading huge photos? Tired of manually scaling? Imsanity to the rescue!
@@ -25,6 +25,8 @@ This plugin is ideal for blogs that do not require hi-resolution original images
25
  to be stored and/or the contributors don't want (or understand how) to scale images
26
  before uploading.
27
 
 
 
28
  = Features =
29
 
30
  * Automatically scales large image uploads to a more "sane" size
@@ -135,6 +137,10 @@ Questions may be posted on the support forum at https://wordpress.org/support/pl
135
 
136
  == Changelog ==
137
 
 
 
 
 
138
  = 2.5.0 =
139
  * added: imsanity_allowed_mimes filter to override the default list of image formats allowed
140
  * added: imsanity_orientation filter to modify auto-rotation behavior, return 1 to bypass
1
  === Imsanity ===
2
  Contributors: nosilver4u
3
+ Donate link: https://ewww.io/donate/
4
  Tags: image, scale, resize, space saver, quality
5
  Requires at least: 5.0
6
+ Tested up to: 5.4
7
  Requires PHP: 5.6
8
+ Stable tag: 2.6.0
9
  License: GPLv3
10
 
11
  Imsanity automatically resizes huge image uploads. Are contributors uploading huge photos? Tired of manually scaling? Imsanity to the rescue!
25
  to be stored and/or the contributors don't want (or understand how) to scale images
26
  before uploading.
27
 
28
+ [](http://coderisk.com/wp/plugin/imsanity/RIPS-YIn4SbnOjz)
29
+
30
  = Features =
31
 
32
  * Automatically scales large image uploads to a more "sane" size
137
 
138
  == Changelog ==
139
 
140
+ = 2.6.0 =
141
+ * added: wp-cli command 'wp help imsanity resize'
142
+ * fixed: adding an image to a post in pre-draft status uses wrong settings/dimensions
143
+
144
  = 2.5.0 =
145
  * added: imsanity_allowed_mimes filter to override the default list of image formats allowed
146
  * added: imsanity_orientation filter to modify auto-rotation behavior, return 1 to bypass
settings.php CHANGED
@@ -638,13 +638,14 @@ function imsanity_settings_page() {
638
 
639
  <div id="imsanity_header">
640
  <p><?php esc_html_e( 'If you have existing images that were uploaded prior to installing Imsanity, you may resize them all in bulk to recover disk space. To begin, click the "Search Images" button to search all existing attachments for images that are larger than the configured limit.', 'imsanity' ); ?></p>
 
 
641
  <?php /* translators: %d: the number of images */ ?>
642
  <p><?php printf( esc_html__( 'NOTE: To give you greater control over the resizing process, a maximum of %d images will be returned at one time. Bitmap images cannot be bulk resized and will not appear in the search results.', 'imsanity' ), IMSANITY_AJAX_MAX_RECORDS ); ?></p>
643
  </div>
644
 
645
  <div style="border: solid 1px #ff6666; background-color: #ffbbbb; padding: 0 10px;">
646
  <h4><?php esc_html_e( 'WARNING: Bulk Resize will alter your original images and cannot be undone!', 'imsanity' ); ?></h4>
647
-
648
  <p><?php esc_html_e( 'It is HIGHLY recommended that you backup your wp-content/uploads folder before proceeding. You will have a chance to preview and select the images to convert.', 'imsanity' ); ?><br>
649
  <?php esc_html_e( 'It is also recommended that you initially select only 1 or 2 images and verify that everything is working properly before processing your entire library.', 'imsanity' ); ?></p>
650
  </div>
638
 
639
  <div id="imsanity_header">
640
  <p><?php esc_html_e( 'If you have existing images that were uploaded prior to installing Imsanity, you may resize them all in bulk to recover disk space. To begin, click the "Search Images" button to search all existing attachments for images that are larger than the configured limit.', 'imsanity' ); ?></p>
641
+ <?php /* translators: %s: the WP-CLI command to run */ ?>
642
+ <p><?php printf( esc_html__( 'You may also use WP-CLI to resize your images: %s', 'imsanity' ), '<code>wp help imsanity resize</code>' ); ?></p>
643
  <?php /* translators: %d: the number of images */ ?>
644
  <p><?php printf( esc_html__( 'NOTE: To give you greater control over the resizing process, a maximum of %d images will be returned at one time. Bitmap images cannot be bulk resized and will not appear in the search results.', 'imsanity' ), IMSANITY_AJAX_MAX_RECORDS ); ?></p>
645
  </div>
646
 
647
  <div style="border: solid 1px #ff6666; background-color: #ffbbbb; padding: 0 10px;">
648
  <h4><?php esc_html_e( 'WARNING: Bulk Resize will alter your original images and cannot be undone!', 'imsanity' ); ?></h4>
 
649
  <p><?php esc_html_e( 'It is HIGHLY recommended that you backup your wp-content/uploads folder before proceeding. You will have a chance to preview and select the images to convert.', 'imsanity' ); ?><br>
650
  <?php esc_html_e( 'It is also recommended that you initially select only 1 or 2 images and verify that everything is working properly before processing your entire library.', 'imsanity' ); ?></p>
651
  </div>