반응형
PHP
질문: How to detect shot angle of photo, and auto rotate for website display like desktop apps do on viewing?
답변: In order to do that, you must read the EXIF information out of the JPEG file. You can either do that with exif
PHP extension or with PEL
.
Basically, you have to read the Orientation
flag in the file. Here is an example using the exif
PHP extension and WideImage
for image manipulation.
<?php
$exif = exif_read_data($filename);
$ort = $exif['Orientation'];
$image = WideImage::load($filename);
// GD doesn't support EXIF, so all information is removed.
$image->exifOrient($ort)->saveToFile($filename);
class WideImage_Operation_ExifOrient
{
/**
* Rotates and mirrors and image properly based on current orientation value
*
* @param WideImage_Image $img
* @param int $orientation
* @return WideImage_Image
*/
function execute($img, $orientation)
{
switch ($orientation) {
case 2:
return $img->mirror();
break;
case 3:
return $img->rotate(180);
break;
case 4:
return $img->rotate(180)->mirror();
break;
case 5:
return $img->rotate(90)->mirror();
break;
case 6:
return $img->rotate(90);
break;
case 7:
return $img->rotate(-90)->mirror();
break;
case 8:
return $img->rotate(-90);
break;
default: return $img->copy();
}
}
}
C# 관련
When processing photos, sometimes you want to re-orient the photo according the orientation recorded by the camera (such as the iPhone’s accelerometer) and stored in the EXIF meta data. It’s easy to do:
// Rotate the image according to EXIF data
var bmp = new Bitmap(pathToImageFile); var exif = new EXIFextractor(ref bmp, "n"); // get source from http://www.codeproject.com/KB/graphics/exifextractor.aspx?fid=207371 if (exif["Orientation"] != null) { RotateFlipType flip = OrientationToFlipType(exif["Orientation"].ToString()); if (flip != RotateFlipType.RotateNoneFlipNone) // don't flip of orientation is correct { bmp.RotateFlip(flip); exif.setTag(0x112, "1"); // Optional: reset orientation tag bmp.Save(pathToImageFile, ImageFormat.Jpeg); } // Match the orientation code to the correct rotation: private static RotateFlipType OrientationToFlipType(string orientation) { switch (int.Parse(orientation)) { case 1: return RotateFlipType.RotateNoneFlipNone; break; case 2: return RotateFlipType.RotateNoneFlipX; break; case 3: return RotateFlipType.Rotate180FlipNone; break; case 4: return RotateFlipType.Rotate180FlipX; break; case 5: return RotateFlipType.Rotate90FlipX; break; case 6: return RotateFlipType.Rotate90FlipNone; break; case 7: return RotateFlipType.Rotate270FlipX; break; case 8: return RotateFlipType.Rotate270FlipNone; break; default: return RotateFlipType.RotateNoneFlipNone; } }
출처: http://automagical.rationalmind.net/2009/08/25/correct-photo-orientation-using-exif/
반응형
'IT공부방' 카테고리의 다른 글
[알고리즘] 퍼센트 계산법; 백분율 계산 방법 공식; Percent Formula (2) | 2017.04.18 |
---|---|
[오픈소그] vTiger (오픈소스 CRM) (0) | 2016.09.22 |
[Adobe] Adobe CC 구매 및 사용자 인증하기 (0) | 2016.05.07 |
Photoshop 으로 원하지 않는 물체 지우기 (0) | 2016.05.06 |
무조건 UAC 관리자로 실행하기 (0) | 2016.01.08 |
Hamburger Icon ? 누가 디자인 했을까요? (0) | 2014.09.20 |
페이스북 방문자 확인하기 (0) | 2014.09.18 |
Dell u2711 menu locked (OSD Locked Out) (0) | 2014.09.01 |
도움이 되셨다면 하트모양의 "♡ 공감"을 눌러주시면 큰 격려가 됩니다.
(로그인하지 않으셔도 가능)
(로그인하지 않으셔도 가능)