PluginProbe
Imsanity / 2.9.2
Imsanity v2.9.2
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.9.2, at imsanity.php

433 lines 16.3 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 Domain Path: /languages
17 Version: 2.9.2
18 Requires at least: 6.7
19 Requires PHP: 7.4
20 Author URI: https://ewww.io/about/
21 License: GPLv3
22 */
23
24 if ( ! defined( 'ABSPATH' ) ) {
25 exit;
26 }
27
28 define( 'IMSANITY_VERSION', '2.9.2' );
29 define( 'IMSANITY_SCHEMA_VERSION', '1.1' );
30
31 define( 'IMSANITY_SOURCE_POST', 1 );
32 define( 'IMSANITY_SOURCE_LIBRARY', 2 );
33 define( 'IMSANITY_SOURCE_OTHER', 4 );
34
35 /**
36 * The full path of the main plugin file.
37 *
38 * @var string IMSANITY_PLUGIN_FILE
39 */
40 define( 'IMSANITY_PLUGIN_FILE', __FILE__ );
41
42 /**
43 * The directory path of the main plugin file.
44 *
45 * @var string IMSANITY_PLUGIN_DIR
46 */
47 define( 'IMSANITY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
48
49 /**
50 * The path of the main plugin file, relative to the plugins/ folder.
51 *
52 * @var string IMSANITY_PLUGIN_FILE_REL
53 */
54 define( 'IMSANITY_PLUGIN_FILE_REL', plugin_basename( __FILE__ ) );
55
56 /**
57 * Initialize the plugin, setup constants.
58 */
59 function imsanity_init() {
60 if ( ! defined( 'IMSANITY_DEFAULT_MAX_WIDTH' ) ) {
61 define( 'IMSANITY_DEFAULT_MAX_WIDTH', (int) apply_filters( 'imsanity_default_max_width', 1920 ) );
62 }
63 if ( ! defined( 'IMSANITY_DEFAULT_MAX_HEIGHT' ) ) {
64 define( 'IMSANITY_DEFAULT_MAX_HEIGHT', (int) apply_filters( 'imsanity_default_max_height', 1920 ) );
65 }
66 if ( ! defined( 'IMSANITY_DEFAULT_BMP_TO_JPG' ) ) {
67 define( 'IMSANITY_DEFAULT_BMP_TO_JPG', (bool) apply_filters( 'imsanity_default_bmp_to_jpg', true ) );
68 }
69 if ( ! defined( 'IMSANITY_DEFAULT_PNG_TO_JPG' ) ) {
70 define( 'IMSANITY_DEFAULT_PNG_TO_JPG', (bool) apply_filters( 'imsanity_default_png_to_jpg', false ) );
71 }
72 if ( ! defined( 'IMSANITY_DEFAULT_QUALITY' ) ) {
73 define( 'IMSANITY_DEFAULT_QUALITY', (int) apply_filters( 'imsanity_default_quality', 82 ) );
74 }
75 if ( ! defined( 'IMSANITY_DEFAULT_AVIF_QUALITY' ) ) {
76 define( 'IMSANITY_DEFAULT_AVIF_QUALITY', (int) apply_filters( 'imsanity_default_avif_quality', 86 ) );
77 }
78 if ( ! defined( 'IMSANITY_DEFAULT_WEBP_QUALITY' ) ) {
79 define( 'IMSANITY_DEFAULT_WEBP_QUALITY', (int) apply_filters( 'imsanity_default_webp_quality', 86 ) );
80 }
81 }
82
83 /**
84 * Load translations for Imsanity.
85 */
86 function imsanity_load_textdomain() {
87 load_plugin_textdomain( 'imsanity', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
88 }
89
90 /**
91 * Import supporting libraries.
92 */
93 require_once plugin_dir_path( __FILE__ ) . 'libs/debug.php';
94 require_once plugin_dir_path( __FILE__ ) . 'libs/utils.php';
95 require_once plugin_dir_path( __FILE__ ) . 'settings.php';
96 require_once plugin_dir_path( __FILE__ ) . 'ajax.php';
97 require_once plugin_dir_path( __FILE__ ) . 'media.php';
98 if ( defined( 'WP_CLI' ) && WP_CLI ) {
99 require_once plugin_dir_path( __FILE__ ) . 'class-imsanity-cli.php';
100 }
101
102 /**
103 * Inspects the request and determines where the upload came from.
104 *
105 * @return IMSANITY_SOURCE_POST | IMSANITY_SOURCE_LIBRARY | IMSANITY_SOURCE_OTHER
106 */
107 function imsanity_get_source() {
108 imsanity_debug( __FUNCTION__ );
109 $id = array_key_exists( 'post_id', $_REQUEST ) ? (int) $_REQUEST['post_id'] : ''; // phpcs:ignore WordPress.Security.NonceVerification
110 $action = ! empty( $_REQUEST['action'] ) ? sanitize_key( $_REQUEST['action'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
111 imsanity_debug( "getting source for id=$id and action=$action" );
112
113 // Uncomment this (and remove the trailing .) to temporarily check the full $_SERVER vars.
114 // imsanity_debug( $_SERVER );.
115 $referer = '';
116 if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
117 $referer = sanitize_text_field( wp_unslash( $_SERVER['HTTP_REFERER'] ) );
118 imsanity_debug( "http_referer: $referer" );
119 }
120
121 $request_uri = wp_referer_field( false );
122 imsanity_debug( "request URI: $request_uri" );
123
124 // A post_id indicates image is attached to a post.
125 if ( $id > 0 ) {
126 imsanity_debug( 'from a post (id)' );
127 return IMSANITY_SOURCE_POST;
128 }
129
130 // If the referrer is the post editor, that's a good indication the image is attached to a post.
131 if ( false !== strpos( $referer, '/post.php' ) ) {
132 imsanity_debug( 'from a post.php' );
133 return IMSANITY_SOURCE_POST;
134 }
135 // If the referrer is the (new) post editor, that's a good indication the image is attached to a post.
136 if ( false !== strpos( $referer, '/post-new.php' ) ) {
137 imsanity_debug( 'from a new post' );
138 return IMSANITY_SOURCE_POST;
139 }
140
141 // Post_id of 0 is 3.x otherwise use the action parameter.
142 if ( 0 === $id || 'upload-attachment' === $action ) {
143 imsanity_debug( 'from the library' );
144 return IMSANITY_SOURCE_LIBRARY;
145 }
146
147 // We don't know where this one came from but $_REQUEST['_wp_http_referer'] may contain info.
148 imsanity_debug( 'unknown source' );
149 return IMSANITY_SOURCE_OTHER;
150 }
151
152 /**
153 * Given the source, returns the max width/height.
154 *
155 * @example: list( $w, $h ) = imsanity_get_max_width_height( IMSANITY_SOURCE_LIBRARY );
156 * @param int $source One of IMSANITY_SOURCE_POST | IMSANITY_SOURCE_LIBRARY | IMSANITY_SOURCE_OTHER.
157 */
158 function imsanity_get_max_width_height( $source ) {
159 if ( ! defined( 'IMSANITY_DEFAULT_MAX_WIDTH' ) ) {
160 imsanity_init();
161 }
162 $w = (int) imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
163 $h = (int) imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
164
165 switch ( $source ) {
166 case IMSANITY_SOURCE_POST:
167 break;
168 case IMSANITY_SOURCE_LIBRARY:
169 $w = (int) imsanity_get_option( 'imsanity_max_width_library', $w );
170 $h = (int) imsanity_get_option( 'imsanity_max_height_library', $h );
171 break;
172 default:
173 $w = (int) imsanity_get_option( 'imsanity_max_width_other', $w );
174 $h = (int) imsanity_get_option( 'imsanity_max_height_other', $h );
175 break;
176 }
177
178 // NOTE: filters MUST return an array of 2 items, or the defaults will be used.
179 return apply_filters( 'imsanity_get_max_width_height', array( $w, $h ), $source );
180 }
181
182 /**
183 * Handler after a file has been uploaded. If the file is an image, check the size
184 * to see if it is too big and, if so, resize and overwrite the original.
185 *
186 * @param Array $params The parameters submitted with the upload.
187 */
188 function imsanity_handle_upload( $params ) {
189 imsanity_debug( __FUNCTION__ );
190
191 if ( empty( $params['file'] ) || empty( $params['type'] ) ) {
192 imsanity_debug( 'missing file or type parameter, skipping' );
193 return $params;
194 }
195
196 // If "noresize" is included in the filename then we will bypass imsanity scaling.
197 if ( strpos( $params['file'], 'noresize' ) !== false ) {
198 imsanity_debug( "skipping {$params['file']}" );
199 return $params;
200 }
201
202 if ( apply_filters( 'imsanity_skip_image', false, $params['file'] ) ) {
203 imsanity_debug( "skipping {$params['file']} per filter" );
204 return $params;
205 }
206
207 // If preferences specify so then we can convert an original bmp or png file into jpg.
208 if ( ( 'image/bmp' === $params['type'] || 'image/x-ms-bmp' === $params['type'] ) && imsanity_get_option( 'imsanity_bmp_to_jpg', IMSANITY_DEFAULT_BMP_TO_JPG ) ) {
209 $params = imsanity_convert_to_jpg( 'bmp', $params );
210 }
211
212 if ( 'image/png' === $params['type'] && imsanity_get_option( 'imsanity_png_to_jpg', IMSANITY_DEFAULT_PNG_TO_JPG ) ) {
213 $params = imsanity_convert_to_jpg( 'png', $params );
214 }
215
216 // Store the path for reference in case $params is modified.
217 $oldpath = $params['file'];
218
219 // Let folks filter the allowed mime-types for resizing.
220 // Also allows conditional support for WebP and AVIF if the server supports it.
221 $allowed_types = apply_filters( 'imsanity_allowed_mimes', array( 'image/png', 'image/gif', 'image/jpeg' ), $oldpath );
222 if ( is_string( $allowed_types ) ) {
223 $allowed_types = array( $allowed_types );
224 } elseif ( ! is_array( $allowed_types ) ) {
225 $allowed_types = array();
226 }
227
228 if (
229 ( ! is_wp_error( $params ) ) &&
230 is_file( $oldpath ) &&
231 is_readable( $oldpath ) &&
232 is_writable( $oldpath ) &&
233 filesize( $oldpath ) > 0 &&
234 in_array( $params['type'], $allowed_types, true )
235 ) {
236 // If the Modern Image Formats plugin is active but fallback mode is disabled, permit conversion to AVIF/WebP during upload by defining IMSANITY_ALLOW_CONVERSION.
237 // Otherwise, no conversion should be allowed at all. The upload handler will still check for conversion and work with it if it happens somehow.
238 if ( ! defined( 'IMSANITY_ALLOW_CONVERSION' ) && function_exists( 'webp_uploads_is_fallback_enabled' ) && ! webp_uploads_is_fallback_enabled() ) {
239 define( 'IMSANITY_ALLOW_CONVERSION', true );
240 }
241
242 // figure out where the upload is coming from.
243 $source = imsanity_get_source();
244
245 $maxw = IMSANITY_DEFAULT_MAX_WIDTH;
246 $maxh = IMSANITY_DEFAULT_MAX_HEIGHT;
247 $max_width_height = imsanity_get_max_width_height( $source );
248 if ( is_array( $max_width_height ) && 2 === count( $max_width_height ) ) {
249 list( $maxw, $maxh ) = $max_width_height;
250 }
251 $maxw = (int) $maxw;
252 $maxh = (int) $maxh;
253
254 $dimensions = getimagesize( $oldpath );
255 if ( is_array( $dimensions ) && count( $dimensions ) >= 2 ) {
256 $oldw = $dimensions[0];
257 $oldh = $dimensions[1];
258 } else {
259 imsanity_debug( "could not get dimensions for $oldpath, skipping" );
260 return $params;
261 }
262
263 if ( ( $oldw > $maxw + 1 && $maxw > 0 ) || ( $oldh > $maxh + 1 && $maxh > 0 ) ) {
264
265 $ftype = imsanity_quick_mimetype( $oldpath );
266 $orientation = imsanity_get_orientation( $oldpath, $ftype );
267 // If we are going to rotate the image 90 degrees during the resize, swap the existing image dimensions.
268 if ( 6 === (int) $orientation || 8 === (int) $orientation ) {
269 $old_oldw = $oldw;
270 $oldw = $oldh;
271 $oldh = $old_oldw;
272 }
273
274 if ( $maxw > 0 && $maxh > 0 && $oldw >= $maxw && $oldh >= $maxh && ( $oldh > $maxh || $oldw > $maxw ) && apply_filters( 'imsanity_crop_image', false ) ) {
275 $neww = $maxw;
276 $newh = $maxh;
277 } else {
278 list( $neww, $newh ) = wp_constrain_dimensions( $oldw, $oldh, $maxw, $maxh );
279 }
280
281 global $ewww_preempt_editor;
282 if ( ! isset( $ewww_preempt_editor ) ) {
283 $ewww_preempt_editor = false;
284 }
285 $original_preempt = $ewww_preempt_editor;
286 $ewww_preempt_editor = true;
287 $resizeresult = imsanity_image_resize( $oldpath, $neww, $newh, apply_filters( 'imsanity_crop_image', false ) );
288 $ewww_preempt_editor = $original_preempt;
289
290 if ( $resizeresult && ! is_wp_error( $resizeresult ) ) {
291 $newpath = $resizeresult;
292 $new_type = $params['type'];
293
294 imsanity_debug( "checking $newpath to see if resize was successful" );
295 if ( is_file( $newpath ) && filesize( $newpath ) < filesize( $oldpath ) ) {
296 imsanity_debug( 'resized image is smaller, replacing original' );
297 // We saved some file space. remove original and replace with resized image.
298 $new_type = imsanity_mimetype( $newpath );
299 unlink( $oldpath );
300 rename( $newpath, $oldpath );
301 if ( $new_type && $new_type !== $params['type'] ) {
302 imsanity_debug( "mimetype changed from {$params['type']} to $new_type" );
303 $params['type'] = $new_type;
304 $params['file'] = imsanity_update_extension( $oldpath, $new_type );
305 if ( $params['file'] !== $oldpath ) {
306 rename( $oldpath, $params['file'] );
307 }
308 $params['url'] = imsanity_update_extension( $params['url'], $new_type );
309 imsanity_debug( "renamed file to match new extension: {$params['file']} / {$params['url']}" );
310 }
311 } elseif ( is_file( $newpath ) ) {
312 imsanity_debug( 'resized image is bigger, discarding' );
313 // The resized image is actually bigger in filesize (most likely due to jpg quality).
314 // Keep the old one and just get rid of the resized image.
315 unlink( $newpath );
316 }
317 } elseif ( false === $resizeresult ) {
318 imsanity_debug( 'resize returned false, unknown error' );
319 return $params;
320 } elseif ( is_wp_error( $resizeresult ) ) {
321 // resize didn't work, likely because the image processing libraries are missing.
322 // remove the old image so we don't leave orphan files hanging around.
323 unlink( $oldpath );
324
325 $params = wp_handle_upload_error(
326 $oldpath,
327 sprintf(
328 /* translators: 1: error message 2: link to support forums */
329 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' ),
330 $resizeresult->get_error_message(),
331 'https://wordpress.org/support/plugin/imsanity'
332 )
333 );
334 imsanity_debug( 'resize result is wp_error, should have already output error to log' );
335 } else {
336 imsanity_debug( 'unknown resize result, inconceivable!' );
337 return $params;
338 }
339 }
340 }
341 clearstatcache();
342 return $params;
343 }
344
345
346 /**
347 * Read in the image file from the params and then save as a new jpg file.
348 * if successful, remove the original image and alter the return
349 * parameters to return the new jpg instead of the original
350 *
351 * @param string $type Type of the image to be converted: 'bmp' or 'png'.
352 * @param array $params The upload parameters.
353 * @return array altered params
354 */
355 function imsanity_convert_to_jpg( $type, $params ) {
356 imsanity_debug( __FUNCTION__ );
357
358 if ( apply_filters( 'imsanity_disable_convert', false, $type, $params ) ) {
359 imsanity_debug( "skipping conversion for {$params['file']}" );
360 return $params;
361 }
362
363 $img = null;
364
365 if ( 'bmp' === $type ) {
366 if ( ! function_exists( 'imagecreatefrombmp' ) ) {
367 imsanity_debug( 'imagecreatefrombmp does not exist' );
368 return $params;
369 }
370 $img = imagecreatefrombmp( $params['file'] );
371 } elseif ( 'png' === $type ) {
372 // Prevent converting PNG images with alpha/transparency, unless overridden by the user.
373 if ( apply_filters( 'imsanity_skip_alpha', imsanity_has_alpha( $params['file'] ), $params['file'] ) ) {
374 return $params;
375 }
376 if ( ! function_exists( 'imagecreatefrompng' ) ) {
377 return wp_handle_upload_error( $params['file'], esc_html__( 'Imsanity requires the GD library to convert PNG images to JPG', 'imsanity' ) );
378 }
379
380 $input = imagecreatefrompng( $params['file'] );
381 // convert png transparency to white.
382 $img = imagecreatetruecolor( imagesx( $input ), imagesy( $input ) );
383 imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
384 imagealphablending( $img, true );
385 imagecopy( $img, $input, 0, 0, 0, 0, imagesx( $input ), imagesy( $input ) );
386 } else {
387 return wp_handle_upload_error( $params['file'], esc_html__( 'Unknown image type specified in imsanity_convert_to_jpg', 'imsanity' ) );
388 }
389
390 // We need to change the extension from the original to .jpg so we have to ensure it will be a unique filename.
391 $uploads = wp_upload_dir();
392 $oldfilename = wp_basename( $params['file'] );
393 $newfilename = wp_basename( str_ireplace( '.' . $type, '.jpg', $oldfilename ) );
394 $newfilename = wp_unique_filename( $uploads['path'], $newfilename );
395
396 $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
397
398 if ( imagejpeg( $img, $uploads['path'] . '/' . $newfilename, $quality ) ) {
399 // Conversion succeeded: remove the original bmp & remap the params.
400 unlink( $params['file'] );
401
402 $params['file'] = $uploads['path'] . '/' . $newfilename;
403 $params['url'] = $uploads['url'] . '/' . $newfilename;
404 $params['type'] = 'image/jpeg';
405 } else {
406 unlink( $params['file'] );
407
408 return wp_handle_upload_error(
409 $oldfilename,
410 /* translators: %s: the image mime type */
411 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 )
412 );
413 }
414
415 return $params;
416 }
417
418 // Run plugin init actions.
419 add_action( 'init', 'imsanity_init' );
420 // Add filter to hook into uploads.
421 add_filter( 'wp_handle_upload', 'imsanity_handle_upload' );
422 // Load legacy language files on plugins_loaded.
423 add_action( 'plugins_loaded', 'imsanity_load_textdomain' );
424
425 // Adds a column to the media library list view to display optimization results.
426 add_filter( 'manage_media_columns', 'imsanity_media_columns' );
427 // Outputs the actual column information for each attachment.
428 add_action( 'manage_media_custom_column', 'imsanity_custom_column', 10, 2 );
429 // Checks for AVIF support and adds it to the allowed mime types.
430 add_filter( 'imsanity_allowed_mimes', 'imsanity_add_avif_support' );
431 // Checks for WebP support and adds it to the allowed mime types.
432 add_filter( 'imsanity_allowed_mimes', 'imsanity_add_webp_support' );
433