Faced with such a problem, I can not convert base64 gif string to gif file and save it to disk, with base64 strings getting get its MIME types - application / octet-stream (video streaming), insert the following code (below) and get this error - " imagecreatefromstring (): Data is not in a recognized format in ", I do not know what to do.
$img = base64_decode($photo);
$f = finfo_open();
$mime_type = finfo_buffer($f, $img, FILEINFO_MIME_TYPE);
if($mime_type !== ImageHelper::MIME_TYPE_PNG
&& $mime_type !== ImageHelper::MIME_TYPE_JPEG
&& $mime_type !== ImageHelper::MIME_TYPE_STREAM) {
throw new HttpException(400);
}
if($mime_type == ImageHelper::MIME_TYPE_STREAM) { // image gif
$name = ImageHelper::generateImageName() . '.gif';
$path = $prefixPath . $name;
$img_handler = imagecreatefromstring($img);
if($img_handler !== false) {
imagegif($img_handler, $path);
imagedestroy($img_handler);
} else {
throw new HttpException(400);
}
}
example string data:image/gif;base64,R0lGODlhuQBpANQdASAgQEIgQCBOQEJYQIk/QCCAQFqMQGDAQICYQMuLQIvCQMDWQCAggEsggCBJgE1ZgEpgwIJfgmCNgGDAgFyCwIWNg...
Update: Interestingly, this question when I convert gif file, I get the format 'application / octet-stream', rather than 'image / gif' why? I can not find the answer ...