1

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);
}
}
Drakes
  • 23,254
  • 3
  • 51
  • 94
Mark
  • 467
  • 1
  • 6
  • 21

1 Answers1

3

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;
}
...
RolandasR
  • 3,030
  • 2
  • 25
  • 26
  • The test page I was using displayed the rotated images just fine but the image gallery was not displaying them correctly. For some reason it started working after I changed it to set orientation to 1 after rotation `$orient->setValue(1);`. This step wasn't necessary on the test page. I must be missing something, but at least it works now. Thanks for your sample code! – Mark Dec 03 '11 at 05:37
  • setImageOrientation basically resets EXIF. If you rotate image 90degree and EXIF says it rotated 90degree in total it's 180 – RolandasR Dec 03 '11 at 06:10