[wp-trac] [WordPress Trac] #63481: WP_Image_Editor_Imagick takes longer time to upload image

WordPress Trac noreply at wordpress.org
Wed May 28 12:59:44 UTC 2025


#63481: WP_Image_Editor_Imagick takes longer time to upload image
-------------------------------------+------------------------------
 Reporter:  alimuzzamanalim          |       Owner:  (none)
     Type:  defect (bug)             |      Status:  new
 Priority:  normal                   |   Milestone:  Awaiting Review
Component:  Media                    |     Version:
 Severity:  major                    |  Resolution:
 Keywords:  needs-patch 2nd-opinion  |     Focuses:  performance
-------------------------------------+------------------------------

Comment (by siliconforks):

 Replying to [comment:8 nosilver4u]:
 > @SirLouen Not only the call to `getImageColors` will be slow on a non-
 indexed PNG, but `quantizeImage` is also likely adding a bit of overhead.

 Maybe a bit ... But I think the main issue is that `getImageColors` is
 just pathologically slow for some reason.

 If I try calling `quantizeImage` with a fixed hard-coded value (256) for
 the number of colors, it only seems to take maybe a few hundredths of a
 second.  But if I try calling `getImageColors`, that appears to be where
 the real problem is:

 {{{#!php
 <?php

 $filename = '44.png';
 $width = 1024;
 $height = 665;

 $output_filename = '44-1024x665.png';
 $start_time = hrtime( true ) ;
 $image = new Imagick( $filename );
 $image->resizeImage( $width, $height, Imagick::FILTER_TRIANGLE, 1 );
 $image->writeImage( $output_filename );
 $end_time = hrtime( true );
 $nanoseconds = $end_time - $start_time;
 $seconds = $nanoseconds / 1e9;
 echo 'Size: ' . filesize( $output_filename ) . "\n";
 echo 'Elapsed time: ' . sprintf( '%.3F', $seconds ) . "\n";

 $output_filename = '44-1024x665-quantizeImage.png';
 $start_time = hrtime( true ) ;
 $image = new Imagick( $filename );
 $image->resizeImage( $width, $height, Imagick::FILTER_TRIANGLE, 1 );
 $image->quantizeImage( 256, $image->getColorspace(), 0, false, false );
 $image->writeImage( $output_filename );
 $end_time = hrtime( true );
 $nanoseconds = $end_time - $start_time;
 $seconds = $nanoseconds / 1e9;
 echo 'Size (quantizeImage): ' . filesize( $output_filename ) . "\n";
 echo 'Elapsed time: ' . sprintf( '%.3F', $seconds ) . "\n";

 $output_filename = '44-1024x665-getImageColors.png';
 $start_time = hrtime( true ) ;
 $image = new Imagick( $filename );
 $image->resizeImage( $width, $height, Imagick::FILTER_TRIANGLE, 1 );
 $current_colors = $image->getImageColors();
 $max_colors = min( 256, $current_colors );
 $image->quantizeImage( $max_colors, $image->getColorspace(), 0, false,
 false );
 $image->writeImage( $output_filename );
 $end_time = hrtime( true );
 $nanoseconds = $end_time - $start_time;
 $seconds = $nanoseconds / 1e9;
 echo 'Size (getImageColors): ' . filesize( $output_filename ) . "\n";
 echo 'Elapsed time: ' . sprintf( '%.3F', $seconds ) . "\n";
 }}}

 The above code tries three different things:

 1. Resize the image without reducing the number of colors
 2. Resize the image calling `quantizeImage` with a fixed value of 256
 3. Resize the image calling `quantizeImage` with a value derived from
 `getImageColors`

 This is the result I got:


 {{{
 Size: 505955
 Elapsed time: 0.526
 Size (quantizeImage): 185245
 Elapsed time: 0.558
 Size (getImageColors): 185245
 Elapsed time: 10.328
 }}}

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


More information about the wp-trac mailing list