From 28129a9e8fbf8a0298585b1344b047a8eb2c68f4 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Fri, 19 Sep 2014 10:45:56 +0200 Subject: thumbnails: Handle EXIF orientation Signed-off-by: Florian Pritz --- application/models/mfile.php | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/application/models/mfile.php b/application/models/mfile.php index 1f5409ec3..c4d2c6e68 100644 --- a/application/models/mfile.php +++ b/application/models/mfile.php @@ -184,6 +184,48 @@ class Mfile extends CI_Model { $target_width, $target_height ); + /* + * Fix orientation according to exif tag + */ + try { + $exif = exif_read_data($source_path); + } catch (ErrorException $e) { + } + + if (isset($exif['Orientation'])) { + $mirror = false; + $deg = 0; + + switch ($exif['Orientation']) { + case 2: + $mirror = true; + break; + case 3: + $deg = 180; + break; + case 4: + $deg = 180; + $mirror = true; + break; + case 5: + $deg = 270; + $mirror = true; + break; + case 6: + $deg = 270; + break; + case 7: + $deg = 90; + $mirror = true; + break; + case 8: + $deg = 90; + break; + } + if ($deg) $thumb = imagerotate($thumb, $deg, 0); + if ($mirror) $thumb = $this->_mirrorImage($thumb); + } + ob_start(); switch ($target_type) { case IMAGETYPE_GIF: @@ -211,6 +253,27 @@ class Mfile extends CI_Model { return $result; } + function _mirrorImage($imgsrc) + { + $width = imagesx($imgsrc); + $height = imagesy($imgsrc); + + $src_x = $width -1; + $src_y = 0; + $src_width = -$width; + $src_height = $height; + + $imgdest = imagecreatetruecolor($width, $height); + imagealphablending($imgdest,false); + imagesavealpha($imgdest,true); + + if (imagecopyresampled($imgdest, $imgsrc, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height)) { + return $imgdest; + } + + return $imgsrc; + } + // Add a hash to the DB function add_file($hash, $id, $filename) { -- cgit v1.2.3-24-g4f1b