Skip to Content
Menu

create_max_width_size_proportionally()

Dynamically create image a maximum width image size to prevent accidentally loading huge filesize images on the front-end.

PHP March 24, 2018

Usage

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

Additional Notes

These image sizes will not appear in lists (since they are dynamically created and have variable heights).

See limit_image_size() page for demo.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

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

    No Hooks

    This function does not have any filters or actions available. Request one?
    PHP
            public function create_max_width_size_proportionally($sizes, $metadata){
                if ( !empty($metadata['width']) && !empty($metadata['height']) ){
                    //Create a max size of 1200px wide
                    $lg_width = $metadata['width'];
                    $lg_height = $metadata['height'];
                    if ( $metadata['width'] > 1200 ){
                        $lg_width = 1200;
                        $lg_height = ($metadata['height']*$lg_width)/$metadata['width']; //Original Height * Desired Width / Original Width = Desired Height
                    }
    
                    $sizes['max_size'] = array(
                        'width' => $lg_width,
                        'height' => $lg_height,
                        'crop' => true
                    );
    
                    //Create a max size of 800px wide for use with the Save Data header
                    $sm_width = $metadata['width'];
                    $sm_height = $metadata['height'];
                    if ( $metadata['width'] > 800 ){
                        $sm_width = 800;
                        $sm_height = ($metadata['height']*$sm_width)/$metadata['width']; //Original Height * Desired Width / Original Width = Desired Height
                    }
    
                    $sizes['max_size_less'] = array(
                        'width' => $sm_width,
                        'height' => $sm_height,
                        'crop' => true
                    );
                }
    
                return $sizes;
            }
    

    Override

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