apply_orientation(); return false; } /** * {@inheritDoc} * * @param string $dest Output path with the matching extension. * @param EncodeOptions $options Encoding parameters. * @param array{width: int, height: int, crop: bool}|null $box Target size, or null for current size. * @return array{width: int, height: int}|WP_Error */ public function render( string $dest, EncodeOptions $options, ?array $box = null ): array|WP_Error { $original_size = $this->size; $image = $this->image; $resized = false; if ( null !== $box && ( $box['width'] !== $this->size['width'] || $box['height'] !== $this->size['height'] ) ) { $result = $this->_resize( $box['width'], $box['height'], $box['crop'] ); if ( is_wp_error( $result ) ) { $this->size = $original_size; return $result; } $image = $result; $resized = true; } $size = array( 'width' => (int) $this->size['width'], 'height' => (int) $this->size['height'], ); $written = $image instanceof GdImage && $this->write( $image, $dest, $options ); if ( $resized && $image instanceof GdImage ) { imagedestroy( $image ); } $this->size = $original_size; return $written ? $size : new WP_Error( 'engine_error', 'GD could not write ' . $options->mime ); } /** * {@inheritDoc} * * @param string $dest Output path with the matching extension. * @param EncodeOptions $options Encoding parameters. * @return array{width: int, height: int}|WP_Error */ public function render_animated( string $dest, EncodeOptions $options ): array|WP_Error { return new WP_Error( 'animated_gd', 'GD cannot encode animation.' ); } /** * Applies the EXIF orientation through the core rotate and flip operations. * * @return void */ private function apply_orientation(): void { switch ( ( new MetadataStripper() )->orientation( (string) $this->file ) ) { case 2: $this->flip( false, true ); break; case 3: $this->rotate( 180 ); break; case 4: $this->flip( true, false ); break; case 5: $this->rotate( 90 ); $this->flip( true, false ); break; case 6: $this->rotate( 270 ); break; case 7: $this->rotate( 90 ); $this->flip( false, true ); break; case 8: $this->rotate( 90 ); break; } } /** * Writes an image in the requested format. * * @param GdImage $image Pixels to write. * @param string $dest Output path. * @param EncodeOptions $options Encoding parameters. * @return bool */ private function write( GdImage $image, string $dest, EncodeOptions $options ): bool { switch ( $options->mime ) { case 'image/jpeg': imageinterlace( $image, true ); return imagejpeg( $image, $dest, $options->quality ); case 'image/png': if ( imageistruecolor( $image ) ) { imagealphablending( $image, false ); imagesavealpha( $image, true ); } return imagepng( $image, $dest, 9 ); case 'image/gif': return imagegif( $image, $dest ); case 'image/webp': if ( ! imageistruecolor( $image ) ) { imagepalettetotruecolor( $image ); } imagealphablending( $image, false ); imagesavealpha( $image, true ); return imagewebp( $image, $dest, $options->lossless ? IMG_WEBP_LOSSLESS : $options->quality ); case 'image/avif': if ( ! imageistruecolor( $image ) ) { imagepalettetotruecolor( $image ); } imagealphablending( $image, false ); imagesavealpha( $image, true ); return function_exists( 'imageavif' ) && imageavif( $image, $dest, $options->quality, 8 ); } return false; } }