Jump to content

What to do when read_exif is not working on other image files


kingreigns

Recommended Posts

I have this short php code which will allow the image to be rotated correctly (based on their exif data)

//IMAGE ROTATION//
                $image_name = "./postimgs/".$post_img;
                $rotated_imagename = "./postimgs/rotated_".$post_img;
                $exif_data = exif_read_data($image_name);

                function orientation($exif_data){
                  foreach ($exif_data as $key => $value) {
                    if(strtolower($key)=="orientation") {
                      return $value;
                    }
                  }
                } // function

                function orientation_flag($orientation) {
                  switch ($orientation):
                    case 1:
                      return 0;
                    case 8:
                      return 90;
                    case 3:
                      return 180;
                    case 6:
                      return 270;
                  endswitch;

                }// function

                $orientation = orientation($exif_data);
                $degrees = orientation_flag($orientation);

                $image_data = imagecreatefromjpeg($image_name);
                $image_rotate = imagerotate($image_data, $degrees, 0);

                imagejpeg($image_rotate, $rotated_imagename);
                imagedestroy($image_data);
                imagedestroy($image_rotate);
                //END IMAGE ROTATION//

                echo "<img id='placeholder2' src='./postimgs/rotated_$post_img' style = 'object-fit:contain; width:250px; height:137px; margin-left:0px; margin-top:10px; border: 0px solid #dedede;' >";

The only problem with this is that, when uploading a non-jpeg file (png,gif,etc), it would give me a `File not supported exif data` error. Is there any get around with this like - when reading the image is not a jpeg, it would skip all the `exif_data` thing?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.