[wp-trac] [WordPress Trac] #42663: Imagick support for stream wrappers

WordPress Trac noreply at wordpress.org
Mon Mar 30 13:17:29 UTC 2020


#42663: Imagick support for stream wrappers
-----------------------------------+------------------------------
 Reporter:  calin                  |       Owner:  (none)
     Type:  enhancement            |      Status:  new
 Priority:  normal                 |   Milestone:  Awaiting Review
Component:  Media                  |     Version:
 Severity:  normal                 |  Resolution:
 Keywords:  has-patch 2nd-opinion  |     Focuses:
-----------------------------------+------------------------------

Comment (by jimyaghi):

 Hey guys i notice the base class WP_Image_Editor assumes a stream is
 always output to stdout and thus it always captures the image using
 ob_start() and friends then writes it to a the stream.

 However, only the WP_Image_Editor_GD class has this behaviour because the
 callbacks imagegif()/imagepng()/imgejpeg() default to stdout when no
 filename specified. The WP_Image_Editor_Imagick class does NOT do this and
 thus does not work to write to streams.

 This can be easily fixed.

 WP_Image_Editor_GD uses the following override method to remove the
 filename argument for streams.


 {{{#!php

         /**
          * Either calls editor's save function or handles file as a
 stream.
          *
          * @since 3.5.0
          *
          * @param string|stream $filename
          * @param callable $function
          * @param array $arguments
          * @return bool
          */
         protected function make_image( $filename, $function, $arguments )
 {
                 if ( wp_is_stream( $filename ) ) {
                         $arguments[1] = null;
                 }

                 return parent::make_image( $filename, $function,
 $arguments );
         }

 }}}


 Perhaps we can do something like the following in WP_Image_Editor_Imagick:

 {{{#!php

        protected function make_image( $filename, $function, $arguments ) {
                 if ( wp_is_stream( $filename ) ) {
                         $function = 'echo';
                         $arguments = [$this->image->getImageBlob()];
                 }

                 return parent::make_image( $filename, $function,
 $arguments );
         }

 }}}

 This way the behaviour is consistent in both classes and streams always
 output to stdout.

 Alternatively, we can skip the extra method and do a condition in the
 _save() method like so:

 {{{#!php
 <?php
 $this->make_image( $filename, array( $image, 'writeImage' ), array(
 $filename ) );
 }}}

 becomes

 {{{#!php

 if(is_stream($filename)) {
     $this->make_image( $filename, 'echo', array( $image->getImageBlob() )
 );
 } else {
     $this->make_image( $filename, array( $image, 'writeImage' ), array(
 $filename ) );
 }

 }}}


 ~j

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/42663#comment:16>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list