【发布时间】:2015-04-30 16:05:26
【问题描述】:
我正在尝试编写我的代码,但我没有成功。 基本上我想要的是将调整大小算法应用于用户选择的多个图像,使它们在 HTML5 Canvas 中,然后将每个字符串(DataURL)插入输入数组中以通过 POST 方法发送。
我的代码(客户端)index.html:
<html>
<head>
</head>
<body>
<input type="file" id="pictures" multiple><br>
<form id="frm" action="save.php" method="POST">
<input type="hidden" name="sizedpics[]" id="sizedpics">
<button type="button" onclick="resize()">submit!</button>
</form>
<canvas style="display:none;" id="canvas" width="800"></canvas><br>
<script>
var pics=[];
function resize()
{
var numpictures=pictures.files.length;
var compresion=0.5;
var dataurl;
for (i=0;i<numpictures;i++)
{
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
img = new Image();
img.onload = function ()
{
canvas.height = canvas.width * (img.height / img.width);
/// step 1
var oc = document.createElement('canvas'), octx = oc.getContext('2d');
oc.width = img.width * compresion;
oc.height = img.height * compresion;
octx.drawImage(img, 0, 0, oc.width, oc.height);
/// step 2
octx.drawImage(oc, 0, 0, oc.width * compresion, oc.height * compresion);
ctx.drawImage(oc, 0, 0, oc.width * compresion, oc.height * compresion, 0, 0, canvas.width, canvas.height);
// Step 3
dataurl = canvas.toDataURL("image/jpeg");
pics.push(dataurl);
}
img.src = window.URL.createObjectURL(pictures.files[i]);
}
document.getElementById("sizedpics").value=pics;
document.forms["frm"].submit();
}
</script>
</body>
</html>
我的代码(服务器)save.php
<?php
print_r(count($_POST['sizedpics']));exit();
?>
我认为与 javascript 同步存在问题,但我不知道如何解决,我需要使用单个输入“sizedpics”将所有图像(base64 编码的 DataURL 字符串)发送到我的服务器。
你们中的一些人知道错误在哪里吗? 感谢您的阅读,请原谅我写英语的糟糕方式:-)
【问题讨论】:
标签: javascript html canvas asynchronous html-input