php - Top layer is rotated when compositing in ImageMagick -


i'm using imagick libray in php some image manipulation imagemagick. user uploads photo, resize it, , use compositeimage function place transparent png layer on it. code looks this:

$image = new imagick($imagepath); $overlay = new imagick("filters/photo-filter-flat.png");  $geo = $image->getimagegeometry();  if ($geo['height'] > $geo['width']) {     $image->scaleimage(0, 480); } else {     $image->scaleimage(320, 0); }  $image->compositeimage($overlay, imagick::composite_atop, 0, 0);  return $image; 

so strange thing is, photos overlay gets rotated 90 degrees when it's placed on top. i'm thinking has different file formats, there accepted way normalize images before composite them prevent this?

so turns out issue exif orientation value. there's information topic in general here: http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/.

basically have resolve image's orientation before composite it. there's function in comments on php documentation site: http://www.php.net/manual/en/imagick.getimageorientation.php

// note: $image imagick object, not filename! see example use below.      function autorotateimage($image) {      $orientation = $image->getimageorientation();       switch($orientation) {          case imagick::orientation_bottomright:              $image->rotateimage("#000", 180); // rotate 180 degrees          break;           case imagick::orientation_righttop:              $image->rotateimage("#000", 90); // rotate 90 degrees cw          break;           case imagick::orientation_leftbottom:              $image->rotateimage("#000", -90); // rotate 90 degrees ccw          break;      }       // it's auto-rotated, make sure exif data correct in case exif gets saved image!      $image->setimageorientation(imagick::orientation_topleft);  }  

Comments