Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get height and width from base64 encoded image?

Tags:

php

base64

I need to get the height and width from an base64 encoded image.

According to other sites getimagesizefromstring() should do the trick, but for me it doesn't work at all.

Example code:

    <?php
    $image = "data:image;base64,/9j/4AAQdihdiwd......";
    $data = getimagesizefromstring($image);
    echo $data[0]; // no output
    print_r($data); // no output

What I did wrong?

like image 382
fips123 Avatar asked Dec 13 '22 18:12

fips123


1 Answers

You passed to the getimagesizefromstring function a data URI string which is used to display an image in HTML, while the binary representation of the image is expected.

You need to grab the base64 encoded string first then decode it:

<?php
$image = 'data:image/jpeg;base64,/9j/4AAQdihdiwd......';
$binary = \base64_decode(\explode(',', $image)[1]);

$data = \getimagesizefromstring($binary);
print_r($data);
like image 107
emix Avatar answered Dec 17 '22 22:12

emix