From 25a7e835dddbe0afe93a71a0377e74f8a67621d8 Mon Sep 17 00:00:00 2001 From: Tobias Reich Date: Sat, 12 Apr 2014 14:15:21 +0200 Subject: [PATCH] Own function for "Set orientation based on EXIF data" > adjustFile --- php/modules/Photo.php | 215 +++++++++++++++++++++++------------------- 1 file changed, 118 insertions(+), 97 deletions(-) diff --git a/php/modules/Photo.php b/php/modules/Photo.php index acca31e..78ca2ed 100755 --- a/php/modules/Photo.php +++ b/php/modules/Photo.php @@ -14,6 +14,9 @@ class Photo extends Module { private $settings = null; private $photoIDs = null; + private $uploadsBig = null; + private $uploadsThumb = null; + public function __construct($database, $plugins, $settings, $photoIDs) { # Init vars @@ -22,6 +25,10 @@ class Photo extends Module { $this->settings = $settings; $this->photoIDs = $photoIDs; + # Set upload dirs + $this->uploadsBig = __DIR__ . '/../../uploads/big/'; + $this->uploadsThumb = __DIR__ . '/../../uploads/thumb/'; + return true; } @@ -70,7 +77,7 @@ class Photo extends Module { $extension = array_reverse(explode('.', $file['name'])); $extension = $extension[0]; $photo_name = md5($id) . ".$extension"; - $path = __DIR__ . '/../../uploads/big/' . $photo_name; + $path = $this->uploadsBig . $photo_name; # Import if not uploaded via web if (!is_uploaded_file($tmp_name)) { @@ -90,94 +97,7 @@ class Photo extends Module { # Set orientation based on EXIF data if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&$info['orientation']!==''&&isset($info['width'])&&isset($info['height'])) { - - if(extension_loaded('imagick')) { - - $rotateImage = 0; - - switch ($info['orientation']) { - - case 3: - $rotateImage = 180; - $imageOrientation = 1; - break; - - case 6: - $rotateImage = 90; - $imageOrientation = 1; - break; - - case 8: - $rotateImage = 270; - $imageOrientation = 1; - break; - - } - - if ($rotateImage) { - $image = new Imagick(); - $image->readImage(__DIR__ . '/../../uploads/big/' . $photo_name); - $image->rotateImage(new ImagickPixel(), $rotateImage); - $image->setImageOrientation($imageOrientation); - $image->writeImage(__DIR__ . '/../../uploads/big/' . $photo_name); - $image->clear(); - $image->destroy(); - } - - } else { - - $newWidth = $info['width']; - $newHeight = $info['height']; - - $sourceImg = imagecreatefromjpeg(__DIR__ . "/../../uploads/big/$photo_name"); - - switch ($info['orientation']) { - - case 2: - # mirror - # not yet implemented - break; - - case 3: - $sourceImg = imagerotate($sourceImg, -180, 0); - break; - - case 4: - # rotate 180 and mirror - # not yet implemented - break; - - case 5: - # rotate 90 and mirror - # not yet implemented - break; - - case 6: - $sourceImg = imagerotate($sourceImg, -90, 0); - $newWidth = $info['height']; - $newHeight = $info['width']; - break; - - case 7: - # rotate -90 and mirror - # not yet implemented - break; - - case 8: - $sourceImg = imagerotate($sourceImg, 90, 0); - $newWidth = $info['height']; - $newHeight = $info['width']; - break; - - } - - $newSourceImg = imagecreatetruecolor($newWidth, $newHeight); - - imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight); - imagejpeg($newSourceImg, __DIR__ . '/../../uploads/big/' . $photo_name, 100); - - } - + if (!$this->adjustFile($path, $info)) return false; } # Create Thumb @@ -221,15 +141,15 @@ class Photo extends Module { private function createThumb($url, $filename, $width = 200, $height = 200) { - if (!isset($this->settings)) return false; + if (!isset($this->settings, $url, $filename)) return false; # Call plugins $this->plugins(__METHOD__, 0, func_get_args()); $info = getimagesize($url); $photoName = explode(".", $filename); - $newUrl = __DIR__ . '/../../uploads/thumb/' . $photoName[0] . '.jpeg'; - $newUrl2x = __DIR__ . '/../../uploads/thumb/' . $photoName[0] . '@2x.jpeg'; + $newUrl = $this->uploadsThumb . $photoName[0] . '.jpeg'; + $newUrl2x = $this->uploadsThumb . $photoName[0] . '@2x.jpeg'; # create thumbnails with Imagick if(extension_loaded('imagick')) { @@ -301,6 +221,107 @@ class Photo extends Module { } + private function adjustFile($path, $info) { + + if (!isset($path, $info)) return false; + + # Call plugins + $this->plugins(__METHOD__, 0, func_get_args()); + + if (extension_loaded('imagick')) { + + $rotateImage = 0; + + switch ($info['orientation']) { + + case 3: + $rotateImage = 180; + $imageOrientation = 1; + break; + + case 6: + $rotateImage = 90; + $imageOrientation = 1; + break; + + case 8: + $rotateImage = 270; + $imageOrientation = 1; + break; + + } + + if ($rotateImage) { + $image = new Imagick(); + $image->readImage($path); + $image->rotateImage(new ImagickPixel(), $rotateImage); + $image->setImageOrientation($imageOrientation); + $image->writeImage($path); + $image->clear(); + $image->destroy(); + } + + } else { + + $newWidth = $info['width']; + $newHeight = $info['height']; + + $sourceImg = imagecreatefromjpeg($path); + + switch ($info['orientation']) { + + case 2: + # mirror + # not yet implemented + break; + + case 3: + $sourceImg = imagerotate($sourceImg, -180, 0); + break; + + case 4: + # rotate 180 and mirror + # not yet implemented + break; + + case 5: + # rotate 90 and mirror + # not yet implemented + break; + + case 6: + $sourceImg = imagerotate($sourceImg, -90, 0); + $newWidth = $info['height']; + $newHeight = $info['width']; + break; + + case 7: + # rotate -90 and mirror + # not yet implemented + break; + + case 8: + $sourceImg = imagerotate($sourceImg, 90, 0); + $newWidth = $info['height']; + $newHeight = $info['width']; + break; + + } + + $newSourceImg = imagecreatetruecolor($newWidth, $newHeight); + + imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight); + imagejpeg($newSourceImg, $path, 100); + + } + + # Call plugins + $this->plugins(__METHOD__, 1, func_get_args()); + + return true; + + } + public function get($albumID) { if (!isset($this->database, $this->photoIDs)) return false; @@ -454,10 +475,10 @@ class Photo extends Module { # Set headers header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"$photo->title.$extension[0]\""); - header("Content-Length: " . filesize(__DIR__ . '/../../uploads/big/' . $photo->url)); + header("Content-Length: " . filesize($this->uploadsBig . $photo->url)); # Send file - readfile(__DIR__ . '/../../uploads/big/' . $photo->url); + readfile($this->uploadsBig . $photo->url); # Call plugins $this->plugins(__METHOD__, 1, func_get_args()); @@ -653,9 +674,9 @@ class Photo extends Module { $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1]; # Delete files - if (!unlink(__DIR__ . '/../../uploads/big/' . $photo->url)) return false; - if (!unlink(__DIR__ . '/../../uploads/thumb/' . $photo->thumbUrl)) return false; - if (!unlink(__DIR__ . '/../../uploads/thumb/' . $thumbUrl2x)) return false; + if (!unlink($this->uploadsBig . $photo->url)) return false; + if (!unlink($this->uploadsThumb . $photo->thumbUrl)) return false; + if (!unlink($this->uploadsThumb . $thumbUrl2x)) return false; # Delete db entry $delete = $this->database->query("DELETE FROM lychee_photos WHERE id = '$photo->id';");