0

I am using the below code from the library https://github.com/antimatter15/jsgif. I tried to convert the code given below to a gif image file but could not achieve it.

    var encoder = new GIFEncoder();
            encoder.setRepeat(0);
            encoder.start();
            var st = '|100:167.1,D,Pencil,3,#000|100:167.1,U|100:167.1,D|102:163.1,M|135:125.1|206:97.1|289:99.1|323:134.1|3\
16:252.1|205:331.1|234:260.1|504:102.1|581:92.1|631:95.1|635:132.1|512:209.1,U|385:29.1,D,,,#66c|384:29.1,M|384:40.1|378:84.\
1|365:168.1|360:215.1|354:269.1|354:300.1|357:313.1|405:319.1|450:314.1|507:291.1|546:260.1|534:223.1|487:171.1|418:119.1|29\
0:131.1|220:201.1|168:284.1|157:339.1|170:353.1|256:308.1|348:212.1|390:118.1|399:77.1|397:68.1|298:82.1|176:168.1|90:265.1|\
70:290.1|70:292.1|250:150.1|465:14.1|594:-42.9|606:-39.9|550:29.1|442:170.1|335:322.1|258:430.1|258:430.1,U';
            var frames_array = st.split('|');
            $.each(frames_array,function(index,value) {
               encoder.addFrame(value);
                });
            encoder.finish();
            var binary_gif = encoder.stream().getData();
            var data_url = 'data:image/gif;base64,'+encode64(binary_gif);

What I get for data_url is as given below

data:image/gif;base64,fDEwMDoxNjcuMSxELFBlbmNpbCwzLCMwMDB8MTAwOjE2Ny4xLFV8MTAwOjE2Ny4xLER8MTAyOjE2My4xLE18MTM1OjEyNS4xfDIwNjo5Ny4xfDI4OTo5OS4xfDMyMzoxMzQuMXwzMTY6MjUyLjF8MjA1OjMzMS4xfDIzNDoyNjAuMXw1MDQ6MTAyLjF8NTgxOjkyLjF8NjMxOjk1LjF8NjM1OjEzMi4xfDUxMjoyMDkuMSxVfDM4NToyOS4xLEQsLCwjNjZjfDM4NDoyOS4xLE18Mzg0OjQwLjF8Mzc4Ojg0LjF8MzY1OjE2OC4xfDM2MDoyMTUuMXwzNTQ6MjY5LjF8MzU0OjMwMC4xfDM1NzozMTMuMXw0MDU6MzE5LjF8NDUwOjMxNC4xfDUwNzoyOTEuMXw1NDY6MjYwLjF8NTM0OjIyMy4xfDQ4NzoxNzEuMXw0MTg6MTE5LjF8MjkwOjEzMS4xfDIyMDoyMDEuMXwxNjg6Mjg0LjF8MTU3OjMzOS4xfDE3MDozNTMuMXwyNTY6MzA4LjF8MzQ4OjIxMi4xfDM5MDoxMTguMXwzOTk6NzcuMXwzOTc6NjguMXwyOTg6ODIuMXwxNzY6MTY4LjF8OTA6MjY1LjF8NzA6MjkwLjF8NzA6MjkyLjF8MjUwOjE1MC4xfDQ2NToxNC4xfDU5NDotNDIuOXw2MDY6LTM5Ljl8NTUwOjI5LjF8NDQyOjE3MC4xfDMzNTozMjIuMXwyNTg6NDMwLjF8MjU4OjQzMC4xLFU=‏

I need to convert this encoded code to an gif image file. How can I achieve it further

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
harshal
  • 10,252
  • 3
  • 18
  • 23
  • 1
    $pic = 'sample.gif'; $org_upload_path = "./upload/"; $image_location = $org_upload_path.$pic; file_put_contents("$image_location", base64_decode($img)); – kamlesh.bar Apr 07 '15 at 10:39
  • Thanks, @kamlesh.bar but I am using the above code in js which is base_64 encoded and I can pass that to php but I dont have any idea of how I can convert that to an image file, can u please help me out with a code which will convert base64_encoded string into image gif? – harshal Apr 07 '15 at 10:41
  • 1
    as i mention above to convert encoded string into file you can just do file_put_contents("upload/sample.gif", base64_decode($img)); http://stackoverflow.com/questions/15153776/convert-base64-string-to-an-image-file – kamlesh.bar Apr 07 '15 at 10:43

4 Answers4

1

try this , may this will help you

<?php
function base64url_decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4,     '=', STR_PAD_RIGHT));
}
?>
anand kulkarni
  • 156
  • 1
  • 12
1

Use this (PHP pseudo-code): http://php.net/manual/en/function.imagecreatefromstring.php

function imagegif_from_base64($base64)
{
    return imagecreatefromstring(base64_decode(substr($base64, 22) /* remove the "data:image/gif;base64," string */));
}

related get image from base64 string

Community
  • 1
  • 1
Nikos M.
  • 8,033
  • 4
  • 36
  • 43
1

AS you are using jsgif library you You can do like this to decode image

    $imgstr = 'data:image/gif;base64,fDEwMDoxNjcuMSxELFBlbmNpbCwzLCMwMDB8MTAwOjE2Ny4xLFV8MTAwOjE2Ny4xLER8MTAyOjE2My4xLE18MTM1OjEyNS4xfDIwNjo5Ny4xfDI4OTo5OS4xfDMyMzoxMzQuMXwzMTY6MjUyLjF8MjA1OjMzMS4xfDIzNDoyNjAuMXw1MDQ6MTAyLjF8NTgxOjkyLjF8NjMxOjk1LjF8NjM1OjEzMi4xfDUxMjoyMDkuMSxVfDM4NToyOS4xLEQsLCwjNjZjfDM4NDoyOS4xLE18Mzg0OjQwLjF8Mzc4Ojg0LjF8MzY1OjE2OC4xfDM2MDoyMTUuMXwzNTQ6MjY5LjF8MzU0OjMwMC4xfDM1NzozMTMuMXw0MDU6MzE5LjF8NDUwOjMxNC4xfDUwNzoyOTEuMXw1NDY6MjYwLjF8NTM0OjIyMy4xfDQ4NzoxNzEuMXw0MTg6MTE5LjF8MjkwOjEzMS4xfDIyMDoyMDEuMXwxNjg6Mjg0LjF8MTU3OjMzOS4xfDE3MDozNTMuMXwyNTY6MzA4LjF8MzQ4OjIxMi4xfDM5MDoxMTguMXwzOTk6NzcuMXwzOTc6NjguMXwyOTg6ODIuMXwxNzY6MTY4LjF8OTA6MjY1LjF8NzA6MjkwLjF8NzA6MjkyLjF8MjUwOjE1MC4xfDQ2NToxNC4xfDU5NDotNDIuOXw2MDY6LTM5Ljl8NTUwOjI5LjF8NDQyOjE3MC4xfDMzNTozMjIuMXwyNTg6NDMwLjF8MjU4OjQzMC4xLFU=‏';
    $new_data=explode(";",$imgstr);
    $type=$new_data[0];
    $imgdata=explode(",",$new_data[1]);
    $imgstr = base64_decode($imgdata[1]);

    // or choose any name instead sample.gif
    file_put_contents("sample.gif", base64_decode($imgstr));
kamlesh.bar
  • 1,774
  • 19
  • 38
0

If you just wanna use PHP try this:

$str = 'data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAA...';
$data = str_replace('data:image/gif;base64,', '', $str);
$data = str_replace(' ', '+', $data);

$data = base64_decode($data); // base64 decoded image data
$source_img = imagecreatefromstring($data);
$file = uniqid() .'.gif';
$success = file_put_contents($file, $data);

This will save the .gif image in the same folder the php file is located.

Sascha
  • 74
  • 2