Skip to content

Commit

Permalink
Merge pull request #32 from saschanos/master
Browse files Browse the repository at this point in the history
use orientation data from exif when creating new image
  • Loading branch information
adityapatadia committed Jan 6, 2016
2 parents cd5ba51 + 4180bc1 commit fd9cc21
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/ImageResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __construct($filename)
break;

case IMAGETYPE_JPEG:
$this->source_image = imagecreatefromjpeg($filename);
$this->source_image = $this->imageCreateJpegfromExif($filename);
break;

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

return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
}

// http://stackoverflow.com/a/28819866
public function imageCreateJpegfromExif($filename){
$img = imagecreatefromjpeg($filename);
$exif = exif_read_data($filename);

if (!$exif || !isset($exif['Orientation'])){
return $img;
}

$orientation = $exif['Orientation'];

if ($orientation === 6 || $orientation === 5){
$img = imagerotate($img, 270, null);
} else if ($orientation === 3 || $orientation === 4){
$img = imagerotate($img, 180, null);
} else if ($orientation === 8 || $orientation === 7){
$img = imagerotate($img, 90, null);
}

if ($orientation === 5 || $orientation === 4 || $orientation === 7){
imageflip($img, IMG_FLIP_HORIZONTAL);
}

return $img;
}

/**
* Saves new image
Expand Down

0 comments on commit fd9cc21

Please sign in to comment.