Php size base64_encode string - file

I am wondering how to find out the size of the base64_encoded line file? For instance:

$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl' . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr' . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r' . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg=='; $data = base64_decode($data); 

thanks

+6
php filesize
source share
4 answers
 strlen(base64_decode($encoded_data)); 

And as a general rule, base64 encoding increases the original data by about 33%

+11
source share

If you want to know the size without decoding, I believe that the following works:

 $size = (int) (strlen(rtrim($data, '=')) * 3 / 4); 

Or:

 $size = (strlen($data) * 3 / 4) - substr_count(substr($data, -2), '='); 

Otherwise, just use strlen() for the decoded data, as stated in Marc.

+7
source share

Try this, give the size in bytes, KB and MB too ..

 public function getBase64ImageSize($base64Image){ //return memory size in B, KB, MB try{ $size_in_bytes = (int) (strlen(rtrim($base64Image, '=')) * 3 / 4); $size_in_kb = $size_in_bytes / 1024; $size_in_mb = $size_in_kb / 1024; return $size_in_mb; } catch(Exception $e){ return $e; } } 
+1
source share

Use the following code:

 function getFileSizeInKb($base64string){ $bytes = strlen(base64_decode($base64string)); $roughsize = (((int)$bytes) / 1024.0)* 0.67; return round($roughsize,2); } 
-one
source share

All Articles