中級プログラマの自宅でPHP ブログ

自宅用ポータルシステム「MyHome Portal」をオープンソースで公開。MyHome Portalの説明とエンハンス情報。

EXIF縦位置撮影情報画像の回転補正

デジカメで撮影して、EXIFに縦位置情報を持っている画像の回転(正しく縦位置に補正する)処理で、MyHome Portal Ver.2.47では左に倒れている画像しか想定しておらず(私の場合、縦位置撮影は、ほとんど右手を下にして撮るので、こうなってしまった)、右回転していました。

Ver.2.48で、その不具合を修正したのですが、IrfanViewでは、自動回転という便利な機能があって、EXIF情報に基づいて自動回転補正してくれるのです。

実際の回転処理は、以下の修正を行っただけです。

i_view32 x.jpg /jpg_rotate=(3,1,0,1,0,0,0,0) /silent /convert=

↓↓↓↓↓

i_view32 x.jpg /jpg_rotate=(6,1,0,1,0,0,0,0) /silent /convert=

ただ、縦位置撮影していない画像にもこの処理をするのは無駄なので、縦位置撮影した画像だけ、つまりEXIFに縦位置情報を持っているものだけを処理しています。

Ver.2.47では、IrfanViewで縮小した画像(EXIF補正される)と元画像の縦横比率を比較して、対象を絞っていました。

しかし、PHPにはEXIF情報を読み取る関数が用意されていたのでした。exif_read_data()です。Ver.2.48では、この関数を使ってEXIF情報に縦位置情報を持っているものを選別しています。'Orientation'という項目がその情報です。'Orientation'が1で無いもの全てを対象にしても良いのかもしれませんが、通常ありうる6と8のみを対象にしています。

【補足:EXIF:Orientation】

  1 => 'nothing',

  2 => 'horizontal flip',

  3 => '180 rotate left',

  4 => 'vertical flip',

  5 => 'vertical flip + 90 rotate right',

  6 => '90 rotate right',

  7 => 'horizontal flip + 90 rotate right',

  8 => '90 rotate left'

http://www.nikep.net/srdpty/index.php?/archives/24-unknown.htmlから、いただいた画像。↓↓↓↓↓

exiforientation.jpg

【補足:IrfanViewコマンドラインでのロスレス回転オプションの説明】

Example for /jpg_rotate=(options):

  options = all 8 options from the JPG lossless dialog:

  transformation, optimize, EXIF date, current date, set DPI, DPI value, marker option, custom markers

  Note: this option will overwrite the original file(s)!

  Values:

  Transformation   : None (0), Vertical (1) ... Auto rotate (6)

  Optimize         : 0 or 1

  Set EXIF date    : 0 or 1

  Keep current date: 0 or 1

  Set DPI          : 0 or 1

  DPI value        : number

  Marker option    : Keep all (0), Clean all (1), Custom (2)

  Custom markers values (can be combined (add values)):

    Keep Comment 1

    Keep EXIF    2

    Keep IPTC    4

    Keep others  8

  i_view32.exe c:\test.jpg /jpg_rotate=(6,1,1,0,1,300,0,0)

  => Auto rotate, optimize, set EXIF date as file date, set DPI to 300, keep all markers

  i_view32.exe c:\test.jpg /jpg_rotate=(6,1,1,0,0,0,2,6)

  => Auto rotate, optimize, set EXIF date as file date, keep EXIF and IPTC markers

  i_view32.exe c:\test.jpg /jpg_rotate=(3,1,0,1,0,0,1,0)

  => Rotate 90, optimize, use current file date, clean all markers

  i_view32.exe c:\images\*.jpg /jpg_rotate=(6,1,1,0,0,0,0,0)

  => For all JPGs: Auto rotate, optimize, set EXIF date as file date, keep all markers