1

I am working on a simple php upload script. it works fine. but for some reason, when the images are compressed, they rotate. sometimes they rotate completely 180 degrees, sometimes 90 degrees. Why is it doing this and how do i stop it? here is my code :

$name = ''; $type = ''; $size = ''; $error = '';

function compress_image($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);
    if ($info['mime'] == 'image/jpeg')
        $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif')
        $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png')
        $image = imagecreatefrompng($source_url);


    imagejpeg($image, $destination_url, $quality);
    //echo "result ".$destination_url;
    //correctImageOrientation($destination_url);
     return $destination_url;
}

$url = 'test/compressed.jpg';

compress_image($_FILES["file"]["tmp_name"], $url, 30);
ADyson
  • 57,178
  • 14
  • 51
  • 63
Skyent Group
  • 109
  • 9
  • I've seen some unexpected orientations pop up with JPEG images taken by some mobile phones. If `imagejpeg` resets an original EXIF orientation, this might result in some unexpected rotation of the image. I suggest you check out the EXIF orientation prior and after running the image file through your code, and see if there is any change. – Ro Achterberg Dec 17 '18 at 18:06
  • @RoAchterberg is there any way to simply remove the EXIF? – Skyent Group Dec 17 '18 at 18:25
  • please see [this answer](https://stackoverflow.com/questions/3614925/remove-exif-data-from-jpg-using-php). – Ro Achterberg Dec 17 '18 at 18:29
  • I am really starting to wander if it really is EXIF. i followed this tutorial, it was still rotating. will try your link and see if there is a difference : https://obrienmedia.co.uk/blog/fixing-photo-orientation-on-images-uploaded-via-php-from-ios-devices – Skyent Group Dec 17 '18 at 18:41
  • I strongly recommend comparing the EXIF orientation value in your source file to the output file as a start. If something changed, then you can try again with a source file that has a default orientation, and see if the problem persists. Maybe then try feeding it a JPEG that you've saved from Photoshop with standard settings? – Ro Achterberg Dec 17 '18 at 19:27

0 Answers0