是否有任何开源算法可以在Web上找到?
<input type="file" id="select">
<img id="preview">
<script>
document.getElementById('select').onchange = function(evt) {
ImageTools.resize(this.files[0], {
width: 320, // maximum width
height: 240 // maximum height
}, function(blob, didItResize) {
// didItResize will be true if it managed to resize it, otherwise false (and will return the original file as 'blob')
document.getElementById('preview').src = window.URL.createObjectURL(blob);
// you can also now upload this blob using an XHR.
});
};
</script>
我加入了许多支持检测和填充以确保它能在尽可能多的浏览器上正常工作。
(它也会忽略 GIF 图像 - 以防它们是动画的)
imageSmoothingEnabled
设置为true,并将imageSmoothingQuality
设置为high
。在TypeScript中,该代码块如下所示:const ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = true;
(ctx as any).imageSmoothingQuality = 'high';
ctx.drawImage(image, 0, 0, width, height);
- Sean Perkins答案是肯定的 - 在HTML 5中,您可以使用画布元素(client-side)调整图像大小。您还可以获取新数据并将其发送到服务器。请参阅以下教程:
http://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
如果您需要在上传之前进行调整大小,我刚刚发现了这个http://www.plupload.com/
它可以用任何想象得到的方法为您自动完成所有工作。
不幸的是,HTML5仅在Mozilla浏览器中支持调整大小,但您可以将其他浏览器重定向到Flash和Silverlight。
我刚试了一下,它在我的安卓手机上运行良好!
我曾经使用过http://swfupload.org/中的Flash,它也很好地完成了工作,但是调整大小的尺寸非常小(无法记住限制),并且在没有Flash可用的情况下不能回到HTML4。
http://nodeca.github.io/pica/demo/
现代浏览器可以使用画布(canvas)来加载/保存图像数据。但是,如果在客户端调整图像大小,应注意以下几点:
/***********************************************
* GLOBAL VARS
**********************************************/
var image = new MarvinImage();
/***********************************************
* FILE CHOOSER AND UPLOAD
**********************************************/
$('#fileUpload').change(function (event) {
form = new FormData();
form.append('name', event.target.files[0].name);
reader = new FileReader();
reader.readAsDataURL(event.target.files[0]);
reader.onload = function(){
image.load(reader.result, imageLoaded);
};
});
function resizeAndSendToServer(){
$("#divServerResponse").html("uploading...");
$.ajax({
method: 'POST',
url: 'https://www.marvinj.org/backoffice/imageUpload.php',
data: form,
enctype: 'multipart/form-data',
contentType: false,
processData: false,
success: function (resp) {
$("#divServerResponse").html("SERVER RESPONSE (NEW IMAGE):<br/><img src='"+resp+"' style='max-width:400px'></img>");
},
error: function (data) {
console.log("error:"+error);
console.log(data);
},
});
};
/***********************************************
* IMAGE MANIPULATION
**********************************************/
function imageLoaded(){
Marvin.scale(image.clone(), image, 120);
form.append("blob", image.toBlob());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.marvinj.org/releases/marvinj-0.8.js"></script>
<form id="form" action='/backoffice/imageUpload.php' style='margin:auto;' method='post' enctype='multipart/form-data'>
<input type='file' id='fileUpload' class='upload' name='userfile'/>
</form><br/>
<button type="button" onclick="resizeAndSendToServer()">Resize and Send to Server</button><br/><br/>
<div id="divServerResponse">
</div>
根据我的经验,这个例子是上传调整大小图片最好的解决方案:
https://zocada.com/compress-resize-images-javascript-browser/它使用了HTML5 Canvas功能。
代码就是这么“简单”:
compress(e) {
const fileName = e.target.files[0].name;
const reader = new FileReader();
reader.readAsDataURL(e.target.files[0]);
reader.onload = event => {
const img = new Image();
img.src = event.target.result;
img.onload = () => {
const elem = document.createElement('canvas');
const width = Math.min(800, img.width);
const scaleFactor = width / img.width;
elem.width = width;
elem.height = img.height * scaleFactor;
const ctx = elem.getContext('2d');
// img.width and img.height will contain the original dimensions
ctx.drawImage(img, 0, 0, width, img.height * scaleFactor);
ctx.canvas.toBlob((blob) => {
const file = new File([blob], fileName, {
type: 'image/jpeg',
lastModified: Date.now()
});
}, 'image/jpeg', 1);
},
reader.onerror = error => console.log(error);
};
}
这种解决方案有两个缺点。
第一个与图像旋转有关,因为忽略了EXIF数据。我无法解决这个问题,在我的使用情况中也不是很重要,但很乐意听取任何反馈意见。
第二个缺点是不支持IE/Edge(尽管支持基于Chrome的版本),我不会在这上面花费任何时间。
//File destination
$destination = "/folder/cropped_image.png";
//Get uploaded image file it's temporary name
$image_tmp_name = $_FILES["cropped_image"]["tmp_name"][0];
//Move temporary file to final destination
move_uploaded_file($image_tmp_name, $destination);
如果你担心Vitaly的观点,你可以在正在工作的jfiddle上尝试一些裁剪和调整大小的方法。