Make WordPress Core

Opened 6 years ago

Last modified 6 years ago

#43741 new enhancement

Improve media_sideload_image() image extensions detection

Reported by: karzin's profile Karzin Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.5
Component: Media Keywords:
Focuses: Cc:


media_sideload_image() applies a pattern to the URL trying to detect image extensions.

There are cases where Image URLs don't end on jpg, png or any other extensions.
They can be simply a number or any other weird urls, but they are actually just fine

So I think it would be a good idea trying to detect the image extensions on these cases, like this:

if ( ! $matches ) {
        $image_type = exif_imagetype( $file );
        if ( $image_type ) {
                $fileextension = image_type_to_extension( $image_type );
                $matches       = array( $fileextension );
        } else {
                return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL' ) );

It's been working on my tests pretty nice.
The only other thing I have to do in order to test such urls is turning reject_unsafe_urls to false, like this:

add_filter( 'http_request_args', function($args,$url){
    $args['reject_unsafe_urls'] = false;
    return $args;
}),10,2 );

Attachments (2)

43741.diff (871 bytes) - added by Karzin 6 years ago.
43742.2.diff (889 bytes) - added by Karzin 6 years ago.

Download all attachments as: .zip

Change History (4)

6 years ago

#1 follow-up: @soulseekah
6 years ago

Hey, welcome to Trac! :)

image_type_to_extension and exif_imagetype require extra PHP libraries to be available (GD and Exif extensions respectively).

There's core's wp_get_image_mime which may be useful instead? It uses the same functions mostly, but provides graceful fallback.

6 years ago

#2 in reply to: ↑ 1 @Karzin
6 years ago

Replying to soulseekah:

Great then!
I just prepated a new patch using wp_get_image_mime() instead.

It's the 43742.2.diff

Note: See TracTickets for help on using tickets.