humanmade / WPThumb

:warning: UNMAINTAINED :warning: On demand image resizing for WordPress
https://humanmade.co.uk/wpthumb/
170 stars 32 forks source link

Non-standard file extensions return incorrect cached filename #121

Open doytch opened 9 years ago

doytch commented 9 years ago

We're currently using WPThumb to provide caching for images in RSS feeds and just had a support ticket that revealed what appears to be an interesting little bug in wpthumb().

The URLs we're having issues with are of the following type: http://static.wixstatic.com/media/d0e46d_df8b7f8076b84f9e993305c484132979.jpg_srb_300_300_75_22_0.50_1.20_0.00_jpg_srb

Obviously, the filename has a few quirks in it. When the image is downloaded and stored in the cached folder, everything appears to be functioning well. For example, that image will get stored some place like wp-content/uploads/cache/remote/static-wixstatic-com/3427518221.jpg

However, when we attempt to look up the cached path for it later, wpthumb() returns an URL like wp-content/uploads/cache/remote/static-wixstatic-com/3427518221.00_jpg_srb. Note the extension is .00_jpg_srb rather than .jpg

In the meantime, I've worked around this with our customers by using the following filter:

add_filter( 'wpthumb_cache_file_path', 'my_interpret_bad_extensions', 10, 2);
function my_interpret_bad_extensions( $path, $obj ) {
    $basename = basename( $path );
    $dirname = dirname( $path );
    $components = explode( '.', $basename );

    if ( sizeof( $components ) !== 2 ) {
        return $path;
    }

    $name = $components[0];
    $ext = $components[1];

    if ( strlen( $ext ) > 4 ) {
        if ( stripos( $ext, 'jpg' ) !== FALSE ) {
            $ext = 'jpg';
        } else if ( stripos( $ext, 'png' ) !== FALSE ) {
            $ext = 'png';
        } else if ( stripos( $ext, 'gif' ) !== FALSE ) {
            $ext = 'gif';
        }
    }

    return trailingslashit( $dirname ) . $name . '.' . $ext;
}