简体   繁体   English

在HTML 5中上载之前调整图像大小

[英]Resizing an image before upload in HTML 5

I'm trying to resize an image in a file upload before uploading the image to the server. 我正在尝试在将图像上传到服务器之前调整文件上传中的图像大小。 I found a decent plugin that returns the blob of the updated image, however I cannot find how to assign the blob back to the file object being used by the upload. 我发现了一个很好的插件 ,可以返回更新后的图像的blob,但是我找不到如何将blob分配回上传所使用的文件对象。

The blob looks like: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB..... blob看起来像:data:image / jpeg; base64,/ 9j / 4AAQSkZJRgABAQAAAQAB .....

        var file = e.target.files[0];
        canvasResize(file, {
            width: 300,
            height: 0,
            crop: false,
            quality: 80,
            //rotate: 90,
            callback: function (data, width, height) {
                // How to assign the data blob back to the file?
                //$(file).attr('src', data);

             // THEN submit with the smaller photo   
             $('#PhotoForm').ajaxSubmit(options);
            }
        });

Thanks! 谢谢!

If you remain stuck with canvasResize, there are a lot of other image manipulating libraries. 如果你仍然坚持使用canvasResize,那么还有很多其他的图像处理库。 I've used Pixastic to great effect before, though it has a larger overhead than canvasResize. 我以前使用Pixastic效果很好,虽然它比canvasResize有更大的开销。 It is better documented though. 但是更好的记录

I ended up just adding the blob to a hidden field in the form and submitting it. 我最后只是将blob添加到表单中的隐藏字段并提交它。 Saving that as an image from the C# was accomplished with the following: 将其保存为C#中的图像是通过以下方式完成的:

        var regex = new Regex(@"data:(?<mime>[\w/]+);(?<encoding>\w+),(?<data>.*)", RegexOptions.Compiled);
        var match = regex.Match(input.ImageBlob);
        var mime = match.Groups["mime"].Value;
        var encoding = match.Groups["encoding"].Value;
        var data = match.Groups["data"].Value;
        byte[] imagedata = Convert.FromBase64String(data);

        Image img = byteArrayToImage(imagedata);

        img.Save("someimagename.jpg");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM