简体   繁体   English

POST中来自base64编码的动画gif的imagecreatefromgif()

[英]imagecreatefromgif() from base64 encoded animated gif in POST

I am trying to make animated GIFs from media streams, videos, or images using the GifShot plugin. 我正在尝试使用GifShot插件从媒体流,视频或图像制作GIF动画。

My problem is that the ajax part does not see webcam_image_ajax.php . 我的问题是ajax部分看不到webcam_image_ajax.php isn't working. 不起作用。 Please do not hate me so the question will be a little longer. 请不要恨我,这样的问题会更长一些。

I have to create this ajax function for uploading image: 我必须创建此ajax函数来上传图像:

    var pos = 0, ctx = null, saveCB, gif = [];
    var createGIFButton = document.createElement("canvas");
    createGIFButton.setAttribute('width', 320);
    createGIFButton.setAttribute('height', 240);
    if (createGIFButton.toDataURL) 
    {
    ctx = createGIFButton.getContext("2d");
    gif = ctx.getImageData(0, 0, 320, 240);
    saveCB = function(data) 
    {
    var col = data.split(";");
    var img = gif;
    for(var i = 0; i < 320; i++) {
    var tmp = parseInt(col[i]);
    img.data[pos + 0] = (tmp >> 16) & 0xff;
    img.data[pos + 1] = (tmp >> 8) & 0xff;
    img.data[pos + 2] = tmp & 0xff;
    img.data[pos + 3] = 0xff;
    pos+= 4;
    }
    if (pos >= 4 * 320 * 240)
     {
    ctx.putImageData(img, 0, 0);
    $.post("webcam_image_ajax.php", {type: "data", gif: createGIFButton.toDataURL("image/gif")},

    function(data)
     {

     if($.trim(data) != "false")
    {
    var dataString = 'webcam='+ 1;
    $.ajax({
    type: "POST",
    url: $.base_url+"webcam_imageload_ajax.php",
    data: dataString,
    cache: false,
    success: function(html){
    var values=$("#uploadvalues").val();
    $("#webcam_preview").prepend(html);
    var M=$('.webcam_preview').attr('id');
    var T= M+','+values;
    if(T!='undefinedd,')
    $("#uploadvalues").val(T);
     }
     });
     }
     else
    {
      $("#webcam").html('<div id="camera_error"><b>Camera could not connect.</b><br/>Please be sure to make sure your camera is plugged in and in use by another application.</div>');
    $("#webcam_status").html("<span style='color:#cc0000'>Camera not found please try again.</span>");
    $("#webcam_takesnap").hide();
        return false;
    }
     });
    pos = 0;
     }
      else {
    saveCB = function(data) {
    gif.push(data);
    pos+= 4 * 320;
     if (pos >= 4 * 320 * 240)
     {
    $.post("webcam_image_ajax.php", {type: "pixel", gif: gif.join('|')},
    function(data)
     {

    var dataString = 'webcam='+ 1;
    $.ajax({
    type: "POST",
    url: "webcam_imageload_ajax.php",
    data: dataString,
    cache: false,
    success: function(html){
    var values=$("#uploadvalues").val();
    $("#webcam_preview").prepend(html);
    var M=$('.webcam_preview, .gifshot-image-preview-section').attr('id');
    var T= M+','+values;
    if(T!='undefined,')
    $("#uploadvalues").val(T);
     }
     });

     });
     pos = 0;
     }
     };
     }
     };
     }
$("#webcam").webcam({
width: 320,
height: 240,
mode: "callback",
swffile: "js/jscam_canvas_only.swf",
onSave: saveCB,
onCapture: function ()
{
webcam.save();
},
debug: function (type, string) {
$("#webcam_status").html(type + ": " + string);
}

});


});
/**Taking snap**/
function takeSnap(){
webcam.capture();
}

You can see this code in my ajax function: 您可以在我的ajax函数中看到以下代码:

$.post("webcam_image_ajax.php", {type: "data", gif: createGIFButton.toDataURL("image/gif")},

the webcam_image_ajax.php is created in base64 format and then it upload the gif image from the images folder. webcam_image_ajax.phpbase64格式创建,然后从images文件夹上载gif图像。

Also when clicked Create GIF button this JavaScript will starting: CLICK . 同样,当单击创建GIF按钮时,此JavaScript也将启动: CLICK

After that my ajax code have this line webcam_imageload_ajax.php 之后,我的ajax代码具有以下内容: webcam_imageload_ajax.php

<?php
include_once 'includes.php';
if(isSet($_POST['webcam']))
{
$newdata=$Wall->Get_Upload_Image($uid,0);
echo "<img src='uploads/".$newdata['image_path']."'  class='webcam_preview gifshot-image-preview-section' id='".$newdata['id']."'/>

";
}
?>

the webcam_imageload_ajax.php working with webcam_image_ajax.php . 使用webcam_imageload_ajax.php webcam_image_ajax.php

If webcam_image_ajax.php created image then webcam_imageload_ajax.php echoing image like: 如果webcam_image_ajax.php创建了图像,则webcam_imageload_ajax.php回显图像如下:

upload/14202558.gif

But now it looks like: 但是现在看起来像:

data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAABE... 数据:image / gif; base64,iVBORw0KGgoAAAANSUhEUgAABE ...

creat a gif button: 创建一个gif按钮:

<button type="button" id="create-gif" class="btn btn-large btn-primary create-gif-button camclick" onclick="return takeSnap();">Create GIF</button>
<input type="hidden" id="webcam_count" />

Forget the JavaScript code in the question. 忘记问题中的JavaScript代码。

If you want to use this script then use this code from demo.js inside in gifshot plugin. 如果要使用此脚本, demo.js在gifshot插件中使用demo.js中的代码。

function create_gif(data){
    $.post(
        "webcam_image_ajax.php", 
        {
            data: data, 
            dataType: 'json'
        },
        function(js_data)
        {
            var js_d = $.parseJSON(js_data);    
            $('#gif_preview').attr('src', js_d.path);
            if(js_d.id != 'error'){
                    $("#uploadvalues").val(js_d.id);
                    $('.webcam_preview, .gifshot-image-preview-section').attr('id', js_d.id);
            } 
        }
    );
}

and you can write your own php code for webcam_image_ajax.php . 您可以为webcam_image_ajax.php编写自己的php代码。

Simply do like this: 只需这样做:

file_put_contents('filename',file_get_contents(str_replace('data:','data://','<your base64 encoded data>')));

This is simply adapting your data:... into the data:// wrapper . 这只是将您的data:...改编为data://包装器

There is no simpler way to do this. 没有更简单的方法可以做到这一点。

Notice that this is HIGHLY UNSECURE and you should validate the data (using preg_match for example) before usage. 请注意,这是高度不安全的 ,您应该在使用之前验证数据(例如,使用preg_match )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM