Opened 7 years ago
Last modified 7 years ago
#43741 new enhancement
Improve media_sideload_image() image extensions detection
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 4.9.5 |
Component: | Media | Keywords: | |
Focuses: | Cc: |
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)
Change History (4)
Note: See
TracTickets for help on using
tickets.
Hey, welcome to Trac! :)
image_type_to_extension
andexif_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.