Skip to Content

limit_image_size()

Limit the requested image to a max size to prevent huge filesize images from loading on the front-end.

PHP April 9, 2019

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Demo


Serving images with a max width of 1200px.



get_post_thumbnail()

nebula()->get_thumbnail_src()

Additional Notes

Original size of this test image is 7360×4545.

This image is loaded via the WYSIWYG editor from the media library.

When this will not control output image sizes:

This function listens for full sizes to be used and limits them. Therefore there are plenty of situations where this will not limit output image sizes.

  • If the Nebula Option “Limit Image Dimensions” is disabled (which it is by default)
  • When specified image sizes are added and used
  • If an image is hard-coded into a template file or stylesheet
  • Inside galleries
  • This may not work inside third-party plugins
  • (Possibly others too)

Here is another test using the WordPress Block Editor.

Source File

Located in /libs/Optimization.php on line 112.

No Hooks

This function does not have any filters or actions available. Request one?
PHP
        public function limit_image_size($size, $id=false){
            if ( $this->get_option('limit_image_dimensions') ){
                if ( is_string($size) && ($size === 'post-thumbnail' || $size === 'full') ){
                    if ( $this->is_save_data() ){
                        return 'max_size_less';
                    }

                    return 'max_size';
                }
            }

            return $size;
        }

Override

This function can not be short-circuited with an override filter. Request one?