aboutsummaryrefslogtreecommitdiffstats
path: root/lib/image.php
diff options
context:
space:
mode:
authorThomas Tanghus <thomas@tanghus.net>2012-01-05 17:42:40 +0100
committerThomas Tanghus <thomas@tanghus.net>2012-01-05 17:42:40 +0100
commit2f86a6203adc82e1221873ed72a0ac60c340d65b (patch)
treebf49449c136ec42ccab54ba42136bac07c311dc0 /lib/image.php
parentd80b477ab55c459cc80cf091c80f81082b7d78f9 (diff)
downloadnextcloud-server-2f86a6203adc82e1221873ed72a0ac60c340d65b.tar.gz
nextcloud-server-2f86a6203adc82e1221873ed72a0ac60c340d65b.zip
Added fixOrientation method which rotates the image based on EXIF data.
Diffstat (limited to 'lib/image.php')
-rw-r--r--lib/image.php84
1 files changed, 82 insertions, 2 deletions
diff --git a/lib/image.php b/lib/image.php
index f120328b0e4..45b6ad3918d 100644
--- a/lib/image.php
+++ b/lib/image.php
@@ -42,8 +42,7 @@ function ellipsis($str, $maxlen) {
}
/**
- * Class for image manipulation
- * Ideas: imagerotate, chunk_split(base64_encode())
+ * Class for basic image manipulation
*
*/
class OC_Image {
@@ -201,6 +200,87 @@ class OC_Image {
}
/**
+ * @brief Fixes orientation based on EXIF data.
+ * @returns bool.
+ */
+ public function fixOrientation() {
+ if(!is_resource(self::$resource)) {
+ OC_Log::write('core','OC_Image::fixOrientation() No image loaded.', OC_Log::DEBUG);
+ return false;
+ }
+ if(is_null(self::$filepath) || !is_readable(self::$filepath)) {
+ OC_Log::write('core','OC_Image::fixOrientation() No readable file path set.', OC_Log::DEBUG);
+ return false;
+ }
+ $exif = exif_read_data(self::$filepath, 'IFD0');
+ if(!$exif) {
+ return false;
+ }
+ if(!isset($exif['Orientation'])) {
+ return true; // Nothing to fix
+ }
+ $o = $exif['Orientation'];
+ OC_Log::write('core','OC_Image::fixOrientation() Orientation: '.$o, OC_Log::DEBUG);
+ $rotate = 0;
+ $flip = false;
+ switch($o) {
+ case 1:
+ $rotate = 0;
+ $flip = false;
+ break;
+ case 2: // Not tested
+ $rotate = 0;
+ $flip = true;
+ break;
+ case 3:
+ $rotate = 180;
+ $flip = false;
+ break;
+ case 4: // Not tested
+ $rotate = 180;
+ $flip = true;
+ break;
+ case 5: // Not tested
+ $rotate = 90;
+ $flip = true;
+ break;
+ case 6:
+ //$rotate = 90;
+ $rotate = 270;
+ $flip = false;
+ break;
+ case 7: // Not tested
+ $rotate = 270;
+ $flip = true;
+ break;
+ case 8:
+ $rotate = 270;
+ $flip = false;
+ break;
+ }
+ if($rotate) {
+ $res = imagerotate(self::$resource, $rotate, -1);
+ if($res) {
+ if(imagealphablending($res, true)) {
+ if(imagesavealpha($res, true)) {
+ self::$resource = $res;
+ return true;
+ } else {
+ OC_Log::write('core','OC_Image::fixOrientation() Error during alphasaving.', OC_Log::DEBUG);
+ return false;
+ }
+ } else {
+ OC_Log::write('core','OC_Image::fixOrientation() Error during alphablending.', OC_Log::DEBUG);
+ return false;
+ }
+ } else {
+ OC_Log::write('core','OC_Image::fixOrientation() Error during oriention fixing.', OC_Log::DEBUG);
+ return false;
+ }
+ }
+ }
+
+ /**
* @brief Loads an image from a local file, a base64 encoded string or a resource created by an imagecreate* function.
* @param $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function.
* If a resource is passed it is the job of the caller to destroy it using imagedestroy($var)