如何检测照片的拍摄角度,以及桌面应用程序在查看时自动旋转网站显示?

Pen*_*m10 21 php image-processing

如果我用相机拍照,它会存储设备的方向/角度,所以当我用一台好的应用程序在PC上查看图像时,显示自动旋转到0.

但是当我上传到网站时它显示原始角度,因此图像看起来不太好.

如何使用PHP检测并旋转图像,并从其元信息中清除此角度标记.

And*_*ore 25

为此,您必须从JPEG文件中读取EXIF信息.您可以使用exifPHP扩展或使用PEL.

基本上,您必须读取Orientation文件中的标志.以下是使用exifPHP扩展和WideImage图像处理的示例.

<?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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Wes*_*Wes 18

我修改了Chris的例子来添加对exif函数的检查,删除镜像,并使用相同的文件名将文件写回文件系统.这样,你可以在调用move_uploaded_file后立即调用这个函数,如下所示:

move_uploaded_file($uploadedFile, $destinationFilename);
correctImageOrientation($destinationFilename);
Run Code Online (Sandbox Code Playgroud)
function correctImageOrientation($filename) {
  if (function_exists('exif_read_data')) {
    $exif = exif_read_data($filename);
    if($exif && isset($exif['Orientation'])) {
      $orientation = $exif['Orientation'];
      if($orientation != 1){
        $img = imagecreatefromjpeg($filename);
        $deg = 0;
        switch ($orientation) {
          case 3:
            $deg = 180;
            break;
          case 6:
            $deg = 270;
            break;
          case 8:
            $deg = 90;
            break;
        }
        if ($deg) {
          $img = imagerotate($img, $deg, 0);        
        }
        // then rewrite the rotated image back to the disk as $filename 
        imagejpeg($img, $filename, 95);
      } // if there is some rotation necessary
    } // if have the exif orientation info
  } // if function exists      
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*lli 17

如果你只想使用GD和php EXIF扩展,你可以使用:

function _mirrorImage ( $imgsrc)
{
    $width = imagesx ( $imgsrc );
    $height = imagesy ( $imgsrc );

    $src_x = $width -1;
    $src_y = 0;
    $src_width = -$width;
    $src_height = $height;

    $imgdest = imagecreatetruecolor ( $width, $height );

    if ( imagecopyresampled ( $imgdest, $imgsrc, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height ) )
    {
        return $imgdest;
    }

    return $imgsrc;
}

function adjustPicOrientation($full_filename){        
    $exif = exif_read_data($full_filename);
    if($exif && isset($exif['Orientation'])) {
        $orientation = $exif['Orientation'];
        if($orientation != 1){
            $img = imagecreatefromjpeg($full_filename);

            $mirror = false;
            $deg    = 0;

            switch ($orientation) {
              case 2:
                $mirror = true;
                break;
              case 3:
                $deg = 180;
                break;
              case 4:
                $deg = 180;
                $mirror = true;  
                break;
              case 5:
                $deg = 270;
                $mirror = true; 
                break;
              case 6:
                $deg = 270;
                break;
              case 7:
                $deg = 90;
                $mirror = true; 
                break;
              case 8:
                $deg = 90;
                break;
            }
            if ($deg) $img = imagerotate($img, $deg, 0); 
            if ($mirror) $img = _mirrorImage($img);
            $full_filename = str_replace('.jpg', "-O$orientation.jpg",  $full_filename); 
            imagejpeg($img, $full_filename, 95);
        }
    }
    return $full_filename;
}
Run Code Online (Sandbox Code Playgroud)