Skip to content

Commit fd9cc21

Browse files
committed
Merge pull request #32 from saschanos/master
use orientation data from exif when creating new image
2 parents cd5ba51 + 4180bc1 commit fd9cc21

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/ImageResize.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __construct($filename)
8080
break;
8181

8282
case IMAGETYPE_JPEG:
83-
$this->source_image = imagecreatefromjpeg($filename);
83+
$this->source_image = $this->imageCreateJpegfromExif($filename);
8484
break;
8585

8686
case IMAGETYPE_PNG:
@@ -94,6 +94,32 @@ public function __construct($filename)
9494

9595
return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
9696
}
97+
98+
// http://stackoverflow.com/a/28819866
99+
public function imageCreateJpegfromExif($filename){
100+
$img = imagecreatefromjpeg($filename);
101+
$exif = exif_read_data($filename);
102+
103+
if (!$exif || !isset($exif['Orientation'])){
104+
return $img;
105+
}
106+
107+
$orientation = $exif['Orientation'];
108+
109+
if ($orientation === 6 || $orientation === 5){
110+
$img = imagerotate($img, 270, null);
111+
} else if ($orientation === 3 || $orientation === 4){
112+
$img = imagerotate($img, 180, null);
113+
} else if ($orientation === 8 || $orientation === 7){
114+
$img = imagerotate($img, 90, null);
115+
}
116+
117+
if ($orientation === 5 || $orientation === 4 || $orientation === 7){
118+
imageflip($img, IMG_FLIP_HORIZONTAL);
119+
}
120+
121+
return $img;
122+
}
97123

98124
/**
99125
* Saves new image

0 commit comments

Comments
 (0)