WordPress.org

Make WordPress Core

Opened 18 months ago

Last modified 18 months ago

#43741 new enhancement

Improve media_sideload_image() image extensions detection

Reported by: Karzin Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.5
Component: Media Keywords:
Focuses: Cc:
PR Number:

Description

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
https://crmall.generalshopping.com.br:8097/Api/store/image/bkxjNm5LU0RzQW5rdVNvUHZVSURkQT09

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

<?php
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:

<?php
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 18 months ago.
43742.2.diff (889 bytes) - added by Karzin 18 months ago.

Download all attachments as: .zip

Change History (4)

@Karzin
18 months ago

#1 follow-up: @soulseekah
18 months 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.

@Karzin
18 months ago

#2 in reply to: ↑ 1 @Karzin
18 months ago

Replying to soulseekah:
Thanks!

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.