Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/d1005c7ee00b30b7a32023e2c9c33cb0 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/d1005c7ee00b30b7a32023e2c9c33cb0 to your computer and use it in GitHub Desktop.
[Smush Pro] - Fix some images didn't update the real size
<?php
/**
* Plugin Name: [Smush Pro] - Fix some images didn't update the real size
* Description: [Smush Pro] - Fix some images didn't update the real size - 1144397401151073
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'after_setup_theme', 'wpmudev_smush_fix_some_images_did_not_update_real_size_func', 100 );
function wpmudev_smush_fix_some_images_did_not_update_real_size_func() {
if( defined('WP_SMUSH_VERSION') && class_exists( 'Smush\WP_Smush' ) ){
// return if disabled auto resize
if( ! ( Smush\Core\Settings::get_instance()->get( 'original' ) && Smush\Core\Settings::get_instance()->get( 'resize' ) ) ){
return;
}
class WPMUDEV_SM_Update_Real_File_Size{
public function __construct(){
add_action( 'image_smushed', array( $this, 'maybe_update_real_size' ) );
}
public function maybe_update_real_size( $id ){
$meta = wp_get_attachment_metadata( $id, true );
if( $meta ){
$file_path = Smush\Core\Helper::get_attached_file( $id );
$real_size = getimagesize( $file_path );
if( $meta['width'] > $real_size[0] || $meta['height'] > $real_size[1] ){
$meta['width'] = $real_size[0];
$meta['height'] = $real_size[1];
return wp_update_attachment_metadata( $id, $meta );
}
}
return false;
}
}
$run = new WPMUDEV_SM_Update_Real_File_Size;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment