mindkomm / timmy

Advanced image handling for Timber.
MIT License
171 stars 13 forks source link

When the timmy/generate_srcset_sizes is set to true the 'oversize' option is ignored #46

Closed jeroenbraspenning closed 1 year ago

jeroenbraspenning commented 3 years ago

I stumbled across an issue with the timmy/generate_srcset_sizes hook. When set to true, the system will automatically generate all responsive sizes. This works perfect, but not if you have the following oversize configuration:

'oversize' => array( 'allow' => false, 'style_attr' => false ) It should not generate sizes bigger than the source image, but it does. The function (generate_srcset_size, timmy.php, r864) which generates the sizes doesn't check the oversize settings. As a temporary workaround i did the following, which fixes it for now.

rule 904:

// Generate additional image sizes used for srcset.
if (isset($img_size['srcset'])) {
    foreach ($img_size['srcset'] as $srcset_size) {
        list($width, $height) = Helper::get_dimensions_for_srcset_size(
            $img_size['resize'],
            $srcset_size
        );

        // hacky fix to make sure that we are not generating image sizes
        // which are bigger that the max size of the image

        // create a cloned version of the $img_size array
        $cloned_img_size = $img_size;
        // add the "to be created" size
        $cloned_img_size['resize'] = array($width,$height);

        // put the img_size through the get_image_params function to make sure
        // it is checked against the "oversize" settings.
        // $width and $height will be overriden by the list() helper
        list(
            $file_src,
            $width,
            $height,
        ) = Timmy::get_image_params($timber_image, $cloned_img_size);

        // For the new source, we use the same $crop and $force values as the default image.
        self::resize($img_size, $file_src, $width, $height, $crop, $force);
    }
}

Thanks for your work!

gchtr commented 3 years ago

Hey @jeroenbraspenning

I think this is a duplicate of https://github.com/mindkomm/timmy/issues/36. Because I have to refactor quite a few things to get this right, it’s taking a little longer.

gchtr commented 1 year ago

This was fixed in the first beta for Timmy 1.0.0. The most current beta version is 1.0.0-beta.3. You can test it with that version.