• Resolved elyrko

    (@elyrko)


    Whenever your plugin starts processing a png image and for some reason that image has errors it will fatally crash instead of producing a warning or ignoring the check.

    Here’s the error produced by your plugin:

    PHP Fatal error:  Uncaught TypeError: imagecolortransparent(): Argument #1 ($image) must be of type GdImage, bool given in ....

    And here’s the code responsible for the error:
    plugins/ewww-image-optimizer/common.php 9238

    $image = imagecreatefrompng( $filename );
    if ( imagecolortransparent( $image ) >= 0 ) {
       ewwwio_debug_message( 'transparency found' );
       return true;
    }
    ewwwio_debug_message( 'preparing to scan image' );

    The issue comes when $image variable has a bool for the return value. There needs to be a check in case that happens. For now i’ve just added the check myself but it will get updated everytime the plugin updates and i need to redo it every time.

    Here’s my solution

    $image = imagecreatefrompng( $filename );
    if(is_a($image, 'GDImage')){
      if ( imagecolortransparent( $image ) >= 0 ) {
        ewwwio_debug_message( 'transparency found' );
        return true;
      }
      ewwwio_debug_message( 'preparing to scan image' );
      for ( $y = 0; $y < $height; $y++ ) {
        for ( $x = 0; $x < $width; $x++ ) {
          $color = imagecolorat( $image, $x, $y );
          $rgb   = imagecolorsforindex( $image, $color );
          if ( $rgb['alpha'] > 0 ) {
    	ewwwio_debug_message( 'transparency found' );
    	return true;
          }
        }
      }
    }
    • This topic was modified 3 years ago by elyrko.
    • This topic was modified 3 years ago by elyrko.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘FATAL ERROR on png transparency check.’ is closed to new replies.