PluginProbe
Imsanity / 2.6.0
Imsanity v2.6.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.6.0, at imsanity.php

353 lines 12.2 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.6.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.6.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', 1 );
33 define( 'IMSANITY_DEFAULT_PNG_TO_JPG', 0 );
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 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.
97 *
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
143 /**
144 * Given the source, returns the max width/height.
145 *
146 * @example: list( $w, $h ) = imsanity_get_max_width_height( IMSANITY_SOURCE_LIBRARY );
147 * @param int $source One of IMSANITY_SOURCE_POST | IMSANITY_SOURCE_LIBRARY | IMSANITY_SOURCE_OTHER.
148 */
149 function imsanity_get_max_width_height( $source ) {
150 $w = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
151 $h = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
152
153 switch ( $source ) {
154 case IMSANITY_SOURCE_POST:
155 break;
156 case IMSANITY_SOURCE_LIBRARY:
157 $w = imsanity_get_option( 'imsanity_max_width_library', $w );
158 $h = imsanity_get_option( 'imsanity_max_height_library', $h );
159 break;
160 default:
161 $w = imsanity_get_option( 'imsanity_max_width_other', $w );
162 $h = imsanity_get_option( 'imsanity_max_height_other', $h );
163 break;
164 }
165
166 // NOTE: filters MUST return an array of 2 items, or the defaults will be used.
167 return apply_filters( 'imsanity_get_max_width_height', array( $w, $h ), $source );
168 }
169
170 /**
171 * Handler after a file has been uploaded. If the file is an image, check the size
172 * to see if it is too big and, if so, resize and overwrite the original.
173 *
174 * @param Array $params The parameters submitted with the upload.
175 */
176 function imsanity_handle_upload( $params ) {
177
178 // If "noresize" is included in the filename then we will bypass imsanity scaling.
179 if ( strpos( $params['file'], 'noresize' ) !== false ) {
180 return $params;
181 }
182
183 // If preferences specify so then we can convert an original bmp or png file into jpg.
184 if ( 'image/bmp' === $params['type'] && imsanity_get_option( 'imsanity_bmp_to_jpg', IMSANITY_DEFAULT_BMP_TO_JPG ) ) {
185 $params = imsanity_convert_to_jpg( 'bmp', $params );
186 }
187
188 if ( 'image/png' === $params['type'] && imsanity_get_option( 'imsanity_png_to_jpg', IMSANITY_DEFAULT_PNG_TO_JPG ) ) {
189 $params = imsanity_convert_to_jpg( 'png', $params );
190 }
191
192 // Make sure this is a type of image that we want to convert and that it exists.
193 $oldpath = $params['file'];
194
195 // Let folks filter the allowed mime-types for resizing.
196 $allowed_types = apply_filters( 'imsanity_allowed_mimes', array( 'image/png', 'image/gif', 'image/jpeg' ), $oldpath );
197 if ( is_string( $allowed_types ) ) {
198 $allowed_types = array( $allowed_types );
199 } elseif ( ! is_array( $allowed_types ) ) {
200 $allowed_types = array();
201 }
202
203 if (
204 ( ! is_wp_error( $params ) ) &&
205 is_file( $oldpath ) &&
206 is_readable( $oldpath ) &&
207 is_writable( $oldpath ) &&
208 filesize( $oldpath ) > 0 &&
209 in_array( $params['type'], $allowed_types, true )
210 ) {
211
212 // figure out where the upload is coming from.
213 $source = imsanity_get_source();
214
215 $maxw = IMSANITY_DEFAULT_MAX_WIDTH;
216 $maxh = IMSANITY_DEFAULT_MAX_HEIGHT;
217 $max_width_height = imsanity_get_max_width_height( $source );
218 if ( is_array( $max_width_height ) && 2 === count( $max_width_height ) ) {
219 list( $maxw, $maxh ) = $max_width_height;
220 }
221
222 list( $oldw, $oldh ) = getimagesize( $oldpath );
223
224 if ( ( $oldw > $maxw && $maxw > 0 ) || ( $oldh > $maxh && $maxh > 0 ) ) {
225 $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
226
227 $ftype = imsanity_quick_mimetype( $oldpath );
228 $orientation = imsanity_get_orientation( $oldpath, $ftype );
229 // If we are going to rotate the image 90 degrees during the resize, swap the existing image dimensions.
230 if ( 6 === (int) $orientation || 8 === (int) $orientation ) {
231 $old_oldw = $oldw;
232 $oldw = $oldh;
233 $oldh = $old_oldw;
234 }
235
236 if ( $maxw > 0 && $maxh > 0 && $oldw >= $maxw && $oldh >= $maxh && ( $oldh > $maxh || $oldw > $maxw ) && apply_filters( 'imsanity_crop_image', false ) ) {
237 $neww = $maxw;
238 $newh = $maxh;
239 } else {
240 list( $neww, $newh ) = wp_constrain_dimensions( $oldw, $oldh, $maxw, $maxh );
241 }
242
243 global $ewww_preempt_editor;
244 if ( ! isset( $ewww_preempt_editor ) ) {
245 $ewww_preempt_editor = false;
246 }
247 $original_preempt = $ewww_preempt_editor;
248 $ewww_preempt_editor = true;
249 $resizeresult = imsanity_image_resize( $oldpath, $neww, $newh, apply_filters( 'imsanity_crop_image', false ), null, null, $quality );
250 $ewww_preempt_editor = $original_preempt;
251
252 if ( $resizeresult && ! is_wp_error( $resizeresult ) ) {
253 $newpath = $resizeresult;
254
255 if ( is_file( $newpath ) && filesize( $newpath ) < filesize( $oldpath ) ) {
256 // we saved some file space. remove original and replace with resized image.
257 unlink( $oldpath );
258 rename( $newpath, $oldpath );
259 } elseif ( is_file( $newpath ) ) {
260 // theresized image is actually bigger in filesize (most likely due to jpg quality).
261 // keep the old one and just get rid of the resized image.
262 unlink( $newpath );
263 }
264 } elseif ( false === $resizeresult ) {
265 return $params;
266 } elseif ( is_wp_error( $resizeresult ) ) {
267 // resize didn't work, likely because the image processing libraries are missing.
268 // remove the old image so we don't leave orphan files hanging around.
269 unlink( $oldpath );
270
271 $params = wp_handle_upload_error(
272 $oldpath,
273 sprintf(
274 /* translators: 1: error message 2: link to support forums */
275 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' ),
276 $resizeresult->get_error_message(),
277 'https://wordpress.org/support/plugin/imsanity'
278 )
279 );
280 } else {
281 return $params;
282 }
283 }
284 }
285 clearstatcache();
286 return $params;
287 }
288
289
290 /**
291 * Read in the image file from the params and then save as a new jpg file.
292 * if successful, remove the original image and alter the return
293 * parameters to return the new jpg instead of the original
294 *
295 * @param string $type Type of the image to be converted: 'bmp' or 'png'.
296 * @param array $params The upload parameters.
297 * @return array altered params
298 */
299 function imsanity_convert_to_jpg( $type, $params ) {
300
301 $img = null;
302
303 if ( 'bmp' === $type ) {
304 include_once( 'libs/imagecreatefrombmp.php' );
305 $img = imagecreatefrombmp( $params['file'] );
306 } elseif ( 'png' === $type ) {
307 if ( ! function_exists( 'imagecreatefrompng' ) ) {
308 return wp_handle_upload_error( $params['file'], esc_html__( 'Imsanity requires the GD library to convert PNG images to JPG', 'imsanity' ) );
309 }
310
311 $input = imagecreatefrompng( $params['file'] );
312 // convert png transparency to white.
313 $img = imagecreatetruecolor( imagesx( $input ), imagesy( $input ) );
314 imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
315 imagealphablending( $img, true );
316 imagecopy( $img, $input, 0, 0, 0, 0, imagesx( $input ), imagesy( $input ) );
317 } else {
318 return wp_handle_upload_error( $params['file'], esc_html__( 'Unknown image type specified in imsanity_convert_to_jpg', 'imsanity' ) );
319 }
320
321 // We need to change the extension from the original to .jpg so we have to ensure it will be a unique filename.
322 $uploads = wp_upload_dir();
323 $oldfilename = basename( $params['file'] );
324 $newfilename = basename( str_ireplace( '.' . $type, '.jpg', $oldfilename ) );
325 $newfilename = wp_unique_filename( $uploads['path'], $newfilename );
326
327 $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
328
329 if ( imagejpeg( $img, $uploads['path'] . '/' . $newfilename, $quality ) ) {
330 // Conversion succeeded: remove the original bmp & remap the params.
331 unlink( $params['file'] );
332
333 $params['file'] = $uploads['path'] . '/' . $newfilename;
334 $params['url'] = $uploads['url'] . '/' . $newfilename;
335 $params['type'] = 'image/jpeg';
336 } else {
337 unlink( $params['file'] );
338
339 return wp_handle_upload_error(
340 $oldfilename,
341 /* translators: %s: the image mime type */
342 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 )
343 );
344 }
345
346 return $params;
347 }
348
349 // Add filter to hook into uploads.
350 add_filter( 'wp_handle_upload', 'imsanity_handle_upload' );
351 // Run necessary actions on init (loading translations mostly).
352 add_action( 'plugins_loaded', 'imsanity_init' );
353