【发布时间】:2015-04-12 01:01:13
【问题描述】:
我正在尝试制作一个上传脚本,在 saveimage.php 处理它们之前调整多个图像客户端的大小。这样做的原因是因为它们将被上传到互联网可能非常慢的外部位置。
我已经能够在这里找到帮助我将其组合在一起的代码片段。我只是一个初学者,所以可能很乱!
它目前所做的是检查何时在“file_input”字段中输入文件。然后它会查看文件的数量并循环遍历它们,直到每个文件都放在画布中并在画布中上传。
但是,问题是:当我不为每个文件添加警报时,循环速度太快,例如仅处理 10 个图像中的 1 个或 2 个。因为上传脚本会比画布更新新图片的速度更快。
这是我当前的页面,简化为核心:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
</head>
<body>
<form>
<input id="file_input" type='file' multiple />
</form>
<div id="preview">
<canvas id="canvas"></canvas>
</div>
<script>
var input = document.getElementById('file_input');
input.addEventListener('change', handleFiles);
function handleFiles(e) {
var files = input.files;
alert(files.length +" files are being uploaded!"); // display amount of files for testing purpose
for (var i = 0; i < files.length; ++i) {
alert("Upload file number: "+i); // display file # for testing purpose, when this is removed the script will go too fast
var MAX_HEIGHT = 1000;
var ctx = document.getElementById('canvas').getContext('2d');
var img = new Image;
img.onload = function(){
if(img.height > MAX_HEIGHT) {
img.width *= MAX_HEIGHT / img.height;
img.height = MAX_HEIGHT;
}
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
preview.style.width = img.width + "px";
preview.style.height = img.height + "px";
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
// the canvas should contain an image now and this will send it through saveimage.php
var myDrawing = document.getElementById("canvas");
var drawingString = myDrawing.toDataURL("image/jpeg", 0.5);
var postData = "canvasData="+drawingString;
var ajax = new XMLHttpRequest();
ajax.open("POST",'saveimage.php',true);
ajax.setRequestHeader('Content-Type', 'canvas/upload');
ajax.onreadystatechange=function()
{
if (ajax.readyState == 4);
}
ajax.send(postData);
};
img.src = URL.createObjectURL(e.target.files[i]);
}
}
</script>
</body>
</html>
这是我的 saveimage.php 效果很好,但只是为了完整的概述:
<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
$rawImage=$GLOBALS['HTTP_RAW_POST_DATA'];
$removeHeaders=substr($rawImage, strpos($rawImage, ",")+1);
$decode=base64_decode($removeHeaders);
// check if the file already exists and keep adding +1 intil it's unique
$i = 1;
while(file_exists('uploads/image'.$i.'.jpg')) {
$i++;
}
$fopen = fopen( 'uploads/image'.$i.'.jpg', 'wb' );
fwrite( $fopen, $decode);
fclose( $fopen );
}
?>
一点额外信息:
- 我添加了画布,因为据我所知 调整图像客户端大小的唯一方法。在最终脚本中,它将是 设置为隐藏。
- 如果可以跳过画布并将数据发送到 saveimage.php 我认为这也可以立即解决问题。
- 正如我所说,我对 javascript 不是很有经验,所以如果有 实现这一目标的简单得多的方法。请告诉我!
提前致谢!
【问题讨论】:
-
在做任何其他事情之前:设置 onload 处理程序 before 设置 src。
-
@GameAlchemist 我现在把 onload 处理程序放在了 src 之前,它似乎仍然以同样的方式运行,这是什么原因?
标签: javascript php image canvas upload