cagataygurturk / image-resizer-service

Serverless image resizing service for AWS
https://serverlessrepo.aws.amazon.com/#/applications/arn:aws:serverlessrepo:us-east-1:526515951862:applications~image-resizer-service
MIT License
101 stars 51 forks source link

Orientation changes #21

Closed jojomartius closed 4 years ago

jojomartius commented 4 years ago

For some reason, some pictures change there orientation as soon as i add a ?width=xxx. Any idea how to prevent this?

ulgens commented 4 years ago

In some weird cases, idk why, image metadata gets corrupted. When that happens, different viewers show the image in different orientation and this resize tool can't handle it. Opening the image in sth like PS and re-exporting it or cleaning the metada solves the problem for a specific file.

jojomartius commented 4 years ago

Ahhhh, now I understand ;-) I'll try to explain...

jojomartius commented 4 years ago

That happens mainly if you upload stuff from an iOS Device...

The iPhone/iPod/iPad wants to be a real camera in landscape. And if you hold your phone like this, it really feels like a camera, it has the lenses in the upper right corner, thats ok. No matter how you do your photo, it will be delivered like you make it. So if you hold it in landscape, this side is on the top, if you hold it in portrait, this side is the top.

And because it was to be a correct device it puts extra information in the meta data how it was hold while making the picture. It is not intended to be corrected by the orientation, it just says "this was my orientation".

And this script (like many others) try to correct the orientation by the metadata, but thats not needed. So the trick is pretty simple. Just throw away all the metadata when you save the image in the backend and you won't have any issues. Then all pictures portrait, landscape, upside down look good and like you made them.

Coming from php, i did it like this:

` if(@is_array(getimagesize($this->path . $newFileName))){ $img = new \Imagick($this->path . $newFileName); $orientation = $img->getImageOrientation();

        switch($orientation) {
            case \Imagick::ORIENTATION_BOTTOMRIGHT: 
                $img->rotateimage("#000", 180); // rotate 180 degrees
                break;

            case \Imagick::ORIENTATION_RIGHTTOP:
                $img->rotateimage("#000", 90); // rotate 90 degrees CW
                break;

            case \Imagick::ORIENTATION_LEFTBOTTOM: 
                $img->rotateimage("#000", -90); // rotate 90 degrees CCW
                break;
        }

        // Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image!
        $img->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT);
        $img->stripImage();
        $img->writeImage($this->path . $newFileName);
    }`