Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Image Dimensions from Base64 String

Is there a relatively simple way (perhaps regular expressions?) of detecting the dimensions -height & width - of an image from a base64 string (and perhaps the filesize)? That is to say without actually creating an image resource?

like image 912
Descartes Avatar asked Oct 02 '12 02:10

Descartes


People also ask

How do I decode a Base64 string?

To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.

How do I determine Base64 string size?

Each Base64 digit represents exactly 6 bits of data. So, three 8-bits bytes of the input string/binary file (3×8 bits = 24 bits) can be represented by four 6-bit Base64 digits (4×6 = 24 bits). This means that the Base64 version of a string or file will be at least 133% the size of its source (a ~33% increase).


1 Answers

Decode the base64, and use getimagesizefromstring().

http://www.php.net/manual/en/function.getimagesizefromstring.php

This will be way more reliable than trying to write your own handling of each image type available.

like image 68
Brad Avatar answered Oct 04 '22 08:10

Brad