gpt4 book ai didi

javascript - 通过javascript循环上传多个 Canvas 图像

转载 作者:行者123 更新时间:2023-12-03 10:49:52 24 4
gpt4 key购买 nike

我正在尝试制作一个上传脚本,在 saveimage.php 处理多个图像之前调整客户端的大小。这样做的原因是因为它们将在互联网速度可能非常慢的外部位置上传。

我在这里找到了一些代码片段,帮助我将它们组合在一起。我只是一个初学者,所以可能很困惑!

它当前所做的是检查“file_input”字段中是否输入了文件。然后它会查看文件的数量并循环遍历它们,直到每个文件都被放置在 Canvas 中并在其中上传。

但是,问题是:当我不为每个文件添加警报时,循环速度太快,例如仅处理 10 个图像中的 1 或 2 个。因为上传脚本的运行速度比 Canvas 更新新图像的速度要快。

这是我当前的页面,已简化为核心:

<!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 );

}

?>

一些额外信息:

  • 我添加了 Canvas ,因为据我所知,这就是客户端调整图像大小的唯一方法。在最终的脚本中它将是设置为隐藏。
  • 是否可以跳过 Canvas 并将数据发送到saveimage.php我认为这也可以立即解决问题。
  • 正如我所说,我对 javascript 不太有经验,所以如果有实现这一目标的更简单的方法。请告诉我!

提前致谢!

最佳答案

JavaScript 异步加载图像。这意味着一旦分配了新图像的 .src,它将开始加载该图像,但同时会在 .src 之后继续处理 javascript,而图像需要一些时间来加载。

您在 for 循环中使用相同的图像变量 (var img)。因此,每次循环时,您都会在完全加载前一个图像之前覆盖前一个图像。

这是一个图像加载器,它完全加载所有图像,然后调用 start() 函数,您可以在其中进行处理:

// image loader

// put the paths to your images in imageURLs[]
var imageURLs=[];
imageURLs.push("myImage1.png");
imageURLs.push("myImage2.png");
// etc, etc.

// the loaded images will be placed in imgs[]
var imgs=[];

var imagesOK=0;
loadAllImages(start);

function loadAllImages(callback){
for (var i=0; i<imageURLs.length; i++) {
var img = new Image();
imgs.push(img);
img.onload = function(){
imagesOK++;
if (imagesOK>=imageURLs.length ) {
callback();
}
};
img.onerror=function(){alert("image load failed");}
img.crossOrigin="anonymous";
img.src = imageURLs[i];
}
}

function start(){

// the imgs[] array now holds fully loaded images
// the imgs[] are in the same order as imageURLs[]

}

关于javascript - 通过javascript循环上传多个 Canvas 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28458052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com