Getting exif metadata from image base64 in PHP
Exif headers provide useful metadata when it comes to images such as the image's orientation.
If you have used / are using Intervention Image, you might have noticed that some of the pictures you upload are turned from portrait to landscape.
If you upload the image normally, in Laravel you can orientate the image properly like so:
```php
Image::make(Request::file('image'))->orientate();
```
But if you get the image data as a base 64 string this approach won't work, but the following snippet can be used to get the desired information from the image
```php
$stream_resource = "data://image/jpeg;base64," . $base_64_image_string;
$exif_meta = exif_read_data($stream_resource);
```
You can now get the image's orientation and act based on it's value
```php
$orientation = $exif_meta["Orientation"];
```
Also, make sure you have this line in you php.ini file
```pf
allow_url_fopen=On
```
Otherwise you will get
```php
ErrorException: exif_read_data(): Unable to open file in file [filename]
```
Denisa Halmaghi
30 Jun 2021
« Back to post