From bda837fcd316a4f8ca90e631c620b8cc93c759a7 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Fri, 29 Apr 2022 17:50:33 +0100 Subject: [PATCH] mason/.perl-lib/TrivGal.pm: Handle EXIF `Orientation'. Apparently Imlib2 doesn't do this for me. :-( OpenCamera hard-rotates its images rather than using EXIF metadata, so I didn't notice before, but my Nikon cameras don't do this. --- mason/.perl-lib/TrivGal.pm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mason/.perl-lib/TrivGal.pm b/mason/.perl-lib/TrivGal.pm index 39c6913..4a41612 100644 --- a/mason/.perl-lib/TrivGal.pm +++ b/mason/.perl-lib/TrivGal.pm @@ -30,6 +30,7 @@ use autodie qw{:all}; use Errno; use Exporter qw{import}; use File::stat; +use Image::ExifTool qw{}; use Image::Imlib2; use User::pwent; use POSIX; @@ -185,6 +186,16 @@ sub clean_temp_files () { ###-------------------------------------------------------------------------- ### Scaled images. +my %ORIENT = + (1 => [0, 0], + 2 => [0, 1], + 3 => [2, 0], + 4 => [2, 1], + 5 => [3, 1], + 6 => [1, 0], + 7 => [1, 1], + 8 => [3, 0]); + package TrivGal::Image { use File::Path qw{make_path}; use File::stat; @@ -216,7 +227,15 @@ package TrivGal::Image { my $img = $me->{img}; unless (defined $img) { my $imgpath = "$IMGROOT/$path"; + my $exif = new Image::ExifTool; + $exif->ExtractInfo($imgpath); + my $orient = $exif->GetValue("Orientation", "ValueConv"); $img = $me->{img} = Image::Imlib2->load($imgpath); + if (defined $orient) { + my ($rot, $flip) = @{$ORIENT{$orient}}; + if ($rot) { $img->image_orientate($rot); } + if ($flip) { $img->flip_horizontal($rot); } + } } my ($wd, $ht) = ($img->width, $img->height); -- 2.11.0