PluginProbe
Imsanity / 2.7.0
Imsanity v2.7.0
2.9.4 2.9.3 2.9.2 trunk 2.4.3 2.5.0 2.6.0 2.7.0 2.7.2 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.9.0 2.9.1
imsanity / imsanity.php

imsanity.php in Imsanity 2.7.0, at imsanity.php

371 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main file for Imsanity plugin.
4 *
5 * This file includes the core of Imsanity and the top-level image handler.
6 *
7 * @link https://wordpress.org/plugins/imsanity/
8 * @package Imsanity
9 */
10
11 /*
12 Plugin Name: Imsanity
13 Plugin URI: https://wordpress.org/plugins/imsanity/
14 Description: Imsanity stops insanely huge image uploads
15 Author: Exactly WWW
16 Text Domain: imsanity
17 Domain Path: /languages
18 Version: 2.7.0
19 Author URI: https://ewww.io/
20 License: GPLv3
21 */
22
23 if ( ! defined( 'ABSPATH' ) ) {
24 exit;
25 }
26
27 define( 'IMSANITY_VERSION', '2.7.0' );
28 define( 'IMSANITY_SCHEMA_VERSION', '1.1' );
29
30 define( 'IMSANITY_DEFAULT_MAX_WIDTH', 1920 );
31 define( 'IMSANITY_DEFAULT_MAX_HEIGHT', 1920 );
32 define( 'IMSANITY_DEFAULT_BMP_TO_JPG', true );
33 define( 'IMSANITY_DEFAULT_PNG_TO_JPG', false );
34 define( 'IMSANITY_DEFAULT_QUALITY', 82 );
35
36 define( 'IMSANITY_SOURCE_POST', 1 );
37 define( 'IMSANITY_SOURCE_LIBRARY', 2 );
38 define( 'IMSANITY_SOURCE_OTHER', 4 );
39
40 if ( ! defined( 'IMSANITY_AJAX_MAX_RECORDS' ) ) {
41 define( 'IMSANITY_AJAX_MAX_RECORDS', 250 );
42 }
43
44 /**
45 * The full path of the main plugin file.
46 *
47 * @var string IMSANITY_PLUGIN_FILE
48 */
49 define( 'IMSANITY_PLUGIN_FILE', __FILE__ );
50 /**
51 * The path of the main plugin file, relative to the plugins/ folder.
52 *
53 * @var string IMSANITY_PLUGIN_FILE_REL
54 */
55 define( 'IMSANITY_PLUGIN_FILE_REL', plugin_basename( __FILE__ ) );
56
57 /**
58 * Load translations for Imsanity.
59 */
60 function imsanity_init() {
61 load_plugin_textdomain( 'imsanity', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
62 }
63
64 /**
65 * Import supporting libraries.
66 */
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 require_once( plugin_dir_path( __FILE__ ) . 'media.php' );
71 if ( defined( 'WP_CLI' ) && WP_CLI ) {
72 require_once( plugin_dir_path( __FILE__ ) . 'class-imsanity-cli.php' );
73 }
74
75 /**
76 * Use the EWWW IO debugging functions (if available).
77 *
78 * @param string $message A message to send to the debugger.
79 */
80 function imsanity_debug( $message ) {
81 if ( function_exists( 'ewwwio_debug_message' ) ) {
82 if ( ! is_string( $message ) ) {
83 if ( function_exists( 'print_r' ) ) {
84 $message = print_r( $message, true );
85 } else {
86 $message = 'not a string, print_r disabled';
87 }
88 }
89 ewwwio_debug_message( $message );
90 if ( function_exists( 'ewww_image_optimizer_debug_log' ) ) {
91 ewww_image_optimizer_debug_log();
92 }
93 }
94 }
95
96 /**
97 * Inspects the request and determines where the upload came from.
98 *
99 * @return IMSANITY_SOURCE_POST | IMSANITY_SOURCE_LIBRARY | IMSANITY_SOURCE_OTHER
100 */
101 function imsanity_get_source() {
102 imsanity_debug( __FUNCTION__ );
103 $id = array_key_exists( 'post_id', $_REQUEST ) ? (int) $_REQUEST['post_id'] : '';
104 $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : '';
105 imsanity_debug( "getting source for id=$id and action=$action" );
106
107 imsanity_debug( $_SERVER );
108 if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
109 imsanity_debug( '_wp_http_referer:' );
110 imsanity_debug( $_REQUEST['_wp_http_referer'] );
111 }
112 if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
113 imsanity_debug( 'http_referer:' );
114 imsanity_debug( $_SERVER['HTTP_REFERER'] );
115 }
116 // A post_id indicates image is attached to a post.
117 if ( $id > 0 ) {
118 imsanity_debug( 'from a post (id)' );
119 return IMSANITY_SOURCE_POST;
120 }
121
122 // If the referrer is the post editor, that's a good indication the image is attached to a post.
123 if ( ! empty( $_SERVER['HTTP_REFERER'] ) && strpos( $_SERVER['HTTP_REFERER'], '/post.php' ) ) {
124 imsanity_debug( 'from a post.php' );
125 return IMSANITY_SOURCE_POST;
126 }
127 // If the referrer is the (new) post editor, that's a good indication the image is attached to a post.
128 if ( ! empty( $_SERVER['HTTP_REFERER'] ) && strpos( $_SERVER['HTTP_REFERER'], '/post-new.php' ) ) {
129 imsanity_debug( 'from a new post' );
130 return IMSANITY_SOURCE_POST;
131 }
132
133 // Post_id of 0 is 3.x otherwise use the action parameter.
134 if ( 0 === $id || 'upload-attachment' === $action ) {
135 imsanity_debug( 'from the library' );
136 return IMSANITY_SOURCE_LIBRARY;
137 }
138
139 // We don't know where this one came from but $_REQUEST['_wp_http_referer'] may contain info.
140 imsanity_debug( 'unknown source' );
141 return IMSANITY_SOURCE_OTHER;
142 }
143
144 /**
145 * Given the source, returns the max width/height.
146 *
147 * @example: list( $w, $h ) = imsanity_get_max_width_height( IMSANITY_SOURCE_LIBRARY );
148 * @param int $source One of IMSANITY_SOURCE_POST | IMSANITY_SOURCE_LIBRARY | IMSANITY_SOURCE_OTHER.
149 */
150 function imsanity_get_max_width_height( $source ) {
151 $w = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
152 $h = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
153
154 switch ( $source ) {
155 case IMSANITY_SOURCE_POST:
156 break;
157 case IMSANITY_SOURCE_LIBRARY:
158 $w = imsanity_get_option( 'imsanity_max_width_library', $w );
159 $h = imsanity_get_option( 'imsanity_max_height_library', $h );
160 break;
161 default:
162 $w = imsanity_get_option( 'imsanity_max_width_other', $w );
163 $h = imsanity_get_option( 'imsanity_max_height_other', $h );
164 break;
165 }
166
167 // NOTE: filters MUST return an array of 2 items, or the defaults will be used.
168 return apply_filters( 'imsanity_get_max_width_height', array( $w, $h ), $source );
169 }
170
171 /**
172 * Handler after a file has been uploaded. If the file is an image, check the size
173 * to see if it is too big and, if so, resize and overwrite the original.
174 *
175 * @param Array $params The parameters submitted with the upload.
176 */
177 function imsanity_handle_upload( $params ) {
178
179 // If "noresize" is included in the filename then we will bypass imsanity scaling.
180 if ( strpos( $params['file'], 'noresize' ) !== false ) {
181 return $params;
182 }
183
184 if ( apply_filters( 'imsanity_skip_image', false, $params['file'] ) ) {
185 return $params;
186 }
187
188 // If preferences specify so then we can convert an original bmp or png file into jpg.
189 if ( ( 'image/bmp' === $params['type'] || 'image/x-ms-bmp' === $params['type'] ) && imsanity_get_option( 'imsanity_bmp_to_jpg', IMSANITY_DEFAULT_BMP_TO_JPG ) ) {
190 $params = imsanity_convert_to_jpg( 'bmp', $params );
191 }
192
193 if ( 'image/png' === $params['type'] && imsanity_get_option( 'imsanity_png_to_jpg', IMSANITY_DEFAULT_PNG_TO_JPG ) ) {
194 $params = imsanity_convert_to_jpg( 'png', $params );
195 }
196
197 // Make sure this is a type of image that we want to convert and that it exists.
198 $oldpath = $params['file'];
199
200 // Let folks filter the allowed mime-types for resizing.
201 $allowed_types = apply_filters( 'imsanity_allowed_mimes', array( 'image/png', 'image/gif', 'image/jpeg' ), $oldpath );
202 if ( is_string( $allowed_types ) ) {
203 $allowed_types = array( $allowed_types );
204 } elseif ( ! is_array( $allowed_types ) ) {
205 $allowed_types = array();
206 }
207
208 if (
209 ( ! is_wp_error( $params ) ) &&
210 is_file( $oldpath ) &&
211 is_readable( $oldpath ) &&
212 is_writable( $oldpath ) &&
213 filesize( $oldpath ) > 0 &&
214 in_array( $params['type'], $allowed_types, true )
215 ) {
216
217 // figure out where the upload is coming from.
218 $source = imsanity_get_source();
219
220 $maxw = IMSANITY_DEFAULT_MAX_WIDTH;
221 $maxh = IMSANITY_DEFAULT_MAX_HEIGHT;
222 $max_width_height = imsanity_get_max_width_height( $source );
223 if ( is_array( $max_width_height ) && 2 === count( $max_width_height ) ) {
224 list( $maxw, $maxh ) = $max_width_height;
225 }
226
227 list( $oldw, $oldh ) = getimagesize( $oldpath );
228
229 if ( ( $oldw > $maxw && $maxw > 0 ) || ( $oldh > $maxh && $maxh > 0 ) ) {
230 $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
231
232 $ftype = imsanity_quick_mimetype( $oldpath );
233 $orientation = imsanity_get_orientation( $oldpath, $ftype );
234 // If we are going to rotate the image 90 degrees during the resize, swap the existing image dimensions.
235 if ( 6 === (int) $orientation || 8 === (int) $orientation ) {
236 $old_oldw = $oldw;
237 $oldw = $oldh;
238 $oldh = $old_oldw;
239 }
240
241 if ( $maxw > 0 && $maxh > 0 && $oldw >= $maxw && $oldh >= $maxh && ( $oldh > $maxh || $oldw > $maxw ) && apply_filters( 'imsanity_crop_image', false ) ) {
242 $neww = $maxw;
243 $newh = $maxh;
244 } else {
245 list( $neww, $newh ) = wp_constrain_dimensions( $oldw, $oldh, $maxw, $maxh );
246 }
247
248 global $ewww_preempt_editor;
249 if ( ! isset( $ewww_preempt_editor ) ) {
250 $ewww_preempt_editor = false;
251 }
252 $original_preempt = $ewww_preempt_editor;
253 $ewww_preempt_editor = true;
254 $resizeresult = imsanity_image_resize( $oldpath, $neww, $newh, apply_filters( 'imsanity_crop_image', false ), null, null, $quality );
255 $ewww_preempt_editor = $original_preempt;
256
257 if ( $resizeresult && ! is_wp_error( $resizeresult ) ) {
258 $newpath = $resizeresult;
259
260 if ( is_file( $newpath ) && filesize( $newpath ) < filesize( $oldpath ) ) {
261 // we saved some file space. remove original and replace with resized image.
262 unlink( $oldpath );
263 rename( $newpath, $oldpath );
264 } elseif ( is_file( $newpath ) ) {
265 // theresized image is actually bigger in filesize (most likely due to jpg quality).
266 // keep the old one and just get rid of the resized image.
267 unlink( $newpath );
268 }
269 } elseif ( false === $resizeresult ) {
270 return $params;
271 } elseif ( is_wp_error( $resizeresult ) ) {
272 // resize didn't work, likely because the image processing libraries are missing.
273 // remove the old image so we don't leave orphan files hanging around.
274 unlink( $oldpath );
275
276 $params = wp_handle_upload_error(
277 $oldpath,
278 sprintf(
279 /* translators: 1: error message 2: link to support forums */
280 esc_html__( 'Imsanity was unable to resize this image for the following reason: %1$s. If you continue to see this error message, you may need to install missing server components. If you think you have discovered a bug, please report it on the Imsanity support forum: %2$s', 'imsanity' ),
281 $resizeresult->get_error_message(),
282 'https://wordpress.org/support/plugin/imsanity'
283 )
284 );
285 } else {
286 return $params;
287 }
288 }
289 }
290 clearstatcache();
291 return $params;
292 }
293
294
295 /**
296 * Read in the image file from the params and then save as a new jpg file.
297 * if successful, remove the original image and alter the return
298 * parameters to return the new jpg instead of the original
299 *
300 * @param string $type Type of the image to be converted: 'bmp' or 'png'.
301 * @param array $params The upload parameters.
302 * @return array altered params
303 */
304 function imsanity_convert_to_jpg( $type, $params ) {
305
306 if ( apply_filters( 'imsanity_disable_convert', false, $type, $params ) ) {
307 return $params;
308 }
309
310 $img = null;
311
312 if ( 'bmp' === $type ) {
313 include_once( 'libs/imagecreatefrombmp.php' );
314 $img = imagecreatefrombmp( $params['file'] );
315 } elseif ( 'png' === $type ) {
316 // Prevent converting PNG images with alpha/transparency, unless overridden by the user.
317 if ( apply_filters( 'imsanity_skip_alpha', imsanity_has_alpha( $params['file'] ), $params['file'] ) ) {
318 return $params;
319 }
320 if ( ! function_exists( 'imagecreatefrompng' ) ) {
321 return wp_handle_upload_error( $params['file'], esc_html__( 'Imsanity requires the GD library to convert PNG images to JPG', 'imsanity' ) );
322 }
323
324 $input = imagecreatefrompng( $params['file'] );
325 // convert png transparency to white.
326 $img = imagecreatetruecolor( imagesx( $input ), imagesy( $input ) );
327 imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
328 imagealphablending( $img, true );
329 imagecopy( $img, $input, 0, 0, 0, 0, imagesx( $input ), imagesy( $input ) );
330 } else {
331 return wp_handle_upload_error( $params['file'], esc_html__( 'Unknown image type specified in imsanity_convert_to_jpg', 'imsanity' ) );
332 }
333
334 // We need to change the extension from the original to .jpg so we have to ensure it will be a unique filename.
335 $uploads = wp_upload_dir();
336 $oldfilename = wp_basename( $params['file'] );
337 $newfilename = wp_basename( str_ireplace( '.' . $type, '.jpg', $oldfilename ) );
338 $newfilename = wp_unique_filename( $uploads['path'], $newfilename );
339
340 $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
341
342 if ( imagejpeg( $img, $uploads['path'] . '/' . $newfilename, $quality ) ) {
343 // Conversion succeeded: remove the original bmp & remap the params.
344 unlink( $params['file'] );
345
346 $params['file'] = $uploads['path'] . '/' . $newfilename;
347 $params['url'] = $uploads['url'] . '/' . $newfilename;
348 $params['type'] = 'image/jpeg';
349 } else {
350 unlink( $params['file'] );
351
352 return wp_handle_upload_error(
353 $oldfilename,
354 /* translators: %s: the image mime type */
355 sprintf( esc_html__( 'Imsanity was unable to process the %s file. If you continue to see this error you may need to disable the conversion option in the Imsanity settings.', 'imsanity' ), $type )
356 );
357 }
358
359 return $params;
360 }
361
362 // Add filter to hook into uploads.
363 add_filter( 'wp_handle_upload', 'imsanity_handle_upload' );
364 // Run necessary actions on init (loading translations mostly).
365 add_action( 'plugins_loaded', 'imsanity_init' );
366
367 // Adds a column to the media library list view to display optimization results.
368 add_filter( 'manage_media_columns', 'imsanity_media_columns' );
369 // Outputs the actual column information for each attachment.
370 add_action( 'manage_media_custom_column', 'imsanity_custom_column', 10, 2 );
371