Jaydeep Mor
Jaydeep Mor

Reputation: 1703

Get image information from base64 encoded string

I have base64 encoded string (Encoded from image).

$str = "......";

Q : How to get encoded image information ?

Like,

1) Image name.

2) MIME type.

3) Image extension.

4) Image size.

I try below code from this url but it gives only MIME type not other information.

$encoded_string = "....";
$imgdata = base64_decode($encoded_string);

$f = finfo_open();

$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);

Many thanks.

Upvotes: 2

Views: 5031

Answers (1)

Jo.
Jo.

Reputation: 790

You can get information about the image using PHP's getimagesizefromstring function.

$encoded_string = "....";
$imgdata = base64_decode($encoded_string);

$data = getimagesizefromstring($imgdata);
print_r($data);

// outputs something like this
Array
(
    [0] => 544
    [1] => 184
    [2] => 3
    [3] => width="544" height="184"
    [bits] => 8
    [mime] => image/png
)

1) Base 64 encoded images will not have a filename

2) The mime type is given in the mime key, while key 2 contains one of the IMAGETYPE_ constants

3) You can use image_type_to_extension($data[2]) to get the proper image extension for the mime type

4) Image width is in key 0 and image height is in key 1.

Upvotes: 3

Related Questions