[wp-trac] [WordPress Trac] #55457: Function media_handle_sideload doesn't return with some pdf files due to Imagick

WordPress Trac noreply at wordpress.org
Thu Mar 24 10:21:41 UTC 2022


#55457: Function media_handle_sideload doesn't return with some pdf files due to
Imagick
--------------------------+-----------------------------
 Reporter:  alceomazza    |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Upload        |    Version:  5.9.2
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 I'm trying to upload procedurally some media with `media_handle_sideload`,
 but some pdf files cause the execution flow to froze without
 `media_handle_sideload` returning a value nor launching an `Exception`.
 This happens when the Imagick module is installed.

 === Environment
 * **OS**: CentOS 7
 * **PHP Version**: 7.4.28
 * **WordPress Version**: 5.9.2
 * **Imagick Version**: 6.9.10-68

 === Debug snippet
 The bug can be verified calling the function `alceo_handle_media_debug`
 {{{#!php
 <?php

 function alceo_handle_media_debug() {
     alceo_handle_media( 'https://life365.s3.eu-
 central-1.amazonaws.com/IT/p/11822/file/FilamentClear_2018.pdf' );
 }

 function alceo_handle_media( $url ) {
     $path_info = pathinfo( parse_url( $url )['path'] );
     $file_type = wp_check_filetype( $path_info['basename'] );

     if ( ! $file_type['ext'] ) {
         return;
     }

     require_once( ABSPATH . 'wp-admin/includes/media.php' );
     require_once( ABSPATH . 'wp-admin/includes/file.php' );
     require_once( ABSPATH . 'wp-admin/includes/image.php' );

     $media_title = $path_info['basename'];

     $tmp_name = download_url( $url );

     if ( is_wp_error( $tmp_name ) ) {
         error_log( print_r( $tmp_name, true ) );
     }

     $file = array(
         'name' => $media_title,
         'tmp_name' => $tmp_name,
     );

     try {
         $media_id = media_handle_sideload(
             $file, 0, null, array(
                 'post_author' => 0,
                 'post_name' => sanitize_title( $media_title ),
             )
         );
     } catch ( \Exception $e ) {
         error_log( print_r( $e, true ) );
     }

     if ( is_wp_error( $media_id ) ) {
         error_log( print_r( $tmp_name, true ) );
     }
 }
 }}}

 === Testing
 I've tried to test adding these `error_log` lines into method
 `pdf_load_source` of class `WP_Image_Editor_Imagick` (file `wp-includes
 /class-wp-image-editor-imagick.php`)
 {{{#!php
 <?php

 class WP_Image_Editor_Imagick extends WP_Image_Editor {

     // ...

     protected function pdf_load_source() {
         error_log( "Into pdf_load_source" );
         $filename = $this->pdf_setup();
         error_log( "Filename: " . print_r( $filename, true ) );

         if ( is_wp_error( $filename ) ) {
             return $filename;
         }

         try {
             error_log( "Trying with " . print_r( $this->image, true ) );
             // When generating thumbnails from cropped PDF pages,
 Imagemagick uses the uncropped
             // area (resulting in unnecessary whitespace) unless the
 following option is set.
             $this->image->setOption( 'pdf:use-cropbox', true );
             error_log( "Option use cropbox true" );

             // Reading image after Imagick instantiation because
 `setResolution`
             // only applies correctly before the image is read.
             $this->image->readImage( $filename );
             error_log( "Image read tried" );
         } catch ( Exception $e ) {
             error_log( "Catched" );
             // Attempt to run `gs` without the `use-cropbox` option. See
 #48853.
             $this->image->setOption( 'pdf:use-cropbox', false );
             error_log( "Option use cropbox false" . print_r( '', true ) );

             $this->image->readImage( $filename );
             error_log( "Image read" . print_r( '', true ) );
         }

         return true;
     }

     // ...
 }
 }}}

 with this result in `debug.log`:

 {{{
 [23-Mar-2022 14:36:23 UTC] Into pdf_load_source
 [23-Mar-2022 14:36:23 UTC] Filename: /<host_path>/wp-
 content/uploads/2022/03/Product-XXXXX-FilamentClear_2018-1.pdf[0]
 [23-Mar-2022 14:36:23 UTC] Trying with Imagick Object
 (
 )

 [23-Mar-2022 14:36:23 UTC] Option use cropbox true

 }}}

 And none of the following `error_log` prints coming after this last line.

 === Other details
 * This happens with some pdf files (the one from the url in the code being
 one of them)
 * I've tried changing php version to the latest 8.1, but the issue
 persists

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


More information about the wp-trac mailing list