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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.