我正在使用PHP中的Imagick库来使用imagemagik进行一些图像操作。用户上传一张照片,我调整它的大小,然后使用compositeImage函数在上面放置一个透明的PNG层
$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;
所以奇怪的是,对于一些照片,当覆盖物放在上面时,它会旋转90度。我认为这与不同的文件格式有关,在合成图像之前,是否有一种公认的方法来规范图像以防止这种情况发生?
所以问题出在exif方向值上。这里有一些关于这个主题的好信息:http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/.
基本上,在合成图像之前,你必须解决图像的方向。PHP文档网站上的评论中有一个很好的功能:http://www.php.net/manual/en/imagick.getimageorientation.php
// Note: $image is an Imagick object, not a 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;
}
// 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);
}