| 1 |
<?php |
| 2 |
/** |
| 3 |
* Imsanity utility functions. |
| 4 |
* |
| 5 |
* @package Imsanity |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Retrieves the path of an attachment via the $id and the $meta. |
| 10 |
* |
| 11 |
* @param array $meta The attachment metadata. |
| 12 |
* @param int $id The attachment ID number. |
| 13 |
* @param string $file Optional. Path relative to the uploads folder. Default ''. |
| 14 |
* @param bool $refresh_cache Optional. True to flush cache prior to fetching path. Default true. |
| 15 |
* @return string The full path to the image. |
| 16 |
*/ |
| 17 |
function imsanity_attachment_path( $meta, $id, $file = '', $refresh_cache = true ) { |
| 18 |
// Retrieve the location of the WordPress upload folder. |
| 19 |
$upload_dir = wp_upload_dir( null, false, $refresh_cache ); |
| 20 |
$upload_path = trailingslashit( $upload_dir['basedir'] ); |
| 21 |
if ( is_array( $meta ) && ! empty( $meta['file'] ) ) { |
| 22 |
$file_path = $meta['file']; |
| 23 |
if ( strpos( $file_path, 's3' ) === 0 ) { |
| 24 |
return ''; |
| 25 |
} |
| 26 |
if ( is_file( $file_path ) ) { |
| 27 |
return $file_path; |
| 28 |
} |
| 29 |
$file_path = $upload_path . $file_path; |
| 30 |
if ( is_file( $file_path ) ) { |
| 31 |
return $file_path; |
| 32 |
} |
| 33 |
$upload_path = trailingslashit( WP_CONTENT_DIR ) . 'uploads/'; |
| 34 |
$file_path = $upload_path . $meta['file']; |
| 35 |
if ( is_file( $file_path ) ) { |
| 36 |
return $file_path; |
| 37 |
} |
| 38 |
} |
| 39 |
if ( ! $file ) { |
| 40 |
$file = get_post_meta( $id, '_wp_attached_file', true ); |
| 41 |
} |
| 42 |
$file_path = ( 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ? $upload_path . $file : $file ); |
| 43 |
$filtered_file_path = apply_filters( 'get_attached_file', $file_path, $id ); |
| 44 |
if ( strpos( $filtered_file_path, 's3' ) === false && is_file( $filtered_file_path ) ) { |
| 45 |
return str_replace( '//_imsgalleries/', '/_imsgalleries/', $filtered_file_path ); |
| 46 |
} |
| 47 |
if ( strpos( $file_path, 's3' ) === false && is_file( $file_path ) ) { |
| 48 |
return str_replace( '//_imsgalleries/', '/_imsgalleries/', $file_path ); |
| 49 |
} |
| 50 |
return ''; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Checks the filename for a protocal wrapper (like s3://). |
| 55 |
* |
| 56 |
* @param string $path The path of the file to check. |
| 57 |
* @return bool True if the file contains :// indicating a stream wrapper. |
| 58 |
*/ |
| 59 |
function imsanity_file_is_stream_wrapped( $path ) { |
| 60 |
if ( false !== strpos( $path, '://' ) ) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get mimetype based on file extension instead of file contents when speed outweighs accuracy. |
| 68 |
* |
| 69 |
* @param string $path The name of the file. |
| 70 |
* @return string|bool The mime type based on the extension or false. |
| 71 |
*/ |
| 72 |
function imsanity_quick_mimetype( $path ) { |
| 73 |
$pathextension = strtolower( pathinfo( $path, PATHINFO_EXTENSION ) ); |
| 74 |
switch ( $pathextension ) { |
| 75 |
case 'jpg': |
| 76 |
case 'jpeg': |
| 77 |
case 'jpe': |
| 78 |
return 'image/jpeg'; |
| 79 |
case 'png': |
| 80 |
return 'image/png'; |
| 81 |
case 'gif': |
| 82 |
return 'image/gif'; |
| 83 |
case 'pdf': |
| 84 |
return 'application/pdf'; |
| 85 |
case 'avif': |
| 86 |
return 'image/avif'; |
| 87 |
case 'webp': |
| 88 |
return 'image/webp'; |
| 89 |
default: |
| 90 |
return false; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Check the mimetype of the given file with magic mime strings/patterns. |
| 96 |
* |
| 97 |
* @param string $path The absolute path to the file. |
| 98 |
* @return bool|string A valid mime-type or false. |
| 99 |
*/ |
| 100 |
function imsanity_mimetype( $path ) { |
| 101 |
imsanity_debug( "testing mimetype: $path" ); |
| 102 |
$type = false; |
| 103 |
// For S3 images/files, don't attempt to read the file, just use the quick (filename) mime check. |
| 104 |
if ( imsanity_file_is_stream_wrapped( $path ) ) { |
| 105 |
return imsanity_quick_mimetype( $path ); |
| 106 |
} |
| 107 |
$path = \realpath( $path ); |
| 108 |
if ( ! is_file( $path ) ) { |
| 109 |
imsanity_debug( "$path is not a file, or out of bounds" ); |
| 110 |
return $type; |
| 111 |
} |
| 112 |
if ( ! is_readable( $path ) ) { |
| 113 |
imsanity_debug( "$path is not readable" ); |
| 114 |
return $type; |
| 115 |
} |
| 116 |
$file_handle = fopen( $path, 'rb' ); |
| 117 |
$file_contents = fread( $file_handle, 4096 ); |
| 118 |
if ( $file_contents ) { |
| 119 |
// Read first 12 bytes, which equates to 24 hex characters. |
| 120 |
$magic = bin2hex( substr( $file_contents, 0, 12 ) ); |
| 121 |
imsanity_debug( $magic ); |
| 122 |
if ( 8 === strpos( $magic, '6674797061766966' ) ) { |
| 123 |
$type = 'image/avif'; |
| 124 |
imsanity_debug( "imsanity type: $type" ); |
| 125 |
return $type; |
| 126 |
} |
| 127 |
if ( '424d' === substr( $magic, 0, 4 ) ) { |
| 128 |
$type = 'image/bmp'; |
| 129 |
imsanity_debug( "imsanity type: $type" ); |
| 130 |
return $type; |
| 131 |
} |
| 132 |
if ( 0 === strpos( $magic, '52494646' ) && 16 === strpos( $magic, '57454250' ) ) { |
| 133 |
$type = 'image/webp'; |
| 134 |
imsanity_debug( "imsanity type: $type" ); |
| 135 |
return $type; |
| 136 |
} |
| 137 |
if ( 'ffd8ff' === substr( $magic, 0, 6 ) ) { |
| 138 |
$type = 'image/jpeg'; |
| 139 |
imsanity_debug( "imsanity type: $type" ); |
| 140 |
return $type; |
| 141 |
} |
| 142 |
if ( '89504e470d0a1a0a' === substr( $magic, 0, 16 ) ) { |
| 143 |
$type = 'image/png'; |
| 144 |
imsanity_debug( "imsanity type: $type" ); |
| 145 |
return $type; |
| 146 |
} |
| 147 |
if ( '474946383761' === substr( $magic, 0, 12 ) || '474946383961' === substr( $magic, 0, 12 ) ) { |
| 148 |
$type = 'image/gif'; |
| 149 |
imsanity_debug( "imsanity type: $type" ); |
| 150 |
return $type; |
| 151 |
} |
| 152 |
if ( '25504446' === substr( $magic, 0, 8 ) ) { |
| 153 |
$type = 'application/pdf'; |
| 154 |
imsanity_debug( "imsanity type: $type" ); |
| 155 |
return $type; |
| 156 |
} |
| 157 |
if ( preg_match( '/<svg/', $file_contents ) ) { |
| 158 |
$type = 'image/svg+xml'; |
| 159 |
imsanity_debug( "imsanity type: $type" ); |
| 160 |
return $type; |
| 161 |
} |
| 162 |
imsanity_debug( "match not found for file: $magic" ); |
| 163 |
} else { |
| 164 |
imsanity_debug( 'could not open for reading' ); |
| 165 |
} |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Update the file extension based on the new mime type. |
| 171 |
* |
| 172 |
* @param string $path The path of the file to update. |
| 173 |
* @param string $new_mime The new mime type. |
| 174 |
* @return string The updated path with the new extension. |
| 175 |
*/ |
| 176 |
function imsanity_update_extension( $path, $new_mime ) { |
| 177 |
$extension = ''; |
| 178 |
switch ( $new_mime ) { |
| 179 |
case 'image/jpeg': |
| 180 |
$extension = 'jpg'; |
| 181 |
break; |
| 182 |
case 'image/png': |
| 183 |
$extension = 'png'; |
| 184 |
break; |
| 185 |
case 'image/gif': |
| 186 |
$extension = 'gif'; |
| 187 |
break; |
| 188 |
case 'image/avif': |
| 189 |
$extension = 'avif'; |
| 190 |
break; |
| 191 |
case 'image/webp': |
| 192 |
$extension = 'webp'; |
| 193 |
break; |
| 194 |
default: |
| 195 |
return $path; |
| 196 |
} |
| 197 |
$pathinfo = pathinfo( $path ); |
| 198 |
if ( empty( $pathinfo['dirname'] ) || empty( $pathinfo['filename'] ) ) { |
| 199 |
return $path; |
| 200 |
} |
| 201 |
$new_name = trailingslashit( $pathinfo['dirname'] ) . $pathinfo['filename'] . '.' . $extension; |
| 202 |
return $new_name; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Check for AVIF support in the image editor and add to the list of allowed mimes. |
| 207 |
* |
| 208 |
* @param array $mimes A list of allowed mime types. |
| 209 |
* @return array The updated list of mimes after checking AVIF support. |
| 210 |
*/ |
| 211 |
function imsanity_add_avif_support( $mimes ) { |
| 212 |
if ( ! in_array( 'image/avif', $mimes, true ) ) { |
| 213 |
if ( class_exists( 'Imagick' ) ) { |
| 214 |
$imagick = new Imagick(); |
| 215 |
$formats = $imagick->queryFormats(); |
| 216 |
if ( in_array( 'AVIF', $formats, true ) ) { |
| 217 |
$mimes[] = 'image/avif'; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
return $mimes; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Check for WebP support in the image editor and add to the list of allowed mimes. |
| 226 |
* |
| 227 |
* @param array $mimes A list of allowed mime types. |
| 228 |
* @return array The updated list of mimes after checking WebP support. |
| 229 |
*/ |
| 230 |
function imsanity_add_webp_support( $mimes ) { |
| 231 |
if ( ! in_array( 'image/webp', $mimes, true ) ) { |
| 232 |
if ( class_exists( 'Imagick' ) ) { |
| 233 |
$imagick = new Imagick(); |
| 234 |
$formats = $imagick->queryFormats(); |
| 235 |
if ( in_array( 'WEBP', $formats, true ) ) { |
| 236 |
$mimes[] = 'image/webp'; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
return $mimes; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Gets the orientation/rotation of a JPG image using the EXIF data. |
| 245 |
* |
| 246 |
* @param string $file Name of the file. |
| 247 |
* @param string $type Mime type of the file. |
| 248 |
* @return int|bool The orientation value or false. |
| 249 |
*/ |
| 250 |
function imsanity_get_orientation( $file, $type ) { |
| 251 |
if ( function_exists( 'exif_read_data' ) && 'image/jpeg' === $type ) { |
| 252 |
$exif = @exif_read_data( $file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 253 |
if ( is_array( $exif ) && array_key_exists( 'Orientation', $exif ) ) { |
| 254 |
return (int) $exif['Orientation']; |
| 255 |
} |
| 256 |
} |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Check an image to see if it has transparency. |
| 262 |
* |
| 263 |
* @param string $filename The name of the image file. |
| 264 |
* @return bool True if transparency is found. |
| 265 |
*/ |
| 266 |
function imsanity_has_alpha( $filename ) { |
| 267 |
imsanity_debug( __FUNCTION__ ); |
| 268 |
if ( ! is_file( $filename ) ) { |
| 269 |
return false; |
| 270 |
} |
| 271 |
if ( false !== strpos( $filename, '../' ) ) { |
| 272 |
return false; |
| 273 |
} |
| 274 |
$file_contents = file_get_contents( $filename ); |
| 275 |
// Determine what color type is stored in the file. |
| 276 |
$color_type = ord( substr( $file_contents, 25, 1 ) ); |
| 277 |
// If we do not have GD and the PNG color type is RGB alpha or Grayscale alpha. |
| 278 |
if ( ! imsanity_gd_support() && ( 4 === $color_type || 6 === $color_type ) ) { |
| 279 |
imsanity_debug( "color type $color_type indicates alpha channel in $filename" ); |
| 280 |
return true; |
| 281 |
} elseif ( imsanity_gd_support() ) { |
| 282 |
$image = imagecreatefrompng( $filename ); |
| 283 |
if ( ! $image ) { |
| 284 |
imsanity_debug( "could not create GD image from $filename" ); |
| 285 |
return false; |
| 286 |
} |
| 287 |
if ( imagecolortransparent( $image ) >= 0 ) { |
| 288 |
imsanity_debug( "$filename has a transparent color" ); |
| 289 |
return true; |
| 290 |
} |
| 291 |
$image_size = getimagesize( $filename ); |
| 292 |
if ( empty( $image_size[0] ) || empty( $image_size[1] ) ) { |
| 293 |
imsanity_debug( "invalid dimensions for $filename" ); |
| 294 |
return false; |
| 295 |
} |
| 296 |
$width = (int) $image_size[0]; |
| 297 |
$height = (int) $image_size[1]; |
| 298 |
for ( $y = 0; $y < $height; $y++ ) { |
| 299 |
for ( $x = 0; $x < $width; $x++ ) { |
| 300 |
$color = imagecolorat( $image, $x, $y ); |
| 301 |
$rgb = imagecolorsforindex( $image, $color ); |
| 302 |
if ( $rgb['alpha'] > 0 ) { |
| 303 |
imsanity_debug( "found alpha in $filename at pixel $x, $y" ); |
| 304 |
return true; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
return false; |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Check for GD support of both PNG and JPG. |
| 314 |
* |
| 315 |
* @return bool True if full GD support is detected. |
| 316 |
*/ |
| 317 |
function imsanity_gd_support() { |
| 318 |
if ( function_exists( 'gd_info' ) ) { |
| 319 |
$gd_support = gd_info(); |
| 320 |
if ( is_iterable( $gd_support ) ) { |
| 321 |
if ( ( ! empty( $gd_support['JPEG Support'] ) || ! empty( $gd_support['JPG Support'] ) ) && ! empty( $gd_support['PNG Support'] ) ) { |
| 322 |
return true; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Resizes the image with the given id according to the configured max width and height settings. |
| 331 |
* |
| 332 |
* @param int $id The attachment ID of the image to process. |
| 333 |
* @return array The success status (bool) and a message to display. |
| 334 |
*/ |
| 335 |
function imsanity_resize_from_id( $id = 0 ) { |
| 336 |
imsanity_debug( __FUNCTION__ ); |
| 337 |
|
| 338 |
$id = (int) $id; |
| 339 |
|
| 340 |
if ( ! $id ) { |
| 341 |
return; |
| 342 |
} |
| 343 |
imsanity_debug( "attempting to resize attachment $id" ); |
| 344 |
if ( ! defined( 'IMSANITY_DEFAULT_MAX_WIDTH' ) ) { |
| 345 |
imsanity_init(); |
| 346 |
} |
| 347 |
|
| 348 |
$meta = wp_get_attachment_metadata( $id ); |
| 349 |
|
| 350 |
if ( $meta && is_array( $meta ) ) { |
| 351 |
$update_meta = false; |
| 352 |
// If "noresize" is included in the filename then we will bypass imsanity scaling. |
| 353 |
if ( ! empty( $meta['file'] ) && false !== strpos( $meta['file'], 'noresize' ) ) { |
| 354 |
/* translators: %s: File-name of the image */ |
| 355 |
$msg = sprintf( esc_html__( 'SKIPPED: %s (noresize)', 'imsanity' ), $meta['file'] ); |
| 356 |
return array( |
| 357 |
'success' => false, |
| 358 |
'message' => $msg, |
| 359 |
); |
| 360 |
} |
| 361 |
|
| 362 |
$oldpath = imsanity_attachment_path( $meta, $id, '', false ); |
| 363 |
|
| 364 |
if ( empty( $oldpath ) ) { |
| 365 |
/* translators: %s: File-name of the image */ |
| 366 |
$msg = sprintf( esc_html__( 'Could not retrieve location of %s', 'imsanity' ), $meta['file'] ); |
| 367 |
return array( |
| 368 |
'success' => false, |
| 369 |
'message' => $msg, |
| 370 |
); |
| 371 |
} |
| 372 |
|
| 373 |
// Let folks filter the allowed mime-types for resizing. |
| 374 |
$allowed_types = apply_filters( 'imsanity_allowed_mimes', array( 'image/png', 'image/gif', 'image/jpeg' ), $oldpath ); |
| 375 |
if ( is_string( $allowed_types ) ) { |
| 376 |
$allowed_types = array( $allowed_types ); |
| 377 |
} elseif ( ! is_array( $allowed_types ) ) { |
| 378 |
$allowed_types = array(); |
| 379 |
} |
| 380 |
$ftype = imsanity_quick_mimetype( $oldpath ); |
| 381 |
if ( ! in_array( $ftype, $allowed_types, true ) ) { |
| 382 |
/* translators: %s: File type of the image */ |
| 383 |
$msg = sprintf( esc_html__( '%1$s does not have an allowed file type (%2$s)', 'imsanity' ), wp_basename( $oldpath ), $ftype ); |
| 384 |
return array( |
| 385 |
'success' => false, |
| 386 |
'message' => $msg, |
| 387 |
); |
| 388 |
} |
| 389 |
|
| 390 |
if ( ! is_writable( $oldpath ) ) { |
| 391 |
/* translators: %s: File-name of the image */ |
| 392 |
$msg = sprintf( esc_html__( '%s is not writable', 'imsanity' ), $meta['file'] ); |
| 393 |
return array( |
| 394 |
'success' => false, |
| 395 |
'message' => $msg, |
| 396 |
); |
| 397 |
} |
| 398 |
|
| 399 |
if ( apply_filters( 'imsanity_skip_image', false, $oldpath ) ) { |
| 400 |
/* translators: %s: File-name of the image */ |
| 401 |
$msg = sprintf( esc_html__( 'SKIPPED: %s (by user exclusion)', 'imsanity' ), $meta['file'] ); |
| 402 |
return array( |
| 403 |
'success' => false, |
| 404 |
'message' => $msg, |
| 405 |
); |
| 406 |
} |
| 407 |
|
| 408 |
$maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH ); |
| 409 |
$maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT ); |
| 410 |
$oldw = false; |
| 411 |
$oldh = false; |
| 412 |
|
| 413 |
// method one - slow but accurate, get file size from file itself. |
| 414 |
$dimensions = getimagesize( $oldpath ); |
| 415 |
if ( is_array( $dimensions ) && count( $dimensions ) >= 2 ) { |
| 416 |
$oldw = $dimensions[0]; |
| 417 |
$oldh = $dimensions[1]; |
| 418 |
} |
| 419 |
// method two - get file size from meta, fast but resize will fail if meta is out of sync. |
| 420 |
if ( ! $oldw || ! $oldh ) { |
| 421 |
$oldw = $meta['width']; |
| 422 |
$oldh = $meta['height']; |
| 423 |
} |
| 424 |
|
| 425 |
if ( ( $oldw > $maxw && $maxw > 0 ) || ( $oldh > $maxh && $maxh > 0 ) ) { |
| 426 |
|
| 427 |
if ( $maxw > 0 && $maxh > 0 && $oldw >= $maxw && $oldh >= $maxh && ( $oldh > $maxh || $oldw > $maxw ) && apply_filters( 'imsanity_crop_image', false ) ) { |
| 428 |
$neww = $maxw; |
| 429 |
$newh = $maxh; |
| 430 |
} else { |
| 431 |
list( $neww, $newh ) = wp_constrain_dimensions( $oldw, $oldh, $maxw, $maxh ); |
| 432 |
} |
| 433 |
|
| 434 |
$source_image = $oldpath; |
| 435 |
if ( ! empty( $meta['original_image'] ) ) { |
| 436 |
$source_image = path_join( dirname( $oldpath ), $meta['original_image'] ); |
| 437 |
imsanity_debug( "subbing in $source_image for resizing" ); |
| 438 |
} |
| 439 |
remove_all_filters( 'image_editor_output_format' ); |
| 440 |
$resizeresult = imsanity_image_resize( $source_image, $neww, $newh, apply_filters( 'imsanity_crop_image', false ) ); |
| 441 |
|
| 442 |
if ( $resizeresult && ! is_wp_error( $resizeresult ) ) { |
| 443 |
$newpath = $resizeresult; |
| 444 |
|
| 445 |
$new_type = imsanity_mimetype( $newpath ); |
| 446 |
if ( $new_type && $new_type !== $ftype ) { |
| 447 |
// The resized image is a different format, |
| 448 |
// keep the old one and just get rid of the resized image. |
| 449 |
imsanity_debug( "mime type changed from $ftype to $new_type, not allowed for existing images" ); |
| 450 |
if ( is_file( $newpath ) ) { |
| 451 |
unlink( $newpath ); |
| 452 |
} |
| 453 |
$results = array( |
| 454 |
'success' => false, |
| 455 |
'id' => $id, |
| 456 |
/* translators: 1: File-name of the image 2: the error message, translated elsewhere */ |
| 457 |
'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $meta['file'], esc_html__( 'File format/mime type was changed', 'imsanity' ) ), |
| 458 |
); |
| 459 |
} elseif ( $newpath !== $oldpath && is_file( $newpath ) && filesize( $newpath ) < filesize( $oldpath ) ) { |
| 460 |
// we saved some file space. remove original and replace with resized image. |
| 461 |
imsanity_debug( "$newpath is smaller, hurrah!" ); |
| 462 |
unlink( $oldpath ); |
| 463 |
rename( $newpath, $oldpath ); |
| 464 |
$meta['width'] = $neww; |
| 465 |
$meta['height'] = $newh; |
| 466 |
|
| 467 |
$update_meta = true; |
| 468 |
|
| 469 |
$results = array( |
| 470 |
'success' => true, |
| 471 |
'id' => $id, |
| 472 |
/* translators: 1: File-name of the image 2: the image width in pixels 3: the image height in pixels */ |
| 473 |
'message' => sprintf( esc_html__( 'OK: %1$s resized to %2$s x %3$s', 'imsanity' ), $meta['file'], $neww . 'w', $newh . 'h' ), |
| 474 |
); |
| 475 |
} elseif ( $newpath !== $oldpath ) { |
| 476 |
// the resized image is actually bigger in filesize (most likely due to jpg quality). |
| 477 |
// keep the old one and just get rid of the resized image. |
| 478 |
imsanity_debug( "$newpath is larger than $oldpath, bummer..." ); |
| 479 |
if ( is_file( $newpath ) ) { |
| 480 |
unlink( $newpath ); |
| 481 |
} |
| 482 |
$results = array( |
| 483 |
'success' => false, |
| 484 |
'id' => $id, |
| 485 |
/* translators: 1: File-name of the image 2: the error message, translated elsewhere */ |
| 486 |
'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $meta['file'], esc_html__( 'File size of resized image was larger than the original', 'imsanity' ) ), |
| 487 |
); |
| 488 |
} else { |
| 489 |
imsanity_debug( "$newpath === $oldpath, strange?" ); |
| 490 |
$results = array( |
| 491 |
'success' => false, |
| 492 |
'id' => $id, |
| 493 |
/* translators: 1: File-name of the image 2: the error message, translated elsewhere */ |
| 494 |
'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $meta['file'], esc_html__( 'Unknown error, resizing function returned the same filename', 'imsanity' ) ), |
| 495 |
); |
| 496 |
} |
| 497 |
} elseif ( false === $resizeresult ) { |
| 498 |
imsanity_debug( 'wp_get_image_editor likely missing, no resize result, and no error' ); |
| 499 |
$results = array( |
| 500 |
'success' => false, |
| 501 |
'id' => $id, |
| 502 |
/* translators: 1: File-name of the image 2: the error message, translated elsewhere */ |
| 503 |
'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $meta['file'], esc_html__( 'wp_get_image_editor missing', 'imsanity' ) ), |
| 504 |
); |
| 505 |
} else { |
| 506 |
imsanity_debug( 'image editor returned an error: ' . $resizeresult->get_error_message() ); |
| 507 |
$results = array( |
| 508 |
'success' => false, |
| 509 |
'id' => $id, |
| 510 |
/* translators: 1: File-name of the image 2: the error message, translated elsewhere */ |
| 511 |
'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $meta['file'], htmlentities( $resizeresult->get_error_message() ) ), |
| 512 |
); |
| 513 |
} |
| 514 |
} else { |
| 515 |
imsanity_debug( "$oldpath is already small enough: $oldw x $oldh" ); |
| 516 |
$results = array( |
| 517 |
'success' => true, |
| 518 |
'id' => $id, |
| 519 |
/* translators: %s: File-name of the image */ |
| 520 |
'message' => sprintf( esc_html__( 'SKIPPED: %s (Resize not required)', 'imsanity' ), $meta['file'] ) . " -- $oldw x $oldh", |
| 521 |
); |
| 522 |
if ( empty( $meta['width'] ) || empty( $meta['height'] ) ) { |
| 523 |
if ( empty( $meta['width'] ) || $meta['width'] > $oldw ) { |
| 524 |
$meta['width'] = $oldw; |
| 525 |
} |
| 526 |
if ( empty( $meta['height'] ) || $meta['height'] > $oldh ) { |
| 527 |
$meta['height'] = $oldh; |
| 528 |
} |
| 529 |
$update_meta = true; |
| 530 |
} |
| 531 |
} |
| 532 |
$remove_original = imsanity_remove_original_image( $id, $meta ); |
| 533 |
if ( $remove_original && is_array( $remove_original ) ) { |
| 534 |
$meta = $remove_original; |
| 535 |
$update_meta = true; |
| 536 |
} |
| 537 |
if ( ! empty( $update_meta ) ) { |
| 538 |
clearstatcache(); |
| 539 |
if ( ! empty( $oldpath ) && is_file( $oldpath ) ) { |
| 540 |
$meta['filesize'] = filesize( $oldpath ); |
| 541 |
} |
| 542 |
wp_update_attachment_metadata( $id, $meta ); |
| 543 |
do_action( 'imsanity_post_process_attachment', $id, $meta ); |
| 544 |
} |
| 545 |
} else { |
| 546 |
$results = array( |
| 547 |
'success' => false, |
| 548 |
'id' => $id, |
| 549 |
/* translators: %s: ID number of the image */ |
| 550 |
'message' => sprintf( esc_html__( 'ERROR: Attachment with ID of %d not found', 'imsanity' ), intval( $id ) ), |
| 551 |
); |
| 552 |
} |
| 553 |
|
| 554 |
// If there is a quota we need to reset the directory size cache so it will re-calculate. |
| 555 |
delete_transient( 'dirsize_cache' ); |
| 556 |
|
| 557 |
return $results; |
| 558 |
} |
| 559 |
|
| 560 |
/** |
| 561 |
* Find the path to a backed-up original (not the full-size version like the core WP function). |
| 562 |
* |
| 563 |
* @param int $id The attachment ID number. |
| 564 |
* @param string $image_file The path to a scaled image file. |
| 565 |
* @param array $meta The attachment metadata. Optional, default to null. |
| 566 |
* @return bool True on success, false on failure. |
| 567 |
*/ |
| 568 |
function imsanity_get_original_image_path( $id, $image_file = '', $meta = null ) { |
| 569 |
$id = (int) $id; |
| 570 |
if ( empty( $id ) ) { |
| 571 |
return false; |
| 572 |
} |
| 573 |
if ( ! wp_attachment_is_image( $id ) ) { |
| 574 |
return false; |
| 575 |
} |
| 576 |
if ( is_null( $meta ) ) { |
| 577 |
$meta = wp_get_attachment_metadata( $id ); |
| 578 |
} |
| 579 |
if ( empty( $image_file ) ) { |
| 580 |
$image_file = get_attached_file( $id, true ); |
| 581 |
} |
| 582 |
if ( empty( $image_file ) || ! is_iterable( $meta ) || empty( $meta['original_image'] ) ) { |
| 583 |
return false; |
| 584 |
} |
| 585 |
|
| 586 |
return trailingslashit( dirname( $image_file ) ) . wp_basename( $meta['original_image'] ); |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* Remove the backed-up original_image stored by WP 5.3+. |
| 591 |
* |
| 592 |
* @param int $id The attachment ID number. |
| 593 |
* @param array $meta The attachment metadata. Optional, default to null. |
| 594 |
* @return bool|array Returns meta if modified, false otherwise (even if an "unlinked" original is removed). |
| 595 |
*/ |
| 596 |
function imsanity_remove_original_image( $id, $meta = null ) { |
| 597 |
imsanity_debug( __FUNCTION__ ); |
| 598 |
$id = (int) $id; |
| 599 |
if ( empty( $id ) ) { |
| 600 |
return false; |
| 601 |
} |
| 602 |
if ( is_null( $meta ) ) { |
| 603 |
$meta = wp_get_attachment_metadata( $id ); |
| 604 |
} |
| 605 |
|
| 606 |
if ( |
| 607 |
$meta && is_array( $meta ) && |
| 608 |
imsanity_get_option( 'imsanity_delete_originals', false ) && |
| 609 |
! empty( $meta['original_image'] ) && function_exists( 'wp_get_original_image_path' ) |
| 610 |
) { |
| 611 |
$original_image = imsanity_get_original_image_path( $id, '', $meta ); |
| 612 |
imsanity_debug( "attempting to remove original image at $original_image" ); |
| 613 |
if ( $original_image && is_file( $original_image ) && is_writable( $original_image ) ) { |
| 614 |
imsanity_debug( 'original is writable, unlinking!' ); |
| 615 |
unlink( $original_image ); |
| 616 |
} |
| 617 |
clearstatcache(); |
| 618 |
if ( empty( $original_image ) || ! is_file( $original_image ) ) { |
| 619 |
imsanity_debug( 'removal successful, updating meta' ); |
| 620 |
unset( $meta['original_image'] ); |
| 621 |
return $meta; |
| 622 |
} |
| 623 |
} elseif ( empty( $meta['original_image'] ) ) { |
| 624 |
imsanity_debug( 'no original_image meta found, nothing to remove' ); |
| 625 |
} elseif ( ! imsanity_get_option( 'imsanity_delete_originals', false ) ) { |
| 626 |
imsanity_debug( 'delete_originals option not enabled, not removing' ); |
| 627 |
} elseif ( ! function_exists( 'wp_get_original_image_path' ) ) { |
| 628 |
imsanity_debug( 'wp_get_original_image_path function does not exist, cannot remove' ); |
| 629 |
} |
| 630 |
return false; |
| 631 |
} |
| 632 |
|
| 633 |
/** |
| 634 |
* Resize an image using the WP_Image_Editor. |
| 635 |
* |
| 636 |
* @param string $file Image file path. |
| 637 |
* @param int $max_w Maximum width to resize to. |
| 638 |
* @param int $max_h Maximum height to resize to. |
| 639 |
* @param bool $crop Optional. Whether to crop image or resize. |
| 640 |
* @param string $suffix Optional. File suffix. |
| 641 |
* @param string $dest_path Optional. New image file path. |
| 642 |
* @return mixed WP_Error on failure. String with new destination path. |
| 643 |
*/ |
| 644 |
function imsanity_image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null ) { |
| 645 |
imsanity_debug( __FUNCTION__ ); |
| 646 |
|
| 647 |
if ( function_exists( 'wp_get_image_editor' ) ) { |
| 648 |
imsanity_debug( "resizing $file to $max_w x $max_h" ); |
| 649 |
if ( $crop ) { |
| 650 |
imsanity_debug( ' cropping enabled' ); |
| 651 |
} |
| 652 |
|
| 653 |
$editor = wp_get_image_editor( $file ); |
| 654 |
if ( is_wp_error( $editor ) ) { |
| 655 |
imsanity_debug( 'get editor error: ' . $editor->get_error_message() ); |
| 656 |
return $editor; |
| 657 |
} |
| 658 |
|
| 659 |
// Default is 82 for JPG, can be anything from 1-100, though the extremes are kind of, well, extreme... |
| 660 |
$quality = imsanity_jpg_quality(); |
| 661 |
$ftype = imsanity_quick_mimetype( $file ); |
| 662 |
if ( 'image/webp' === $ftype ) { |
| 663 |
$quality = imsanity_webp_quality(); |
| 664 |
} elseif ( 'image/avif' === $ftype ) { |
| 665 |
$quality = imsanity_avif_quality(); |
| 666 |
} |
| 667 |
|
| 668 |
// Return 1 to override auto-rotate. |
| 669 |
$orientation = (int) apply_filters( 'imsanity_orientation', imsanity_get_orientation( $file, $ftype ) ); |
| 670 |
// Try to correct for auto-rotation if the info is available. |
| 671 |
switch ( $orientation ) { |
| 672 |
case 3: |
| 673 |
imsanity_debug( 'rotating 180' ); |
| 674 |
$editor->rotate( 180 ); |
| 675 |
break; |
| 676 |
case 6: |
| 677 |
imsanity_debug( 'rotating -90' ); |
| 678 |
$editor->rotate( -90 ); |
| 679 |
break; |
| 680 |
case 8: |
| 681 |
imsanity_debug( 'rotating 90' ); |
| 682 |
$editor->rotate( 90 ); |
| 683 |
break; |
| 684 |
} |
| 685 |
|
| 686 |
$resized = $editor->resize( $max_w, $max_h, $crop ); |
| 687 |
if ( is_wp_error( $resized ) ) { |
| 688 |
imsanity_debug( 'resize error: ' . $resized->get_error_message() ); |
| 689 |
return $resized; |
| 690 |
} |
| 691 |
|
| 692 |
$dest_file = $editor->generate_filename( $suffix, $dest_path ); |
| 693 |
|
| 694 |
// Make sure that the destination file does not exist. |
| 695 |
if ( file_exists( $dest_file ) ) { |
| 696 |
$dest_file = $editor->generate_filename( 'TMP', $dest_path ); |
| 697 |
} |
| 698 |
imsanity_debug( "saving resized image to $dest_file with quality $quality" ); |
| 699 |
|
| 700 |
$editor->set_quality( min( 92, $quality ) ); |
| 701 |
|
| 702 |
// If Modern Image Formats is active, but fallback option is disabled, IMSANITY_ALLOW_CONVERSION will be set to allow AVIF/WebP conversion. |
| 703 |
// Otherwise don't allow conversion by any plugin at this stage--MIF will do it later during thumbnail generation. |
| 704 |
if ( defined( 'IMSANITY_ALLOW_CONVERSION' ) && IMSANITY_ALLOW_CONVERSION ) { |
| 705 |
imsanity_debug( 'Modern Image Formats detected, but no fallback option, conversion allowed' ); |
| 706 |
add_filter( 'wp_editor_set_quality', 'imsanity_editor_quality', 11, 2 ); |
| 707 |
$saved = $editor->save( $dest_file ); |
| 708 |
remove_filter( 'wp_editor_set_quality', 'imsanity_editor_quality', 11 ); |
| 709 |
} else { |
| 710 |
imsanity_debug( "passing mime type $ftype to prevent conversion by Modern Image Formats (or any other plugin)" ); |
| 711 |
remove_all_filters( 'image_editor_output_format' ); |
| 712 |
$saved = $editor->save( $dest_file, $ftype ); |
| 713 |
} |
| 714 |
|
| 715 |
if ( is_wp_error( $saved ) ) { |
| 716 |
imsanity_debug( 'save error: ' . $saved->get_error_message() ); |
| 717 |
return $saved; |
| 718 |
} |
| 719 |
|
| 720 |
if ( ! empty( $saved['path'] ) && $saved['path'] !== $dest_file && is_file( $saved['path'] ) ) { |
| 721 |
$dest_file = $saved['path']; |
| 722 |
} |
| 723 |
imsanity_debug( "resized image saved to $dest_file" ); |
| 724 |
return $dest_file; |
| 725 |
} |
| 726 |
return false; |
| 727 |
} |
| 728 |
|