1

Using the safari mobile browser with IOS6, the file upload function gives users the option to snap a photo. Unfortunately, upon snapping the photo, while the photo thumb shows up properly in the browser, when you upload to a server, the file is rotated 90 degrees. This appears to be due to the exif data that the iphone sets. I have code that fixes the orientation by rotating the image when serving. However, I suspect it would be better to save the rotated, properly oriented, image so I no longer have to worry about orientation. Many of my other photos do not even have exif data and i don't want to mess with it if I can avoid it.

Can anyone suggest code to save the image so it is properly oriented?

Here is the code that rotates the image. The following code will display the properly oriented image, however, what I want to do is save it so I can then serve it whenever I want without worrying about orientation.

Also I would like to replace impagejpeg call in code below so that any code works for gifs as well as jpgs.

Thanks for suggestions/code!

PHP

//Here is sample image after uploaded to server and moved to a directory
$target = "pics/779_pic.jpg";
$source = imagecreatefromstring(file_get_contents($target));
$exif = exif_read_data($target);
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']) {
        case 8:
            $image = imagerotate($source,90,0);
//echo 'It is 8';
            break;
        case 3:
            $image = imagerotate($source,180,0);
//echo 'It is 3';
            break;
        case 6:
            $image = imagerotate($source,-90,0);
//echo 'It is 6';
            break;

    }
}
// $image now contains a resource with the image oriented correctly
//This is where I want to save resource properly oriented instead of display.
 header('Content-type: image/jpg');
imagejpeg($image);
?>
user1904273
  • 4,562
  • 11
  • 45
  • 96
  • figured out it can be done with no header and imagejpeg($image,$target); only works for jpeg but this solves my problem in the near term. – user1904273 Jan 16 '13 at 01:12

2 Answers2

0

Only JPEG or TIFF files can carry EXIF metadata, so there's no need to worry about handling GIFs (or PNGs, for that matter) with your code.

From page 9 of what I believe is the official specification:

Compressed files are recorded as JPEG (ISO/IEC 10918-1) with application marker segments (APP1 and APP2) inserted. Uncompressed files are recorded in TIFF Rev. 6.0 format.

http://www.cipa.jp/english/hyoujunka/kikaku/pdf/DC-008-2010_E.pdf

cml
  • 101
  • 2
0

To save your image just use the same function imagejpeg and the next parameter to save the image, something like:

imagejpeg($image, $target, 100);

In this case you don't need the specify the header, because you are not showing nothing.

Reference: http://sg3.php.net/manual/en/function.imagejpeg.php

AndreX
  • 417
  • 5
  • 15