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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With