ivate function update_image_resource( $image, string $extension ) { if ( ! function_exists( 'imageistruecolor' ) ) { throw new FunctionUnavailableException( 'imageistruecolor' ); } if ( ! imageistruecolor( $image ) ) { if ( ! function_exists( 'imagepalettetotruecolor' ) ) { throw new FunctionUnavailableException( 'imagepalettetotruecolor' ); } imagepalettetotruecolor( $image ); } switch ( $extension ) { case 'png': if ( ! function_exists( 'imagealphablending' ) ) { throw new FunctionUnavailableException( 'imagealphablending' ); } imagealphablending( $image, false ); if ( ! function_exists( 'imagesavealpha' ) ) { throw new FunctionUnavailableException( 'imagesavealpha' ); } imagesavealpha( $image, true ); break; } return $image; } /** * {@inheritdoc} * * @throws ConversionErrorException * @throws FunctionUnavailableException * @throws ResolutionOversizeException */ public function convert_image_to_output( $image, string $source_path, string $output_path, string $format, array $plugin_settings ) { $function = self::get_format_function( $format ); if ( $function === null ) { return; } $image = apply_filters( 'webpc_gd_before_saving', $image, $source_path ); $output_quality = min( $plugin_settings[ ImagesQualityOption::OPTION_NAME ], self::MAX_METHOD_QUALITY ); if ( ! function_exists( $function ) ) { throw new FunctionUnavailableException( $function ); } elseif ( ( imagesx( $image ) > 8192 ) || ( imagesy( $image ) > 8192 ) ) { throw new ResolutionOversizeException( $source_path ); } elseif ( is_callable( $function ) && ! $function( $image, $output_path, $output_quality ) ) { throw new ConversionErrorException( $source_path ); } if ( filesize( $output_path ) % 2 === 1 ) { file_put_contents( $output_path, "\0", FILE_APPEND ); } } /** * @param string $source_path . * * @link https://www.php.net/manual/en/function.imagecreatefromgif.php#104473 */ private function is_animated( string $source_path ): bool { if ( ! ( $fh = @fopen( $source_path, 'rb' ) ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged return false; } $count = 0; while ( ! feof( $fh ) && ( $count < 2 ) ) { $chunk = fread( $fh, 1024 * 100 ); $count = $count + preg_match_all( '#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk ?: '', $matches ); } fclose( $fh ); return ( $count > 1 ); } }