TechMech
TechMech

Reputation: 170

php get file size from data uri

I have a form upload plugin in my form which upload the image using data uri such as

$img = 'data:image/jpeg;base64,/9j/4QAYRXhpZ...............

I need to have file size constraint such as file size should not exceed 1MB.

I can get the image dimension(width & height) using list($width, $height) = getimagesize($img);.

How can I get the file size for the same. Is there any pre defined function in php which I can use to get the file size of data uri

Upvotes: 4

Views: 3177

Answers (1)

Tushar
Tushar

Reputation: 8049

You can use strlen to get the size of the string (and therefore, the file) in bytes. base64 data is ~1/3 larger than binary data, so don't forget to base64_decode first.

Then your code is:

$img = 'base64,/9j/4QAYRXhpZ...............';
echo 'File Size: ' . strlen(base64_decode($img));

Don't forget to get rid of the headers ("data:image/jpeg", etc.) if you want to be more accurate.

Upvotes: 7

Related Questions