PHP获取图像退出和旋转/调整图像大小



我正在构建一个应用程序,您可以用手机拍摄照片并上传图像,但您作为人像拍摄的图像会自动旋转到横向。相信我,我已经尝试了很多,并在发布之前做了研究!

我没有高级的PHP技能,但我有下面的代码可以处理大小调整,但它不做旋转。

有人发现有什么直接的问题吗?

// resizeImage
function loadResize($imageName, $imageWidth, $imageHeight, $resizeType) {
list($width, $height, $type, $attr) = getimagesize($imageName);
if($resizeType=="portrait" && $height<=$imageHeight) {
    return;
}
if($resizeType=="landscape" && $width<=$imageWidth) {
    return;
}
    $exif = exif_read_data($imageName);
if (!empty($exif['Orientation'])) {
    switch ($exif['Orientation']) {
        case 3:
            $imageName = imagerotate($imageName, 180, 0);
            break;
        case 6:
            $imageName = imagerotate($imageName, -90, 0);
            break;
        case 8:
            $imageName = imagerotate($imageName, 90, 0);
            break;
    }
}
// *** 1) Initialise / load image
$resizeObj = new resize($imageName);
// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
$resizeObj -> resizeImage($imageWidth, $imageHeight, $resizeType);
// *** 3) Save image
$resizeObj -> saveImage($imageName, 100);
}

感谢你的帮助!

你没有在你的例子中调用loadResize函数。

使用GD库的解决方案。此解决方案仅查找exif数据以查看是否应该旋转图像。它不考虑实际的长宽比。

<?php
$imageName = "image.jpg";
$exif = exif_read_data($imageName);
if (!empty($exif['Orientation'])) {
    switch ($exif['Orientation']) {
    case 3:
        $angle = 180 ;
        break;
    case 6:
        $angle = -90;
        break;
    case 8:
        $angle = 90; 
        break;
    default:
        $angle = 0;
        break;
    }   
}   
if (preg_match("/.*.jpg/i", $imageName))
    $source = imagecreatefromjpeg($imageName);
else
    $source = imagecreatefrompng($imageName);
$source = imagerotate($source, $angle, 0);
imagejpeg($source, $imageName);
?>

我想从一个网站用户的角度回答这个问题。**因为我最近遇到了同样的问题。

首先,我从android设备或PC/MAC上传图像文件(jpg)没有问题。图片上传后方向正确

然而,大部分问题都出在iOS设备上,尤其是从iPhone上拍摄的照片。(我有一部iPhone 6 Plus 64GB)

我尝试了很多方法,包括上面的建议,并使用代码info(0) &Info(1)获取宽度和高度

问题是iPhone的图像不会在拍照后自动设置方向。用户可以通过手机默认的照片编辑器运行每个图像文件,旋转并保存,然后再次旋转到正确的方向并再次保存,问题就解决了。

但是要求网站用户这样做是不可能的。

因为超过90%的问题与来自iOS设备的照片图像有关。有趣的是,所有的轮廓都以相同的角度变成了景观。(90度逆时针)。

因此,我的最终解决方案是为"横向"图像添加一个复选框。

上传图像文件时,如果是横向格式,用户只需点击横向即可。

程序将检查宽度除以高度是否小于1。如果是,它将旋转到横向(在我的情况下我使用90)。*备注:实际上,我对"风景图片"没有任何问题。

如果图像为"protrait",则不勾选该复选框。现在程序将检查宽度除以高度是否大于1。如果是这样,它将旋转它到轮廓(我使用-90在我的情况下)。*备注:到目前为止,这已经解决了我使用iPhone照片的所有问题。

所以,剩下的10%的问题(如果有的话),我只是要求用户手动使用PC上的照片编辑器再次打开并保存图像文件。

这不是太多的工作,问题解决了。*备注:到目前为止,我没有收到任何关于这个问题的电话。
In my situation I uploaded the image not noticing of EXIF 
functionality. When I use my mobile I had problem with the rotation. 
so I used the best EXIF functionality occasionally the one stickered. 
OK..... when uploaded I normally resize and that caused rotation 
problems from big mobile image. which they are big.   so what I did 
was
 to 
  send
   $collection= resizefunctioncall("url to files");
   before resizing the image.

调整图像大小。

wont work if not. Because  you would have change details about the 
image.... so check before change. 

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;
                                                        }
               $zzz=adjustPicOrientation("../img/albums/$usera.jpg");
                       if(file_exists($zzz))
                               {
                                 $image = new SimpleImage();
                                $image->load($zzz );
                                $image->resize(300,450);
                          $image->save("../img/albums/sm/$usera.jpg" 
                      );

最新更新