简体   繁体   中英

PHP get image exif and rotate/resize image

I'm building an application were you can take a photo and upload an image with your mobile phone, but the images you take as an portrait automatically rotates to landscape orientation. Trust me I've tried a lot and made research before posting!

I don't have advanced skills in PHP but I have a code below that handles the resize good but it doesn't do the rotation.

Does anyone see any immediate issues with it?

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

Thankful for help!

You don't call your loadResize function in your example.

A solution using the GD library. This solution only looks for the exif data to see if the image should be rotated. It does not look to the actual aspect ratio.

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

I want to answer this question from the point of view of a user of a web site. **as I have encountered the same issue recently.

First, I don't have problems uploading image files (jpg) from android device or PC/MAC. The orientation of the image is correct after upload.

However, most of the problem is with iOS device, especially photos taken from iPhone. (I have an iPhone 6 Plus 64GB)

I have tried many methods including the suggestion above, and using the code info(0) & info(1) for getting width and height.

The trouble is that image from iPhone won't set orientation automatically after photo is taken. One can run each image files by the phone's default photo editor, rotate it, save it, and then rotate it again to the correct orientation and save again, and problem is solved.

But to ask the user of the web site to do this is not possible.

Since over 90% of the problem is related to photo images from iOS device. And the fun part is all protraits are turned to landscape in the same angle. (90degree counterclockwise).

Thus, my final solution is to include a checkbox for "landscape" images.

When the image file is uploaded, if it is in landscape format, user just needs to click landscape.

And program will check if width divided by height is less one. If so, it will rotate it to landscape (I use 90 in my case). *remark: in reality, I don't have any problem with "landscape images at all.

Then if the image is "protrait", user leave the checkbox unchecked. Now program will check if width divided by height is larger than one. If so, it will rotate it to protrait (I use -90 in my case). *remark: so far, this has solved all my problem with iPhone photos.

So, with the remaining 10% problem(if any), I just ask the users to manually use a photo editor on PC to open and save image file again.

It is not too much work, and problem is solved. *remark: so far, I don't have any phone calls about this issue anymore after doing this.

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.

after doing that resize 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" 
                      );`enter code here`

`?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM