【问题标题】:Resize images in client side using uploadifive使用 uploadifive 在客户端调整图像大小
【发布时间】:2014-04-04 04:44:55
【问题描述】:

我已经使用uploadifive 实现了一个图片上传页面。有没有办法在客户端而不是在服务器端重新调整图像大小?如果图片可以在客户端重新调整大小,将减少上传图片的时间。

谁能给我一些例子。

【问题讨论】:

    标签: jquery image-uploading uploadifive


    【解决方案1】:

    最后我实现了图片调整大小功能并包含在uploadifive js中。

    我修改了 $data.uploadFile 方法(第 363 行)

    $data.uploadFile = function(file, uploadAll) {
        if (!file.skip && !file.complete && !file.uploading) {
            file.uploading = true;
            $data.uploads.current++;
            $data.uploads.attempted++;
    
            // Create a new AJAX request
            xhr = file.xhr = new XMLHttpRequest();
    
            // Start the upload
            // Use the faster FormData if it exists
            if (typeof FormData === 'function' || typeof FormData === 'object') {
                // Resizing starts
                var readerResize = new FileReader();
                readerResize.onload = function() {
                    // Create a new FormData object
                    var formData = new FormData();
    
                    var dataURL = methods.getResizedDataULR(readerResize, file.type);
    
                    // Convert resize dataURL into a blob again
                    var blob = methods.dataURItoBlob(dataURL);
    
                    // Append resized blob into form data with file name
                    formData.append(settings.fileObjName, blob, file.name);
    
                    // Add the rest of the formData
                    for (i in settings.formData) {
                        formData.append(i, settings.formData[i]);
                    }
    
                    // Open the AJAX call
                    xhr.open(settings.method, settings.uploadScript, true);
    
                    // On progress function
                    xhr.upload.addEventListener('progress', function(e) {
                        if (e.lengthComputable) {
                            $data.progress(e, file);
                        }
                    }, false);
    
                    // On complete function
                    xhr.addEventListener('load', function(e) {
                        if (this.readyState == 4) {
                            file.uploading = false;
                            if (this.status == 200) {
                                if (file.xhr.responseText !== 'Invalid file type.') {
                                    $data.uploadComplete(e, file, uploadAll);
                                } else {
                                    $data.error(file.xhr.responseText, file, uploadAll);
                                }
                            } else if (this.status == 404) {
                                $data.error('404_FILE_NOT_FOUND', file, uploadAll);
                            } else if (this.status == 403) {
                                $data.error('403_FORBIDDEN', file, uploadAll);
                            } else {
                                $data.error('Unknown Error', file, uploadAll);
                            }
                        }
                    });
    
                    // Send the form data (multipart/form-data)
                    xhr.send(formData);
                };
    
                readerResize.readAsDataURL(file);
    
                // Resizing ends
            } else {
    
                // Send as binary
                // Don't change this else block
    
            }
        }
    }  
    

    并添加了如下的辅助方法

    var methods = {
        .....
        // appended below new methods for resizing purpose
    
        dataURItoBlob: function (dataURI) {
            var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
            var binary = atob(dataURI.split(',')[1]), array = [];
            for (var i = 0; i < binary.length; i++) array.push(binary.charCodeAt(i));
            return new Blob([new Uint8Array(array)], { type: mimeString });
        },
    
        getResizedDataULR: function (reader, fileType) {
            var tempImg = new Image();
            tempImg.src = reader.result;
    
            var MAX_WIDTH = 800;
            var MAX_HEIGHT = 600;
            var tempW = tempImg.width;
            var tempH = tempImg.height;
            if (tempW > tempH) {
                if (tempW > MAX_WIDTH) {
                    tempH *= MAX_WIDTH / tempW;
                    tempW = MAX_WIDTH;
                }
            } else {
                if (tempH > MAX_HEIGHT) {
                    tempW *= MAX_HEIGHT / tempH;
                    tempH = MAX_HEIGHT;
                }
            }
    
            var canvas = document.createElement('canvas');
            canvas.width = tempW;
            canvas.height = tempH;
            var ctx = canvas.getContext("2d");
            ctx.drawImage(tempImg, 0, 0, tempW, tempH);
            return canvas.toDataURL(fileType);
        }
    }
    

    【讨论】:

    • 次要挑剔:getResizedDataULR 函数的名称起初有点令人困惑。试图弄清楚“ULR”是否是我从未听说过的新事物。当我意识到它应该是“URL”时,我感到很震惊。
    猜你喜欢
    • 2013-12-30
    • 2020-11-04
    • 2012-12-27
    • 2018-09-20
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    相关资源
    最近更新 更多