News:

SMF 2.0.19 has been released! Please update. Read more.

Main Menu

Resize Attached Images Mods for SMF 2.0.x?

Started by ApplianceJunk, April 14, 2014, 09:12:00 PM

Previous topic - Next topic

roshaoar

Quote from: Blue Crab on October 20, 2014, 09:50:58 AM
I love the "Resize Attached Images" mod (thanks Kays)... it allows iPhone users to upload images to my forum without having to resize them first. But, I'm having an issue with image orientation. A lot of iPhone images have the wrong orientation... sideways, upsidedown, etc... Does anyone know of a mod that will automatically read the exif data on an image and set the correct orientation during image upload?

Just wanted to mention a bodge that'll help, at least for images in posts using the IMG BBC tag. The mod I just found "BBCode with style" allows CSS rotation to be added to an image. With rotation it's really easy for the poster to set their image right. It works in my testexample really well - http://goo.gl/iWtQjS - the 4th and 5th and 6th posts are the same image. 5 uses user-set (ie the poster does it) styles to get it pointing the right way.

szinski

#21
I am reporting back to say that I have found a solution to the exif orientation problem.

Using the Resize Attached Images mod, add the following code above/before the line that reads "// Added for the Image resize mod" in Sources/Post.php.

NOTE: In order for this to work, your PHP server must be built with the --enable-exif configuration option, and the Re-encode potentially dangerous image attachments option turned off under attachment settings in SMF.



            $imageFile = $dummy;
            $imageSize = getimagesize($imageFile);
           
            if ($imageSize['mime'] == 'image/jpeg') {
                $imageExif = exif_read_data($imageFile);
                $imageOrientation = $imageExif['Orientation'];

                if ($imageOrientation) {
                    $image = imagecreatefromjpeg($imageFile);
                    $imageRotated = false;

                    switch($imageOrientation) {
                        case 3:
                            $image = imagerotate($image, 180, 0);
                            $imageRotated = true;
                            break;
                        case 6:
                            $image = imagerotate($image, -90, 0);
                            $imageRotated = true;
                            break;
                        case 8:
                            $image = imagerotate($image, 90, 0);
                            $imageRotated = true;
                            break;
                    }

                    if ($imageRotated) {
                        imagejpeg($image, $imageFile);
                    }

                    imagedestroy($image);
                }
            }



I have this running on my forum and now people can upload photos straight from their iPads/iPhones and the orientation is correct. A big shout-out to member "dsissitka" on my forum for finding this solution.

FrizzleFried

I can CONFIRM the code above works FANTASTICALLY!

Awesome!

Thanks!

szinski

#23
While the above code works, I found that it generates an error if the image doesn't contain orientation information (it's a harmless error and can be ignored). But, no code should generate errors, so here is version 2.0 which checks to see if the 'Orientation' key exists before trying to rotate it.


                        // Rotate image according to Exif orientation
                        $imageFile = $dummy;
                        $imageSize = getimagesize($imageFile);

                        if ($imageSize['mime'] == 'image/jpeg')
                        {
                                $imageExif = exif_read_data($imageFile);

                                if (array_key_exists('Orientation', $imageExif))
                                {
                                        $imageOrientation = $imageExif['Orientation'];
                                        $image = imagecreatefromjpeg($imageFile);
                                        $imageRotated = false;

                                        switch($imageOrientation)
                                        {
                                                case 3:
                                                        $image = imagerotate($image, 180, 0);
                                                        $imageRotated = true;
                                                        break;
                                                case 6:
                                                        $image = imagerotate($image, -90, 0);
                                                        $imageRotated = true;
                                                        break;
                                                case 8:
                                                        $image = imagerotate($image, 90, 0);
                                                        $imageRotated = true;
                                                        break;
                                        }

                                        if ($imageRotated)
                                                imagejpeg($image, $imageFile);

                                        imagedestroy($image);
                                }
                        }


Enjoy!

jakes_t

Just a heads up to anybody also facing the same problem, the resize mod together with this above code works nicely to keep file sizes nice and small as well as correctly orientating the image.

Users can now upload pics directly from their phones and have it orientated correctly and reduced in size.

Advertisement: