1

The data the user is sending is not an image file but its content. How can I get its size without making it a file and use filesize and something similar?

The content of the image as an example:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABLAAAASwCAYAA......

Is it possible to create a formula to calculate the size of the image content? something like strlen($imageContent).

kodfire
  • 1,612
  • 3
  • 18
  • 57

2 Answers2

1

You need to use the 8bit encoding on mb_strlen:

$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABLAAAASwCAYAA......';
list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);

$size = mb_strlen($data, '8bit');
Maarten Veerman
  • 1,546
  • 1
  • 8
  • 10
0

You can make a file and then rapidly deleted.

$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABLAAAASwCAYAA......';
list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);
file_put_contents('/tmp/image.png', $data);

$size = getimagesize('/tmp/image.png');
unlink(/tmp/image.png);