I was using this call to imageMagick in PHP to merge multiple images horizontally
$combined_image = $images2merge->appendImages(false);
And it works great when I upload the images from my laptop. But when I go to the same page with my android phone browser and use the camera to take the pics to upload, this function ends up merging them vertically.
So I tried to swap it out with this function
$combined_image = $images2merge->montageImage($draw, '6×6+0+0', '+1+1', Imagick::MONTAGEMODE_UNFRAME, 0);
And that does now merge them horizontally when I use my phone, but it rotates them 90 degrees counterclockwise. Again that's different from what happens when I upload from my laptop, in which case it works as expected. It's hard to fix something that behaves inconsistently.
Any suggestions on how to fix this would be greatly appreciated!
After taking photos from any smartphone, please check the orientation value and rotate the image accordingly before further image processing.
So you can use the following autoRotateImage function to handle the auto-rotation:
<?php
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;
}
// Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image!
$image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
}
?>
For example:
<?php
$image = new Imagick('my-image-file.jpg');
autoRotateImage($image);
// - Do other stuff to the image here -
$image->writeImage('result-image.jpg');
?>