Angularjs上传文件和文件阅读器来调整图像大小?

时间:2014-05-13 15:43:29

标签: javascript html5 angularjs canvas filereader

我正在使用angularjs文件上传angularjs-file-upload将文件上传到服务器端。在下面的过程中,我正在检查图像大小的宽度和高度以及html5文件阅读器和画布,以便从同一个文件创建不同的大小。但是,我对onloadend事件有一些问题。

$scope.onFileSelect = function ($files) {
    file = $files[0];
    $scope.loading = true;
    //$files: an array of files selected, each file has name, size, and type.

    img = new Image();
    img.onload = function () {
        var self = this;
        if (self.width > sizes.width && self.height > sizes.height) {
            var i, len = Images.channel.length;
            for (i = 0; i < len; i++) {
                var size = Images.channel[i];
                var reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onloadend = function () {
                    var tempImg = new Image();
                    tempImg.src = reader.result;
                    tempImg.onload = function () {
                        var canvas = document.createElement('canvas');
                        canvas.width = size.width;
                        canvas.height = size.height;
                        var ctx = canvas.getContext("2d");
                        ctx.drawImage(this, 0, 0, size.width, size.height);
                        var dataURL = canvas.toDataURL("image/jpeg");
                        //extract data from urlString
                        var n = dataURL.indexOf(",");
                        var data = dataURL.toString().substring(n); //we skip the ',' symbol used by navigator to detect canvas text
                        var imageFile = new Blob([data], { type: "plain/text"});
                        $scope.$apply(function () {
                            $uploadWrapper(pinholeAdminServerRoutes.image.upload,
                                imageFile,
                                {   "operationType": size.operationType,
                                    "objectId": $scope.channel.id,
                                    "size": size.label
                                }, function (response) {
                                    $scope.loading = false;
                                }, function (error) {
                                    $scope.loading = false;
                                });
                        });
                    };
                };
            }
        } else {
            $scope.$apply(function () {
                $scope.imageSizeNotValid = true;
                $scope.loading = false;
            });
        }
    };
    img.src = _URL.createObjectURL(file);
};

最终结果是以下代码只会上传最终图像。

1 个答案:

答案 0 :(得分:0)

似乎毫无疑问,我无法理解你到底哪里失败了,但乍看之下我看到blob类型应该是&#39; image / png&#39;。

无论如何,这是我在上传之前裁剪和调整图片大小的方法:

uploader.onAfterAddingFile = function(item) {
    var reader = new FileReader();
    reader.onload = function (event) {
        var max_size = 150;
        var tempImg = new Image();
        tempImg.src = reader.result;
        tempImg.onload = function () {
            if(this.width>max_size || this.height>max_size) {
                var canvas = document.createElement('canvas');
                canvas.width = max_size;
                canvas.height = max_size;
                var dimRatio = this.width / this.height;
                var padLeft = 0;
                var padTop = 0;
                if(dimRatio > 1) {
                    cropHeight = this.height;
                    cropWidth  = this.height;
                    padLeft = (this.width - this.height)/2;
                }
                if(dimRatio < 1) {
                    cropHeight = this.width;
                    cropWidth  = this.width;
                    padLeft = (this.height - this.width)/2;
                }

                document.body.appendChild(canvas);
                var ctx = canvas.getContext("2d");
                ctx.drawImage(this, padLeft, padTop, cropWidth, cropHeight, 0, 0, max_size, max_size);

                var dataURL = canvas.toDataURL("image/png", 1);
                //extract data from urlString
                var n = dataURL.indexOf(",");
                var data = dataURL.toString().substring(n+1);

                $scope.$apply(function () {
                    //check extension type
                    var ext = item.file.type.split("/")[1];
                    if (['jpg', 'jpeg', 'gif', 'png', 'pdf'].indexOf(ext) >= 0) {
                        $scope.user.avatar = dataURL;
                        var imgFile = b64toBlob(data,'image/png')
                        item._file = imgFile;
                        item.upload();
                    } else {
                        // invalid type
                    }
                });                    
            }
        };

    };
    reader.readAsDataURL(item._file);
};