Vertical images rotated horizontal when uploaded to server in php

0 votes
asked Jul 3, 2014 in Php by xavier

I have created a webpage using php where users can upload images. I am using php GD library for image functionality. When I test that functionality, some images are automatically rotated to horizontal, even though I uploaded the image in vertical shape. Before uploading the image, I have opened it using image viewer in my computer and it shows vertical. Below is code I have used. What am I doing wrong here?

$src = imagecreatefromjpeg($originalFile);
$dest = imagecreatetruecolor($imageResizeWidth, $imageResizeHeight);

imagecopyresized($dest, $src, 0, 0, $cropCoordination[0], $cropCoordination[1], $imageResizeWidth, $imageResizeHeight, $cropCoordination[4], $cropCoordination[5]);
imagedestroy($src);

$destinationFilePath = $destinationFolderPath . DIRECTORY_SEPARATOR . $fileNameWithoutExtn . ".jpg";

if (!file_exists($destinationFolderPath)) {
      mkdir($destinationFolderPath, 0777, true);
 }

 header('Content-Type: image/jpeg');
 imagejpeg($imageResource, $destinationFilePath, 90);
Share

1 Answer

0 votes
answered Jul 3, 2014 by Anitha
selected Jul 3, 2014 by administrator
 
Best answer

The issue is because of the Orientation value of the image. Each image will have orientation as meta data, that will have the original direction of the image. So, you can check that orientation and rotate the image before you save that. The same orientation is applied when you view it in the image viewer, that’s why it looks correct.

Below code will help to get the orientation if you send image file path.

private static function RotateImage($imageFilePath, $imageToRotate) {
        $exif = exif_read_data($imageFilePath);
        $ort = $exif['Orientation'];
        switch ($ort) {
            case 3:
                return imagerotate($imageToRotate, 180, 0);
            case 6:
                return imagerotate($imageToRotate, -90, 0);
            case 8:
                return imagerotate($imageToRotate, 90, 0);
        }
        return false;
    }

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...