0

I have tried a lot of solutions that I have found on internet but nothing is working for me. I am trying to resize and rotate the image depends on exif data but the image is not rotating. Resizing the image is working fine. But the rotation is not working. Below is the function I used to do that.

function resize_imageb($newbfile,$max_resolution){
    if(file_exists($newbfile)){
        $original_image = imagecreatefromjpeg($newbfile);   
        
        $exif = exif_read_data($newbfile, 0, true);
        
        if(!empty($exif['Orientation'])) {
            switch($exif['Orientation']) {
            case 8:
                $original_image = imagerotate($original_image,90,0);
                break;
            case 3:
                $original_image = imagerotate($original_image,180,0);
                break;
            case 6:
                $original_image = imagerotate($original_image,-90,0);
                break;
            } 
        }
        
        $original_width = imagesx($original_image);
        $original_height = imagesy($original_image);
        
        $ratio = $max_resolution/$original_width;
        $new_width = $max_resolution;
        $new_height = $original_height * $ratio;
        
        if($new_height > $max_resolution){
            $ratio = $max_resolution / $original_height;
            $new_height = $max_resolution;
            $new_width = $original_width * $ratio;
        }
        if($original_image){
            $new_image = imagecreatetruecolor($new_width,$new_height);
            imagecopyresampled($new_image, $original_image, 0, 0, 0, 0,$new_width, $new_height, $original_width, $original_height);
            imagejpeg($new_image,$newbfile,100);
            imagedestroy($original_image);
            imagedestroy($new_image);
        }
    }
}

When I check the resized image the Orientation information is gone from exif data however the original image I have uploaded did had the Orientation information. I am not sure what i am missing or doing wrong. Can someone help me with this?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Could you show us EXIF contents? also what's the value in `$exif['Orientation']` could you log it and tell us the value? – ZeroWorks Sep 07 '21 at 14:22
  • IFD0.Orientation: 6, its the value i get when check log for the original image I upload – Toral Harish Sep 07 '21 at 14:40
  • Could you try to replace rotation angle from `-90` to `270`?, I think negative numbers aren't allowed. So like should be `$original_image = imagerotate($original_image,270,0);` – ZeroWorks Sep 07 '21 at 14:48
  • Yes, but you are calling `imagerotate` function with a negative angle -90. Could you try using positive angle: 270? – ZeroWorks Sep 07 '21 at 14:53
  • still have the same issue. – Toral Harish Sep 07 '21 at 14:57
  • So when I upload the portrait image it will rotate -90, but when i upload landscape it it doesn't rotate and stays the same. so the issue is with portrait images. How do I fix this? – Toral Harish Sep 07 '21 at 15:04
  • ... a dump of exif data for both portrait and landscape would help. – ZeroWorks Sep 07 '21 at 15:07
  • I think `switch($exif['Orientation'])` is not wroking as expected... `exif_read_data` returns an array, please try to replace it with `switch($exif['IDF0']['Orientation'])`, and remove the avove `empty` if temporally. To dump you can use `print_r` function or `var_dump`. To output formated: `echo('
    '); print_r($exif); echo('
    ');`
    – ZeroWorks Sep 07 '21 at 15:14
  • still the same result. – Toral Harish Sep 07 '21 at 17:43

1 Answers1

0

Instead of doing the 2 part (resize and rotate) in one function, I made two functions one for rotation created by Wes and then for resize. First called the rotation function and then resize function.