Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php size of base64_encode string - file

Tags:

php

filesize

I'm wondering how could I know the file size of a base64_encoded string? For instance:

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

Thanks

like image 840
synonym Avatar asked Mar 21 '11 03:03

synonym


2 Answers

strlen(base64_decode($encoded_data));

And as a rough rule of thumb, base64 encoding increases the original data in size by about 33%

like image 145
Marc B Avatar answered Oct 06 '22 19:10

Marc B


Try this one, gives you 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;
    }
}
like image 21
Shahrukh Anwar Avatar answered Oct 06 '22 18:10

Shahrukh Anwar