PHP - Image to base64 and base64 to image file

PHP - Image to base64 and base64 to image file

參考兩個資料:

轉成Base64格式

https://stackoverflow.com/questions/3967515/how-to-convert-an-image-to-base64-encoding

Base64轉成Image

https://stackoverflow.com/questions/11511511/how-to-save-a-png-image-server-side-from-a-base64-data-string

$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$data = 'data:image/png;base64,AAAFBfj42Pj4';

list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);

file_put_contents('/tmp/image.png', $data);

及取得檔案大小

filesize($filePath);