[英]Detect EXIF Orientation and Rotate Image using ImageMagick
佳能數碼單反相機似乎以橫向方式保存照片,並使用exif::orientation
進行旋轉。
問題:如何使用 exif 方向數據使用 imagemagick 將圖像重新保存到預期的方向,以便不再需要以正確的方向顯示 exif 數據?
使用 ImageMagick 的convert
的自動定向選項來做到這一點。
convert your-image.jpg -auto-orient output.jpg
或者使用mogrify
就地做
mogrify -auto-orient your-image.jpg
PHP Imagick 方法是測試圖像方向並相應地旋轉/翻轉圖像:
function autorotate(Imagick $image)
{
switch ($image->getImageOrientation()) {
case Imagick::ORIENTATION_TOPLEFT:
break;
case Imagick::ORIENTATION_TOPRIGHT:
$image->flopImage();
break;
case Imagick::ORIENTATION_BOTTOMRIGHT:
$image->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_BOTTOMLEFT:
$image->flopImage();
$image->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_LEFTTOP:
$image->flopImage();
$image->rotateImage("#000", -90);
break;
case Imagick::ORIENTATION_RIGHTTOP:
$image->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_RIGHTBOTTOM:
$image->flopImage();
$image->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_LEFTBOTTOM:
$image->rotateImage("#000", -90);
break;
default: // Invalid orientation
break;
}
$image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
}
該函數可以這樣使用:
$img = new Imagick('/path/to/file');
autorotate($img);
$img->stripImage(); // if you want to get rid of all EXIF data
$img->writeImage();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.