Created
October 19, 2019 01:57
-
-
Save wpmudev-sls/d1005c7ee00b30b7a32023e2c9c33cb0 to your computer and use it in GitHub Desktop.
[Smush Pro] - Fix some images didn't update the real size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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