Mark
Mark

Reputation: 467

iPhone image orientation- Rotating images automatically with PHP, PEL, Image_moo

I'm trying to rotate uploaded images before saving, based on the EXIF ORIENTATION tag.

I've checked each portion of the code separately and everything seems to work on it's own.

$new is the correct path and filename.

Image_moo has been loaded.

PEL(PHP EXIF Library) [http://lsolesen.github.com/pel/] has been loaded.

Images have EXIF data including rotation.

I believe I got the switch statement correct.

The code doesn't cause any errors, it just doesn't rotate the image.

What am I missing or doing wrong?

Mark

 $new = the file that was just uploaded
 ...
 $ext = strtolower($path_parts['extension']);
 ...

  if($ext == 'jpg'){
  $input_jpg = new PelJpeg($new);
  $exif = $input_jpg->getExif();
  if($exif !== NULL){
  $tiff = $exif->getTiff();
  $ifd0 = $tiff->getIfd();
  if($orient = $ifd0->getEntry(PelTag::ORIENTATION)){
     $this->image_moo->load($new);
     $orient = str_replace(' ', '', $orient);
     if (($tmp = strstr($orient, 'Value:')) !== false) {
          $str = substr($tmp, 6, 1);//Get the value of the ORIENTATION tag
     }
     $this->image_moo->load($new);
     switch ($str)
     {
     case 8:
        $this->image_moo->rotate(270);
        break;
     case 3:
        $this->image_moo->rotate(180);
        break;
     case 6:
        $this->image_moo->rotate(90);
        break;
     case 1:
        break;
     }
     $this->image_moo->save($new, TRUE);
 }
 $output_jpg = new PelJpeg($new);
 $output_jpg->setExif($exif);
 $output_jpg->saveFile($new);
}
}

Upvotes: 1

Views: 2369

Answers (1)

RolandasR
RolandasR

Reputation: 3046

Im not sure about PelJpeg but in Imagick i do:

...
switch( $imagick->getImageOrientation() ){
    case 6:
        $imagick->rotateImage(new ImagickPixel(), 90);
        $imagick->setImageOrientation(1);
    break;
    case 8:
        $imagick->rotateImage(new ImagickPixel(), 270);
        $imagick->setImageOrientation(1);
    break;
}
...

Upvotes: 3

Related Questions