【问题标题】:Resizing an image before upload in HTML 5在 HTML 5 中上传之前调整图像大小
【发布时间】:2013-03-11 06:06:45
【问题描述】:

在将图像上传到服务器之前,我正在尝试调整文件上传中的图像大小。我找到了一个decent plugin,它返回更新图像的 blob,但是我找不到如何将 blob 分配回上传使用的文件对象。

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

谢谢!

【问题讨论】:

  • 你有一个数据:方案 URI。您可以将其发布到服务器,然后将其解码回那里的二进制文件。
  • 是的,您必须将 blob 作为文本发送回服务器,您将无法使用 blob 覆盖文件上传控件的值。

标签: javascript jquery image-processing html5-canvas


【解决方案1】:

如果您仍然坚持使用 canvasResize,还有很多其他的图像处理库。我以前使用过 Pixastic 效果很好,尽管它的开销比 canvasResize 大。不过是better documented

【讨论】:

    【解决方案2】:

    我最终只是将 blob 添加到表单中的隐藏字段并提交。将其保存为 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");
    

    【讨论】:

    猜你喜欢
    • 2011-04-16
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多