4

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 ...

user2759544
  • 181
  • 1
  • 10
  • Are you removing `data:image/gif;base64,` from the start of the string before feeding it into `base64_decode()` ? – Scuzzy Feb 20 '16 at 23:39
  • @Scuzzy It's strange, when I work with the formats jpeg png I did not delete this line, it should be removed? – user2759544 Feb 20 '16 at 23:40
  • Yes, you could try... `base64_decode(end(explode(',',$photo))` (I'm sure there's a better strrpos/substr way but I'm feeling lazy) – Scuzzy Feb 20 '16 at 23:42
  • @Scuzzy there are no errors, but the animation does not work, only the first frame appeared and nothing moves. ( – user2759544 Feb 20 '16 at 23:48
  • @Scuzzy I need to maintain a dynamic gif. I think this is unrealistic)) – user2759544 Feb 20 '16 at 23:53
  • What is the purpose of the image functions here anyway? It appears you're loading the gif and then printing it without any image processing? – Scuzzy Feb 21 '16 at 21:37
  • @Scuzzy I have a moving gif, I have to pour it on a server that when inserted in the article, he also moved – user2759544 Feb 21 '16 at 23:30

0 Answers0