I'm currently using Gifshot.js to create user avatars on my site. I want to be able to store the GIF files server side, but am unable to get it to work. The question is how do I properly decode a base64 encoded GIF file, to store via PHP? Also, is there a way to upload the files as incremented numbers, instead of a static name? Here is what I currently have for code:
Javascript:
$("#savegif").click(function(){
gifshot.createGIF({
interval: 0.1,
numFrames: 25,
}, function (obj) {
if (!obj.error) {
var image = obj.image,
animatedImage = document.getElementById('animatedgif');
animatedImage.src = image;
var data = animatedImage.src;
$.ajax({
url: "gifsave.php",
method: "POST",
dataType: "text",
data: { data }
});
}
});
});
PHP:
$file = $_POST['data'];
$img = str_replace('data:image/gif;base64,', '', $file);
file_put_contents('images/image.gif', base64_decode($img));
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…