So I’ve been able to almost get up and running with foresight.js to be able to serve the right size of media files on the fly.

There’s just one problem, if I upload a file with the same dimensions as the largest image size specified (in this example, the image size is 2400×1600), it will not get renamed, it just stays as filename.jpg rather than filename-large-2x.jpg. Any idea why this happens?

add_image_size('small-gallery', 9999, 259);
add_image_size('small-2x-gallery', 9999, 518);
add_image_size('medium-gallery', 9999, 552); 
add_image_size('medium-2x-gallery', 9999, 1104); 
add_image_size('large-gallery', 1200, 800); 
add_image_size('large-2x-gallery', 2400, 1600); 
add_filter( 'image_make_intermediate_size', 'rename_images' );
function rename_images( $image ) 
{
    $info = pathinfo($image);
    $dir = $info['dirname'] . '/';
    $ext = '.' . $info['extension'];
    $name = wp_basename( $image, "$ext" );
    $img = wp_get_image_editor( $image );
    $img_size = $img->get_size();
    switch( $img_size['height'])
    {
        case '259':
            $size_based_img_name_suffix = 'small';
            break;
        case '518':
            $size_based_img_name_suffix = 'small-2x';
            break;
        case '552';
            $size_based_img_name_suffix = 'medium';
            break;
        case '1104';
            $size_based_img_name_suffix = 'medium-2x';
            break;
    }
    switch( $img_size['width'] )
    {
        case '150' :
            $size_based_img_name_suffix = 'thumbnail';
            break;
        case '1200' :
            $size_based_img_name_suffix = 'large';
            break;
        case '2400' :
            $size_based_img_name_suffix = 'large-2x';
            break;
    }
    $name_suffix = substr( $name, 0, strrpos( $name, '-' ) );
    $new_name = $dir . $name_suffix . '-' . $size_based_img_name_suffix . $ext;
    $did_it = rename( $image, $new_name );
    if( $did_it )
        return $new_name;
    return $image;
}

Read more here: Alternative naming convention for uploads, works for all but one image size


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , , ,

Wordpress related questions and answers: