PHP read_exif_data 및 방향 조정
방향이 꺼져있는 경우 다음 코드를 사용하여 업로드 된 jpeg 이미지를 회전하고 있습니다. iPhone 및 Android에서 업로드 한 이미지에만 문제가 있습니다.
if(move_uploaded_file($_FILES['photo']['tmp_name'], $upload_path . $newfilename)){
chmod($upload_path . $newfilename, 0755);
$exif = exif_read_data($upload_path . $newfilename);
$ort = $exif['IFD0']['Orientation'];
switch($ort)
{
case 3: // 180 rotate left
$image->imagerotate($upload_path . $newfilename, 180, -1);
break;
case 6: // 90 rotate right
$image->imagerotate($upload_path . $newfilename, -90, -1);
break;
case 8: // 90 rotate left
$image->imagerotate($upload_path . $newfilename, 90, -1);
break;
}
imagejpeg($image, $upload_path . $newfilename, 100);
$success_message = 'Photo Successfully Uploaded';
}else{
$error_count++;
$error_message = 'Error: Upload Unsuccessful<br />Please Try Again';
}
jpeg에서 EXIF 데이터를 읽는 방식에 문제가 있습니까? 예상대로 이미지를 회전하지 않습니다.
이것이 내가 var_dump ($ exif);
array(41) {
["FileName"]=> string(36) "126e7c0efcac2b76b3320e6187d03cfd.JPG"
["FileDateTime"]=> int(1316545667)
["FileSize"]=> int(1312472)
["FileType"]=> int(2)
["MimeType"]=> string(10) "image/jpeg"
["SectionsFound"]=> string(30) "ANY_TAG, IFD0, THUMBNAIL, EXIF"
["COMPUTED"]=> array(8) {
["html"]=> string(26) "width="2048" height="1536""
["Height"]=> int(1536)
["Width"]=> int(2048)
["IsColor"]=> int(1)
["ByteOrderMotorola"]=> int(1)
["ApertureFNumber"]=> string(5) "f/2.8"
["Thumbnail.FileType"]=> int(2)
["Thumbnail.MimeType"]=> string(10) "image/jpeg" }
["Make"]=> string(5) "Apple"
["Model"]=> string(10) "iPhone 3GS"
["Orientation"]=> int(6)
["XResolution"]=> string(4) "72/1"
["YResolution"]=> string(4) "72/1" ["ResolutionUnit"]=> int(2) ["Software"]=> string(5) "4.3.5" ["DateTime"]=> string(19) "2011:09:16 21:18:46" ["YCbCrPositioning"]=> int(1) ["Exif_IFD_Pointer"]=> int(194) ["THUMBNAIL"]=> array(6) { ["Compression"]=> int(6) ["XResolution"]=> string(4) "72/1" ["YResolution"]=> string(4) "72/1" ["ResolutionUnit"]=> int(2) ["JPEGInterchangeFormat"]=> int(658) ["JPEGInterchangeFormatLength"]=> int(8231) } ["ExposureTime"]=> string(4) "1/15" ["FNumber"]=> string(4) "14/5" ["ExposureProgram"]=> int(2) ["ISOSpeedRatings"]=> int(200) ["ExifVersion"]=> string(4) "0221" ["DateTimeOriginal"]=> string(19) "2011:09:16 21:18:46" ["DateTimeDigitized"]=> string(19) "2011:09:16 21:18:46" ["ComponentsConfiguration"]=> string(4) "" ["ShutterSpeedValue"]=> string(8) "3711/949" ["ApertureValue"]=> string(9) "4281/1441" ["MeteringMode"]=> int(1) ["Flash"]=> int(32) ["FocalLength"]=> string(5) "77/20" ["SubjectLocation"]=> array(4) { [0]=> int(1023) [1]=> int(767) [2]=> int(614) [3]=> int(614) } ["FlashPixVersion"]=> string(4) "0100" ["ColorSpace"]=> int(1) ["ExifImageWidth"]=> int(2048) ["ExifImageLength"]=> int(1536) ["SensingMethod"]=> int(2) ["ExposureMode"]=> int(0) ["WhiteBalance"]=> int(0) ["SceneCaptureType"]=> int(0) ["Sharpness"]=> int(1) }
imagerotate에 대한 문서 는 첫 번째 매개 변수에 사용하는 것과 다른 유형을 참조합니다.
imagecreatetruecolor ()와 같은 이미지 생성 함수 중 하나에서 반환하는 이미지 리소스입니다.
다음은이 함수를 사용하는 간단한 예입니다.
function resample($jpgFile, $thumbFile, $width, $orientation) {
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($jpgFile);
$height = (int) (($width / $width_orig) * $height_orig);
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($jpgFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Fix Orientation
switch($orientation) {
case 3:
$image_p = imagerotate($image_p, 180, 0);
break;
case 6:
$image_p = imagerotate($image_p, -90, 0);
break;
case 8:
$image_p = imagerotate($image_p, 90, 0);
break;
}
// Output
imagejpeg($image_p, $thumbFile, 90);
}
Daniel의 코드를 기반으로 리샘플링없이 필요한 경우 이미지를 간단히 회전하는 함수를 작성했습니다.
GD
function image_fix_orientation(&$image, $filename) {
$exif = exif_read_data($filename);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
}
}
한 줄 버전 (GD)
function image_fix_orientation(&$image, $filename) {
$image = imagerotate($image, array_values([0, 0, 0, 180, 0, 0, -90, 0, 90])[@exif_read_data($filename)['Orientation'] ?: 0], 0);
}
ImageMagick
function image_fix_orientation($image) {
if (method_exists($image, 'getImageProperty')) {
$orientation = $image->getImageProperty('exif:Orientation');
} else {
$filename = $image->getImageFilename();
if (empty($filename)) {
$filename = 'data://image/jpeg;base64,' . base64_encode($image->getImageBlob());
}
$exif = exif_read_data($filename);
$orientation = isset($exif['Orientation']) ? $exif['Orientation'] : null;
}
if (!empty($orientation)) {
switch ($orientation) {
case 3:
$image->rotateImage('#000000', 180);
break;
case 6:
$image->rotateImage('#000000', 90);
break;
case 8:
$image->rotateImage('#000000', -90);
break;
}
}
}
이미지를 업로드하는 사람들을위한 더 간단한 기능으로 필요한 경우 자동으로 회전합니다.
function image_fix_orientation($filename) {
$exif = exif_read_data($filename);
if (!empty($exif['Orientation'])) {
$image = imagecreatefromjpeg($filename);
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
imagejpeg($image, $filename, 90);
}
}
미러링 된 케이스 2,4,5,7을 고려하지 않는 이유는 무엇입니까? exif 오리엔테이션 랜드에는 4 개의 케이스가 더 있습니다.
다음은 파일 이름을 취하는 완전한 솔루션입니다.
function __image_orientate($source, $quality = 90, $destination = null)
{
if ($destination === null) {
$destination = $source;
}
$info = getimagesize($source);
if ($info['mime'] === 'image/jpeg') {
$exif = exif_read_data($source);
if (!empty($exif['Orientation']) && in_array($exif['Orientation'], [2, 3, 4, 5, 6, 7, 8])) {
$image = imagecreatefromjpeg($source);
if (in_array($exif['Orientation'], [3, 4])) {
$image = imagerotate($image, 180, 0);
}
if (in_array($exif['Orientation'], [5, 6])) {
$image = imagerotate($image, -90, 0);
}
if (in_array($exif['Orientation'], [7, 8])) {
$image = imagerotate($image, 90, 0);
}
if (in_array($exif['Orientation'], [2, 5, 7, 4])) {
imageflip($image, IMG_FLIP_HORIZONTAL);
}
imagejpeg($image, $destination, $quality);
}
}
return true;
}
누군가가 이것을 보게 될 경우를 대비하여. 위의 switch 문 중 일부가 잘못되었음을 알 수 있습니다.
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($image, -180, 0);
break;
case 6:
$image = imagerotate($image, 90, 0);
break;
case 8:
$image = imagerotate($image, -90, 0);
break;
}
명령 줄에서 ImageMagick을 사용하는 경우 기존 EXIF 방향 데이터를 기반으로 이미지를 자동 회전하는 -auto-orient 옵션을 사용할 수 있다는 점을 언급하는 것이 좋습니다 .
convert -auto-orient /tmp/uploadedImage.jpg /save/to/path/image.jpg
참고 : EXIF 데이터가 프로세스 전에 제거 된 경우 설명 된대로 작동하지 않습니다.
여기서는 모든 것을 설명하고 있습니다. 저는 Laravel을 사용하고 Image Intervention Package를 사용합니다.
First of all, I get my image and send it to my another function for resizing and some other functionality, if we do not need this, you can skip...
Grab the file with a method in my controller,
public function getImageFile(Request $request){
$image = $request->image;
$this->imageUpload($image);
}
Now, I send it to resize and getting the image name and extension...
public function imageUpload($file){
ini_set('memory_limit', '-1');
$directory = 'uploads/';
$name = str_replace([" ", "."], "_", $file->getClientOriginalName()) . "_";
$file_name = $name . time() . rand(1111, 9999) . '.' . $file->getClientOriginalExtension();
//path set
$img_url = $directory.$file_name;
list($width, $height) = getimagesize($file);
$h = ($height/$width)*600;
Image::make($file)->resize(600, $h)->save(public_path($img_url));
$this->image_fix_orientation($file,$img_url);
return $img_url;
}
Now I call my image orientation function,
public function image_fix_orientation($file,$img_url ) {
$data = Image::make($file)->exif();
if (!empty($data['Orientation'])) {
$image = imagecreatefromjpeg($file);
switch ($data['Orientation']) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
imagejpeg($image, $img_url, 90);
}
}
And That's all...
I hate to chime in with yet another set of orientation values, but in my experience using any of the values listed above, I always ended up with upside down images when uploading portrait orientation shots directly from an iPhone. Here's the switch statement I ended up with.
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($image, -180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
jhead -autorot jpegfile.jpg
Is also a useful way to approach this.
jhead is a standard program in Linux (use 'sudo apt-get install jhead' to install), this option looks at the orientation and rotates the image correctly and losslessly only if it requires. It then also updates the EXIF data correctly.
In this way you can process a jpeg (or multiple jpegs in a folder) in a simple one-pass way that fixes rotation issues permanently.
E.g: jhead -autorot *.jpg will fix a whole folder of jpeg images in just the manner the OP requires in the initial question.
While it's not technically PHP I did read this thread and then used my jhead suggestion instead, called from a PHP system() call to achieve the results I was after which were coincident with the OPs: to rotate images so any software (like 'fbi' in Raspbian) could display them correctly.
이것에 비추어 다른 사람들이 jhead가이 문제를 얼마나 쉽게 해결하는지 알면 도움이 될 것이라고 생각했으며, 이전에 아무도 언급하지 않았기 때문에 정보 제공 목적으로 만 여기에 정보를 게시했습니다.
나는 또한 orientate()
form Intervention을 사용 했으며 완벽하게 작동합니다.
$image_resize = Image::make($request->file('photo'));
$image_resize->resize(1600, null,function ($constraint)
{
$constraint->aspectRatio();
});
$filename = $this->checkFilename();
$image_resize->orientate()->save($this->photo_path.$filename,80);
중재 이미지에는 방법이 orientate()
있습니다.
$img = Image::make('foo.jpg')->orientate();
참고 URL : https://stackoverflow.com/questions/7489742/php-read-exif-data-and-adjust-orientation
'code' 카테고리의 다른 글
네이티브와 폰갭 사이의 어려움, 간단한 앱 요구 사항 (0) | 2020.10.18 |
---|---|
Java Swing-JScrollPane 사용 및 맨 위로 스크롤 (0) | 2020.10.18 |
Java에서 while 루프를 어떻게 종료합니까? (0) | 2020.10.18 |
Gradle DSL 메서드를 찾을 수 없음 : 'compile ()' (0) | 2020.10.18 |
Apache Tomcat 서버의 명령 프롬프트에서 디버그 모드를 시작하는 방법은 무엇입니까? (0) | 2020.10.18 |