Detect EXIF Orientation and Rotate Image using ImageMagick

前端 未结 2 636

Canon DSLRs appear to save photos in landscape orientation and uses exif::orientation to do the rotation.

Question: How can imagemagick

相关标签:
2条回答
  • 2020-12-02 09:22

    Use the auto-orient option of ImageMagick's convert to do this.

    convert your-image.jpg -auto-orient output.jpg
    

    Or use mogrifyto do it in place

    mogrify -auto-orient your-image.jpg
    
    0 讨论(0)
  • 2020-12-02 09:25

    The PHP Imagick way would be to test the image orientation and rotate/flip the image accordingly:

    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);
    }
    

    The function might be used like this:

    $img = new Imagick('/path/to/file');
    autorotate($img);
    $img->stripImage(); // if you want to get rid of all EXIF data
    $img->writeImage();
    
    0 讨论(0)
提交回复
热议问题