Bananam00n
Bananam00n

Reputation: 812

Iphone imagecopy rotates my image

I'm building an HTML application that works with the ipad camera, and uploads the taken image to my server. When uploaded the image gets a watermark over it by using imagecopy().

Everything works fine when I test it on my computer, but for some reason, if I take a picture on ipad/ipod/iphone in portrait, the image WITH the watermark gets rotated to a landscape mode.

To clarify: the original picture is uploaded correctly, the image with the watermark is rotated. This does NOT happen when I try that on my computer with a portrait image.

Here is some code, if that helps (I use the Codeigniter framework). If you need any more code, just ask, although I don't think there's going anything wrong with the upload itself.

//The code for the imagecopy do add the watermark
$overlay = imagecreatefrompng(base_url() . 'assets/images/imgoverlay.png');
$img = imagecreatefromjpeg(base_url() . 'uploads/' . $config['file_name']);
$imageSize = getimagesize(base_url() . 'uploads/' . $config['file_name']);
$sx = imagesx($overlay);
$sy = imagesy($overlay);
$newWatermarkWidth = $imageSize[0];
$newWatermarkHeight = $sy * $newWatermarkWidth / $sx;

$test = imagecopy(
          $img, 
          $overlay, 
          $imageSize[0]/2 - $newWatermarkWidth/2,
          $imageSize[1] - $newWatermarkHeight,
          0,
          0,
          imagesx($img),imagesy($img)
        );

Many thanks!

Upvotes: 0

Views: 1218

Answers (2)

Harald K
Harald K

Reputation: 27094

I'd guess this is related to this question: Generated images showing flipped

The iPad camera probably just stores the image in the same way, regardless of device orientation, and instead writes an Exif tag to make sure it's properly rotated when displayed.

Upvotes: 0

Til
Til

Reputation: 24

Are you sure the image is not rotated? Most viewers automatically rotates the image for you, so you will never see this on your computer.

Try this:

$exif = exif_read_data('test.jpg');
if(isset($exif['Orientation'])) {
    if($exif['Orientation'] === 1) print 'rotated clockwise by 0 deg (nothing)';
    if($exif['Orientation'] === 8) print 'rotated clockwise by 90 deg';
    if($exif['Orientation'] === 3) print 'rotated clockwise by 180 deg';
    if($exif['Orientation'] === 6) print 'rotated clockwise by 270 deg';

    if($exif['Orientation'] === 2) print 'vertical flip, rotated clockwise by 0 deg';
    if($exif['Orientation'] === 7) print 'vertical flip, rotated clockwise by 90 deg';
    if($exif['Orientation'] === 4) print 'vertical flip, rotated clockwise by 180 deg';
    if($exif['Orientation'] === 5) print 'vertical flip, rotated clockwise by 270 deg';
}

Upvotes: 1

Related Questions