PHP的read_exif_data和调整方向

如果方向closures,我正在使用以下代码来旋转上传的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的文档指的是第一个参数的不同types,而不是您使用的:

一个图像资源,由图像创build函数之一返回,如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更简单,只需要进行自动旋转即可。

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

可能值得一提的是,如果您是通过命令行使用ImageMagick,则可以使用-auto-orient选项,该选项将根据现有的EXIF方向数据自动旋转图像。

 convert -auto-orient /tmp/uploadedImage.jpg /save/to/path/image.jpg 

请注意:如果EXIF数据在进程之前被剥离,将不会像描述的那样工作。

以防万一有人遇到这种情况。 从我能弄出来的上面的一些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; } 

我讨厌用另外一组方向值,但在使用上面列出的任何一个值的经验中,当直接从iPhone上传人像定向镜头时,我总是以颠倒的图像结束。 这是我结束的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; } 

在这里,我解释了整个事情,我使用Laravel并使用图像干预包。

首先,我得到我的形象,并发送到我的另一个function,resize和其他function,如果我们不需要这个,你可以跳过…

用我的控制器中的方法抓取文件,

  public function getImageFile(Request $request){ $image = $request->image; $this->imageUpload($image); } 

现在,我发送它来调整和获取图像名称和扩展名…

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

现在我打电话给我的图像定位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); } } 

就这样…