[wp-trac] [WordPress Trac] #43741: Improve media_sideload_image() image extensions detection

WordPress Trac noreply at wordpress.org
Thu Apr 12 00:30:44 UTC 2018


#43741: Improve media_sideload_image() image extensions detection
-------------------------+-----------------------------
 Reporter:  Karzin       |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Media        |    Version:  4.9.5
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 **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
 <?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
 <?php
 add_filter( 'http_request_args', function($args,$url){
     $args['reject_unsafe_urls'] = false;
     return $args;
 }),10,2 );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/43741>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list