Auto-orient image during processing (#1242)

* Auto-orient image during processing

* Detect if thumb was rotated 90 degrees and switch dimentions

* Consistently auto-rotate images

* fix incorrect path for the original photo dimensions reading

* Swap the dimensions if needed and call the `wand.ThumbnailImage()` unconditionally

* revert unnecessary line split

---------

Co-authored-by: Konstantin Koval
This commit is contained in:
Kostiantyn
2025-07-11 13:21:28 +03:00
committed by GitHub
parent 06d2e05ae4
commit 124b6d34a9
3 changed files with 33 additions and 1 deletions

View File

@@ -81,6 +81,15 @@ func EncodeThumbnail(db *gorm.DB, inputPath string, outputPath string) (Dimensio
return Dimension{}, fmt.Errorf("can't generate thumbnail of file %q: %w", inputPath, err)
}
w, h, err = executable_worker.Magick.IdentifyDimension(outputPath)
if err != nil {
return Dimension{}, fmt.Errorf("can't generate thumbnail of file %q: %w", inputPath, err)
}
thumbnail = Dimension{
Width: int(w),
Height: int(h),
}
return thumbnail, nil
}

View File

@@ -44,6 +44,14 @@ func (cli *MagickWand) EncodeJpeg(inputPath string, outputPath string, jpegQuali
return fmt.Errorf("ImagickWand read %q error: %w", inputPath, err)
}
if err := wand.AutoOrientImage(); err != nil {
return fmt.Errorf("ImagickWand auto-orient %q error: %w", inputPath, err)
}
// Reset EXIF orientation to 1 (top-left) since image is now properly oriented
if err := wand.SetImageOrientation(imagick.ORIENTATION_TOP_LEFT); err != nil {
return fmt.Errorf("ImagickWand set orientation for %q error: %w", inputPath, err)
}
if err := wand.SetFormat("JPEG"); err != nil {
return fmt.Errorf("ImagickWand set JPEG format for %q error: %w", inputPath, err)
}
@@ -71,6 +79,21 @@ func (cli *MagickWand) GenerateThumbnail(inputPath string, outputPath string, wi
return fmt.Errorf("ImagickWand read %q error: %w", inputPath, err)
}
originalWidth := wand.GetImageWidth()
originalHeight := wand.GetImageHeight()
if err := wand.AutoOrientImage(); err != nil {
return fmt.Errorf("ImagickWand auto-orient %q error: %w", inputPath, err)
}
// Reset EXIF orientation to 1 (top-left) since image is now properly oriented
if err := wand.SetImageOrientation(imagick.ORIENTATION_TOP_LEFT); err != nil {
return fmt.Errorf("ImagickWand set orientation for %q error: %w", inputPath, err)
}
// If the original image is rotated by 90 degrees, swap width and height for thumbnail generation
if originalWidth != wand.GetImageWidth() && originalHeight != wand.GetImageHeight() {
width, height = height, width
}
if err := wand.ThumbnailImage(width, height); err != nil {
return fmt.Errorf("ImagickWand generate thumbnail for %q error: %w", inputPath, err)
}

View File

@@ -86,7 +86,7 @@ func (t ProcessPhotoTask) ProcessMedia(ctx scanner_task.TaskContext, mediaData *
if origURL == nil {
// Make sure photo dimensions is set
photoDimensions, err := media_encoding.GetPhotoDimensions(baseImagePath)
photoDimensions, err := media_encoding.GetPhotoDimensions(photo.Path)
if err != nil {
return []*models.MediaURL{}, err
}