how to fix php 8.1.2 GD image resize rotation problem imagecopyresampled

Author: Anonymous Publish Time : 2023.11.11 02:12:46 Updated Time : 2023.11.11 02:12:46

when upload image and resize images to server, 

some images was rotated by mistake, why the original image is right but the resize image is rotated ?

this was happened when we upload the images which was rotated from the  windows OS 

when we use imagecopyresampled  to generate the resized images, the rotation information was lost (or is the original 'Orientation' when the picture was taken)

so we should use 

 

$exif = exif_read_data($sourceImg);

        if (!empty($exif['Orientation'])) {
            switch ($exif['Orientation']) {
                case 8:
                    $thumb = imagerotate($thumb, 90, 0);
                    break;
                case 3:
                    $thumb = imagerotate($thumb, 180, 0);
                    break;
                case 6:
                    $thumb = imagerotate($thumb, -90, 0);
                    break;
            }
        }
 
to check the 'Orientation'  and rotated it to right  before generate samall images (using 
imagewebp or other format
Comment
Login To Comment

No Comments