apply_orientation(); return $this->convert_to_srgb(); } /** * {@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; $original_image = $this->image; try { $this->image = $original_image->getImage(); if ( null !== $box && ( $box['width'] !== $this->size['width'] || $box['height'] !== $this->size['height'] ) ) { $resized = $this->resize( $box['width'], $box['height'], $box['crop'] ); if ( is_wp_error( $resized ) ) { return $resized; } } $this->image->stripImage(); $this->apply_encode_options( $this->image, $options ); $this->image->writeImage( $dest ); return array( 'width' => (int) $this->size['width'], 'height' => (int) $this->size['height'], ); } catch ( Throwable $e ) { return new WP_Error( 'engine_error', $e->getMessage() ); } finally { if ( $this->image !== $original_image ) { $this->image->clear(); } $this->image = $original_image; $this->size = $original_size; } } /** * {@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 { try { $frames = $this->image->coalesceImages(); $output = 'image/gif' === $options->mime ? $frames->optimizeImageLayers() : $frames; foreach ( $output as $frame ) { $frame->stripImage(); $this->apply_encode_options( $frame, $options ); } $output->setFirstIterator(); $output->writeImages( $dest, true ); if ( $output !== $frames ) { $output->clear(); } $frames->clear(); return array( 'width' => (int) $this->size['width'], 'height' => (int) $this->size['height'], ); } catch ( Throwable $e ) { return new WP_Error( 'engine_error', $e->getMessage() ); } } /** * Returns a downscaled copy for similarity scoring. * * @param int $longest_edge Maximum width or height. * @return Imagick */ public function comparison_copy( int $longest_edge ): Imagick { $copy = $this->image->getImage(); if ( max( (int) $this->size['width'], (int) $this->size['height'] ) > $longest_edge ) { $copy->thumbnailImage( $longest_edge, $longest_edge, true ); } $copy->stripImage(); return $copy; } /** * Applies stripping and progressive encoding to everything WordPress saves. * * @param Imagick $image Image being saved. * @param string|null $filename Output path. * @param string|null $mime_type Output type. * @return array|WP_Error */ protected function _save( $image, $filename = null, $mime_type = null ) { $this->strip_before_save(); $saved = parent::_save( $image, $filename, $mime_type ); if ( ! is_wp_error( $saved ) && ! empty( $saved['path'] ) ) { ( new MetadataStripper() )->scrub_gps_file( (string) $saved['path'] ); } return $saved; } /** * Strips metadata when enabled, keeping any wide-gamut profile the pixels depend on. * * @return void */ private function strip_before_save(): void { $settings = Plugin::instance()->get( Settings::class ); if ( ! $settings->flag( 'strip_metadata' ) ) { return; } try { // A pending orientation tag means WordPress did not rotate; removing it would turn the image sideways. if ( $this->image->getImageOrientation() > Imagick::ORIENTATION_TOPLEFT ) { return; } $profiles = $this->image->getImageProfiles( 'icc', true ); $icc = isset( $profiles['icc'] ) ? (string) $profiles['icc'] : null; $this->image->stripImage(); if ( null !== $icc && ( $settings->flag( 'keep_icc' ) || ! IccProfile::is_srgb( $icc ) ) ) { // profileImage() would do nothing here without LCMS. This attaches the bytes as they are. $this->image->setImageProfile( 'icc', $icc ); } } catch ( ImagickException $e ) { // A failed strip must not fail the upload; GPS is still scrubbed after the save. return; } } /** * Rotates pixels to match the EXIF orientation, then clears the tag. * * @return void */ private function apply_orientation(): void { $image = $this->image; $orientation = $image->getImageOrientation(); switch ( $orientation ) { case Imagick::ORIENTATION_TOPRIGHT: $image->flopImage(); break; case Imagick::ORIENTATION_BOTTOMRIGHT: $image->rotateImage( 'none', 180 ); break; case Imagick::ORIENTATION_BOTTOMLEFT: $image->flipImage(); break; case Imagick::ORIENTATION_LEFTTOP: $image->transposeImage(); break; case Imagick::ORIENTATION_RIGHTTOP: $image->rotateImage( 'none', 90 ); break; case Imagick::ORIENTATION_RIGHTBOTTOM: $image->transverseImage(); break; case Imagick::ORIENTATION_LEFTBOTTOM: $image->rotateImage( 'none', 270 ); break; default: return; } $image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT ); $image->setImagePage( 0, 0, 0, 0 ); $this->update_size(); } /** * Converts to sRGB through the embedded profile, or by formula when there is none. * * @return bool Whether the pixels are now sRGB. */ private function convert_to_srgb(): bool { $image = $this->image; $cmyk = Imagick::COLORSPACE_CMYK === $image->getImageColorspace(); $profiles = $image->getImageProfiles( 'icc', true ); $icc = isset( $profiles['icc'] ) ? (string) $profiles['icc'] : null; if ( null !== $icc && ! $cmyk && IccProfile::is_srgb( $icc ) ) { return true; } if ( null !== $icc ) { if ( Plugin::instance()->get( Capabilities::class )->can_convert_profiles() ) { $image->profileImage( 'icc', IccProfile::srgb() ); $image->setImageColorspace( Imagick::COLORSPACE_SRGB ); return true; } if ( ! $cmyk ) { // Without color management the pixels stay as they are, so the profile must stay too. return false; } } if ( $cmyk ) { $image->transformImageColorspace( Imagick::COLORSPACE_SRGB ); } return true; } /** * Sets the writer options for a format. * * @param Imagick $image Target image or frame. * @param EncodeOptions $options Encoding parameters. * @return void */ private function apply_encode_options( Imagick $image, EncodeOptions $options ): void { // Named here because WordPress before 6.5 has no extension on record for AVIF. $formats = array( 'image/jpeg' => 'JPEG', 'image/png' => 'PNG', 'image/gif' => 'GIF', 'image/webp' => 'WEBP', 'image/avif' => 'AVIF', ); $image->setImageFormat( $formats[ $options->mime ] ); switch ( $options->mime ) { case 'image/jpeg': $image->setImageCompression( Imagick::COMPRESSION_JPEG ); $image->setImageCompressionQuality( $options->quality ); $image->setInterlaceScheme( Imagick::INTERLACE_PLANE ); if ( $options->quality < 90 ) { $image->setSamplingFactors( array( '2x2', '1x1', '1x1' ) ); } break; case 'image/png': $image->setOption( 'png:compression-filter', '5' ); $image->setOption( 'png:compression-level', '9' ); $image->setOption( 'png:compression-strategy', '1' ); $image->setOption( 'png:exclude-chunk', 'all' ); break; case 'image/webp': $image->setOption( 'webp:method', '4' ); $image->setOption( 'webp:lossless', $options->lossless ? 'true' : 'false' ); $image->setImageCompressionQuality( $options->lossless ? 100 : $options->quality ); break; case 'image/avif': $image->setOption( 'heic:speed', '8' ); $image->setImageCompressionQuality( $options->quality ); break; } } }