pixelstunde / joomla-plg-pxlcompressor

PXLCompressor is a joomla plugin meant for resizing and compressing images uploaded with the media manager on the fly.
http://pixelstun.de
30 stars 4 forks source link

Rotate images based on exif data #26

Open Bytehawk opened 3 years ago

Bytehawk commented 3 years ago

Not a real coder so my excuse for messy code. I 'm sure someone can improve and integrate this better.

after the line

if (in_array($object->type, $this->allowed_mime_types))
        {

I added this (right before you scale the image)

```

                    // Path to an image containing exif data
                    $imagePath = $object_path;

                    // new JImage object from image path
                    $image = new JImage($imagePath);

                    // Read the exif data from the image
                    $exif = exif_read_data($imagePath);
                    $rotate = 0;

                    // Get the angle to rotate the image based on the orientation
                    if (!empty($exif['Orientation'])) {

                        // Based on the orientation of time image, assign the rotation angle.
                        switch ($exif['Orientation']) {
                            case 8:
                                $rotate = 90;
                                break;
                            case 3:
                                $rotate = 180;
                                break;
                            case 6:
                                $rotate = -90;
                                break;
                        }

                        // Rotate the image appropriately
                        $newImage = $image->rotate($rotate);;

                        // Overwrite the old image with new image
                        if (!$newImage->toFile($imagePath)) {
                            die("Failed to overwrite file: " . $imagePath);
                        }
                    }


it rotates the image before scaling it.  Makes uploads from phones super easy.

code comes from https://www.codementor.io/tips/8307241779/rotating-an-image-in-joomla-based-on-exif-orientation