summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClément Guillemain <clement.guillemain@gmail.com>2015-03-12 18:50:09 +0100
committerClément Guillemain <clement.guillemain@gmail.com>2015-03-12 18:50:09 +0100
commit5d4d5e80c6c42bf26d232536ffb4205f4326d08e (patch)
tree3049c170788633034a9f7c1529963d693c32fe42
parent20b228504d8a475f5361e8d73f9a477d181492ff (diff)
parent4ca597d8f98c8bf8f1a7fadc4088d5680e6f95cd (diff)
downloadImageWorkshop-5d4d5e80c6c42bf26d232536ffb4205f4326d08e.zip
ImageWorkshop-5d4d5e80c6c42bf26d232536ffb4205f4326d08e.tar.gz
ImageWorkshop-5d4d5e80c6c42bf26d232536ffb4205f4326d08e.tar.bz2
Merge pull request #63 from jdecool/exif-data
Fix image orientation based on EXIF data
-rw-r--r--.gitignore2
-rw-r--r--composer.json9
-rw-r--r--src/PHPImageWorkshop/Core/ImageWorkshopLayer.php55
-rw-r--r--src/PHPImageWorkshop/Exif/ExifOrientations.php19
-rw-r--r--src/PHPImageWorkshop/ImageWorkshop.php17
5 files changed, 95 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d8a7996
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+composer.lock
+vendor/
diff --git a/composer.json b/composer.json
index 902ccec..0aed6d1 100644
--- a/composer.json
+++ b/composer.json
@@ -13,9 +13,14 @@
}
],
"require": {
- "php": ">=5.3.0"
+ "php": ">=5.3.0",
+ "ext-gd": "*",
+ "ext-exif": "*"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "~4.5"
},
"autoload": {
"psr-0": { "PHPImageWorkshop": "src" }
}
-} \ No newline at end of file
+}
diff --git a/src/PHPImageWorkshop/Core/ImageWorkshopLayer.php b/src/PHPImageWorkshop/Core/ImageWorkshopLayer.php
index a303bd5..185a0e3 100644
--- a/src/PHPImageWorkshop/Core/ImageWorkshopLayer.php
+++ b/src/PHPImageWorkshop/Core/ImageWorkshopLayer.php
@@ -2,6 +2,7 @@
namespace PHPImageWorkshop\Core;
+use PHPImageWorkshop\Exif\ExifOrientations;
use PHPImageWorkshop\ImageWorkshop as ImageWorkshop;
use PHPImageWorkshop\Core\ImageWorkshopLib as ImageWorkshopLib;
use PHPImageWorkshop\Core\Exception\ImageWorkshopLayerException as ImageWorkshopLayerException;
@@ -84,6 +85,13 @@ class ImageWorkshopLayer
* Background Image
*/
protected $image;
+
+ /**
+ * @var array
+ *
+ * Exif data
+ */
+ protected $exif;
/**
* @var string
@@ -132,7 +140,7 @@ class ImageWorkshopLayer
*
* @param \resource $image
*/
- public function __construct($image)
+ public function __construct($image, array $exif = array())
{
if (!extension_loaded('gd')) {
throw new ImageWorkshopLayerException('PHPImageWorkshop requires the GD extension to be loaded.', static::ERROR_GD_NOT_INSTALLED);
@@ -145,6 +153,7 @@ class ImageWorkshopLayer
$this->width = imagesx($image);
$this->height = imagesy($image);
$this->image = $image;
+ $this->exif = $exif;
$this->layers = $this->layerLevels = $this->layerPositions = array();
$this->clearStack();
}
@@ -1858,6 +1867,50 @@ class ImageWorkshopLayer
unset($this->image);
$this->image = $virginLayoutImage;
}
+
+ /**
+ * Fix image orientation based on exif data
+ */
+ public function fixOrientation()
+ {
+ if (!isset($this->exif['Orientation']) || 0 == $this->exif['Orientation']) {
+ return;
+ }
+
+ switch ($this->exif['Orientation']) {
+ case ExifOrientations::TOP_RIGHT:
+ $this->flip('horizontal');
+ break;
+
+ case ExifOrientations::BOTTOM_RIGHT:
+ $this->rotate(180);
+ break;
+
+ case ExifOrientations::BOTTOM_LEFT:
+ $this->flip('vertical');
+ break;
+
+ case ExifOrientations::LEFT_TOP:
+ $this->rotate(-90);
+ $this->flip('vertical');
+ break;
+
+ case ExifOrientations::RIGHT_TOP:
+ $this->rotate(90);
+ break;
+
+ case ExifOrientations::RIGHT_BOTTOM:
+ $this->rotate(90);
+ $this->flip('horizontal');
+ break;
+
+ case ExifOrientations::LEFT_BOTTOM:
+ $this->rotate(-90);
+ break;
+ }
+
+ $this->exif['Orientation'] = ExifOrientations::TOP_LEFT;
+ }
// Deprecated, don't use anymore
// =========================================================
diff --git a/src/PHPImageWorkshop/Exif/ExifOrientations.php b/src/PHPImageWorkshop/Exif/ExifOrientations.php
new file mode 100644
index 0000000..94fa5df
--- /dev/null
+++ b/src/PHPImageWorkshop/Exif/ExifOrientations.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace PHPImageWorkshop\Exif;
+
+/**
+ * Container for all EXIF orientations.
+ */
+final class ExifOrientations
+{
+ const UNDEFINED = 0;
+ const TOP_LEFT = 1;
+ const TOP_RIGHT = 2;
+ const BOTTOM_RIGHT = 3;
+ const BOTTOM_LEFT = 4;
+ const LEFT_TOP = 5;
+ const RIGHT_TOP = 6;
+ const RIGHT_BOTTOM = 7;
+ const LEFT_BOTTOM = 8;
+}
diff --git a/src/PHPImageWorkshop/ImageWorkshop.php b/src/PHPImageWorkshop/ImageWorkshop.php
index d45b74e..88330f7 100644
--- a/src/PHPImageWorkshop/ImageWorkshop.php
+++ b/src/PHPImageWorkshop/ImageWorkshop.php
@@ -49,10 +49,11 @@ class ImageWorkshop
* From an upload form, you can give the "tmp_name" path
*
* @param string $path
+ * @param bool $fixOrientation
*
* @return ImageWorkshopLayer
*/
- public static function initFromPath($path)
+ public static function initFromPath($path, $fixOrientation = false)
{
if (file_exists($path) && !is_dir($path)) {
@@ -68,10 +69,12 @@ class ImageWorkshop
}
$mimeContentType = $mimeContentType[1];
+ $exif = array();
switch ($mimeContentType) {
case 'jpeg':
$image = imageCreateFromJPEG($path);
+ $exif = read_exif_data($path);
break;
case 'gif':
@@ -86,8 +89,14 @@ class ImageWorkshop
throw new ImageWorkshopException('Not an image file (jpeg/png/gif) at "'.$path.'"', static::ERROR_NOT_AN_IMAGE_FILE);
break;
}
-
- return new ImageWorkshopLayer($image);
+
+ $layer = new ImageWorkshopLayer($image, $exif);
+
+ if ($fixOrientation) {
+ $layer->fixOrientation();
+ }
+
+ return $layer;
}
throw new ImageWorkshopException('No such file found at "'.$path.'"', static::ERROR_IMAGE_NOT_FOUND);
@@ -165,4 +174,4 @@ class ImageWorkshop
return new ImageWorkshopLayer($image);
}
-} \ No newline at end of file
+}