wp-media / imagify-plugin

Speed up your website with lighter images without losing quality.
https://imagify.io
73 stars 27 forks source link

WebP generation of rotated JPG files results in wrong orientation #859

Open redbotHQ opened 8 months ago

redbotHQ commented 8 months ago

Describe the bug WebP generation of rotated JPG files results in wrong orientation.

To Reproduce

  1. Upload a JPG file with EXIF orientation data. The file below has orientation : 6 (Rotate 90 CW).

bird-box-portrait

Expected behavior The WebP file would be rotated as per the JPG equivalent.

Screenshots https://redbot.uk/imagify-bug-demo.html

Additional context On upload Wordpress rotates the JPG image via the maybe_exif_rotate() function, appending '-rotated' to the filename, before updating metadata orientation to 1.

Imagify generates the 'full' size WebP file from the backup path, but does not replicate the rotation of the file performed on the raw fullsize path by Wordpress.

Bug Fix The bug can be patched by adding the following code to the start of the optimize() function within file: /imagify/classes/Optimization/File.php

public function optimize( $args = [] ) {
    $exif_meta = wp_read_image_metadata( $this->path );
    if ( ! empty( $args['convert'] ) && 'webp' === $args['convert'] && ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) {
        $editor = wp_get_image_editor( $this->path );
        if ( ! is_wp_error( $editor ) ) {
            $rotated = $editor->maybe_exif_rotate();
            if ( true === $rotated ) {
                $editor->save( $this->path );
                // update metadata - orientation : 1 (not needed as removed for webp)
            }
        }
    }
    ...
}
krystoflp commented 3 months ago

I too am experiencing this, which means I either have to disable Imagify, or manually adjust each image to use the original path. I can confirm the patch suggested by @redbotHQ works too. It would be good if someone from the team could prioritise this for an update seeing as a fix is available. @Mai-Saad

saranshj4 commented 1 week ago

https://secure.helpscout.net/conversation/2757496519/522087?folderId=8160051