【发布时间】: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.php 以base64 格式创建,然后从 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.php 与 webcam_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 >= 4 * 320 * 240)的问题? -
@MichaelBerkowski 这就是我试图解释的。
var_dump($_POST)没有显示任何内容。 -
您是否在浏览器网络控制台中看到任何对 webcam_image_ajax.php 的调用?你能在
$.post()应该发生之前在浏览器控制台中插入断点吗? -
@MichaelBerkowski 我正在测试肯定总是在那里。我什么也得不到。没有错误,同时没有发布数据。
-
它显示了 gifshot.js 创建的 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABE...
标签: javascript php jquery ajax