
- 富山ホームページ/システム開発 グリークグリークトップ
- ブログ
- PHP
- iphoneで撮った写真の方向をphpで補正する方法 ブログ
iphoneで撮った写真の方向をphpで補正する方法
皆さん本日もお疲れ様です。
今回はiPhoneで撮影した写真のphpを使った修整方法をご紹介いたします。
iPhoneでは写真を撮った場合、横で撮影したものも、縦で撮影したものもすべて横長の画像として保存されます。
↑例えば横向きで撮影した写真はこんな感じに
ちなみにこちらはお世話になりましたクールストリートモータース様より頂きました(^^)
ではどうしてiPhone上ではちゃんと表示されるのでしょうか?
それは画像の中に撮影された画像の向き「Exif情報」が保存されているためです。
Exif情報とは?
EXIF (Exchangeable Image File Format)とは、撮影日時、画像の縦横サイズ、
シャッタースピードなどのさまざまな撮影情報が、画像に埋め込まれたメタ情報のことです。
iPhoneの場合はHD上に保存された画像の向きを統一し、Exif情報を読み取り向きを補正しております。
phpで画像の向きを修正
これから説明するコードをコピーするだけで上の画像がしっかり修正されます。
phpではexif_read_data関数を使用し、Orientationの値から向きを取得いたします。
// 調べたい画像のパス $filename = "./photo.jpeg"; $exif_data = exif_read_data($filename); // 向きを表示 print $exif_data['Orientation'];
$exif_data[‘Orientation’]の値は下記を参照下さい。
Orientation = 1 回転無し
Orientation = 2 左右反転
Orientation = 3 180°回転
Orientation = 4 上下反転
Orientation = 5 時計回りに90°回転した後、左右反転
Orientation = 6 時計回りに90°回転
Orientation = 7 反時計回りに90°回転した後、左右反転
Orientation = 8 反時計回りに90°回転
取得した向きから画像を補正いたします。
今回、画像の回転にはimagerotate関数、反転にはimagesetpixel関数を使用いたします。
// 画像の左右反転 function image_flop($image){ // 画像の幅を取得 $w = imagesx($image); // 画像の高さを取得 $h = imagesy($image); // 変換後の画像の生成(元の画像と同じサイズ) $destImage = @imagecreatetruecolor($w,$h); // 逆側から色を取得 for($i=($w-1);$i>=0;$i--){ for($j=0;$j<$h;$j++){ $color_index = imagecolorat($image,$i,$j); $colors = imagecolorsforindex($image,$color_index); imagesetpixel($destImage,abs($i-$w+1),$j,imagecolorallocate($destImage,$colors["red"],$colors["green"],$colors["blue"])); } } return $destImage; } // 上下反転 function image_flip($image){ // 画像の幅を取得 $w = imagesx($image); // 画像の高さを取得 $h = imagesy($image); // 変換後の画像の生成(元の画像と同じサイズ) $destImage = @imagecreatetruecolor($w,$h); // 逆側から色を取得 for($i=0;$i<$w;$i++){ for($j=($h-1);$j>=0;$j--){ $color_index = imagecolorat($image,$i,$j); $colors = imagecolorsforindex($image,$color_index); imagesetpixel($destImage,$i,abs($j-$h+1),imagecolorallocate($destImage,$colors["red"],$colors["green"],$colors["blue"])); } } return $destImage; } // 画像を回転 function image_rotate($image, $angle, $bgd_color){ return imagerotate($image, $angle, $bgd_color, 0); }
画像の回転・上下反転・左右反転の関数を準備しましたので、
あとはOrientationの値を読み取り、画像の補正します。
// 画像の方向を正す function orientationFixedImage($output,$input){ $image = ImageCreateFromJPEG($input); $exif_datas = @exif_read_data($input); if(isset($exif_datas['Orientation'])){ $orientation = $exif_datas['Orientation']; if($image){ // 未定義 if($orientation == 0){ // 通常 }else if($orientation == 1){ // 左右反転 }else if($orientation == 2){ image_flop($image); // 180°回転 }else if($orientation == 3){ image_rotate($image,180, 0); // 上下反転 }else if($orientation == 4){ image_Flip($image); // 反時計回りに90°回転 上下反転 }else if($orientation == 5){ image_rotate($image,270, 0); image_flip($image); // 時計回りに90°回転 }else if($orientation == 6){ image_rotate($image,90, 0); // 時計回りに90°回転 上下反転 }else if($orientation == 7){ image_rotate($image,90, 0); image_flip($image); // 反時計回りに90°回転 }else if($orientation == 8){ image_rotate($image,270, 0); } } } // 画像の書き出し ImageJPEG($image ,$output); return false; }
下記のように呼び出せば簡単に画像修正できます。
// iphoneで撮影した元画像 $input = "iphone_before.jpeg"; // 書き出し先の画像ファイル $output = "iphone_after.jpeg"; // 画像の補正 orientationFixedImage($output,$input);
一度試してみてはいかがでしょうか?