【问题标题】:imagecreatefromgif() from base64 encoded animated gif in POSTPOST 中 base64 编码动画 gif 的 imagecreatefromgif()
【发布时间】:2015-02-05 10:01:33
【问题描述】:

我正在尝试使用GifShot 插件从媒体流、视频或图像制作动画 GIF。

我的问题是 ajax 部分看不到webcam_image_ajax.php。不工作。请不要恨我,所以问题会更长一点。

我必须创建这个 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();
}

你可以在我的 ajax 函数中看到这段代码:

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

webcam_image_ajax.phpbase64 格式创建,然后从 images 文件夹上传 gif 图像。

此外,当单击创建 GIF 按钮时,此 JavaScript 将启动:CLICK

之后我的 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']."'/>

";
}
?>

webcam_imageload_ajax.phpwebcam_image_ajax.php 合作。

如果webcam_image_ajax.php 创建了图像,则webcam_imageload_ajax.php 回显图像,例如:

upload/14202558.gif

但现在看起来像:

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

创建一个 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" />

【问题讨论】:

  • 当JS调用webcam_image_ajax.php时,请将var_dump($_POST)的输出放到webcam_image_ajax.php中。还是$.post() 内部永远不会出现if (pos &gt;= 4 * 320 * 240) 的问题?
  • @MichaelBerkowski 这就是我试图解释的。 var_dump($_POST) 没有显示任何内容。
  • 您是否在浏览器网络控制台中看到任何对 webcam_image_ajax.php 的调用?你能在$.post()应该发生之前在浏览器控制台中插入断点吗?
  • @MichaelBerkowski 我正在测试肯定总是在那里。我什么也得不到。没有错误,同时没有发布数据。
  • 它显示了 gifshot.js 创建的 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABE...

标签: javascript php jquery ajax


【解决方案1】:

忘记问题中的 JavaScript 代码。

如果您想使用此脚本,请在 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);
            } 
        }
    );
}

你可以为webcam_image_ajax.php编写你自己的php代码。

【讨论】:

    【解决方案2】:

    只需这样做:

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

    这只是将您的data:... 调整为data:// wrapper

    没有更简单的方法可以做到这一点。

    请注意,这是非常不安全,您应该在使用前验证数据(例如使用preg_match)。

    【讨论】:

    • 感谢您的回答,但问题不在 php.ini 中。问题出在javascript代码中。此代码在我的 javascript 中不起作用:$.post("webcam_image_ajax.php", {type: "data", gif: createGIFButton.toDataURL("image/gif")}, 由于第 1103 行的THIS
    • 根据你的问题,引用标题,上面写着“How to save a GIF image server-side, from a base64 data string”,我给出的答案实际上是正确的。此外,这条线非常好,它完全按照它的意思做:它返回格式为data:&lt;mimetype&gt;;base64,&lt;base64 data&gt;的字符串。
    • 你的答案是正确的,但显然我做错了。
    • 我会把你所有的webcam_image_ajax.php 扔掉。我会提供你应该做的方式。此外,您正在尝试提取二进制数据并将其转换为像素,这是行不通的。它适用于 bmp 图像,但您必须占用 3 个字节,颠倒字节顺序,然后将字符代码用于像素数据。这很耗时。稍后我会为你重写部分代码。
    • 好的。那么你能告诉我我需要删除我在webcam_image_ajax.php 中的所有代码,然后我需要正确地传递你的代码吗?
    猜你喜欢
    • 2014-04-03
    • 1970-01-01
    • 2018-08-16
    • 2012-06-12
    • 2016-04-27
    • 2014-04-23
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    相关资源
    最近更新 更多