• AngularJs + html 5 实现 裁剪上传


    直接上代码 

    directive.js

    app.directive('fileUploadersm', function () {
        return {
            restrict: 'E',
            transclude: true,
            template: '<input type="file" accept="images/*" id="files" >' + '<div class="containertsm"></div><button ng-click="upload()" class="btn btn-danger">确定上传</button><br/>' + '<ul><li ng-repeat="file in files"> - </li></ul>',
            controller: function ($scope, $fileUpload) {
                $scope.notReady = true;
                $scope.addfood = function () {
                    $fileUpload.upload($scope.files);
                };
            },
            link: function ($scope, $element) {
                ////////////
                var tmp = $('<div class="resizer">' + '<div class="inner">' + '<img class="imgs">' + '<div class="frames"></div>' + '</div>' + '<button class="ok">✓</button>' + '</div>');
                $.imageResizer = function () {
                    if (Uint8Array && HTMLCanvasElement && atob && Blob) {
                    }
                    else {
                        return false;
                    }
                    var resizer = tmp.clone();
                    resizer.image = resizer.find('img')[0];
                    resizer.frames = resizer.find('.frames');
                    resizer.okButton = resizer.find('button.ok');
                    resizer.frames.offset = {
                        top: 0,
                        left: 0
                    };
                    resizer.okButton.click(function () {
                        resizer.clipImage();
                    });
                    resizer.clipImage = function () {
                        var nh = this.image.naturalHeight, nw = this.image.naturalWidth, size = nw > nh ? nh : nw;
                        size = size > 150 ? 150 : size;
                        var canvas = $('<canvas class="tcanvas" width="' + size + '" height="' + size + '"></canvas>')[0],
                            ctx = canvas.getContext('2d'),
                            scale = nw / this.offset.width,
                            x = this.frames.offset.left * scale,
                            y = this.frames.offset.top * scale,
                            w = this.frames.offset.size * scale,
                            h = this.frames.offset.size * scale;
                        ctx.drawImage(this.image, x, y, w, h, 0, 0, size, size);
    
                        var src = canvas.toDataURL();
                        this.canvas = canvas;
                        this.append(canvas);
                        this.addClass('uploading');
                        this.removeClass('have-img');
                        src = src.split(',')[1];
                        if (!src)
                            return this.doneCallback(null);
                        src = window.atob(src);
                        var ia = new Uint8Array(src.length);
                        for (var i = 0; i < src.length; i++) {
                            ia[i] = src.charCodeAt(i);
                        }
                        ;
                        this.doneCallback(new Blob([ia], { type: "image/png" }));
                        Id = new Blob([ia], { type: "image/png" });//这里Id  存放的就是裁剪之后的相片你可以自定义全局参数
                    };
                    resizer.resize = function (file, done) {
                        this.reset();
                        this.doneCallback = done;
                        this.setFrameSize(0);
                        this.frames.css({
                            top: 0,
                            left: 0
                        });
                        var reader = new FileReader();
                        reader.onload = function () {
                            resizer.image.src = reader.result;
                            reader = null;
                            resizer.addClass('have-img');
                            resizer.setFrames();
                        };
                        reader.readAsDataURL(file);
                    };
                    resizer.reset = function () {
                        this.image.src = '';
                        this.removeClass('have-img');
                        this.removeClass('uploading');
                        this.find('canvas').detach();
                    };
                    resizer.setFrameSize = function (size) {
                        this.frames.offset.size = size;
                        return this.frames.css({
                             size + 'px',
                            height: size + 'px'
                        });
                    };
                    resizer.getDefaultSize = function () {
                        var width = this.find(".inner").width(), height = this.find(".inner").height();
                        this.offset = {
                             width,
                            height: height
                        };
                        console.log(this.offset);
                        return width > height ? height : width;
                    };
                    resizer.moveFrames = function (offset) {
                        var x = offset.x, y = offset.y, top = this.frames.offset.top, left = this.frames.offset.left, size = this.frames.offset.size, width = this.offset.width, height = this.offset.height;
                        if (x + size + left > width) {
                            x = width - size;
                        }
                        else {
                            x = x + left;
                        }
                        ;
                        if (y + size + top > height) {
                            y = height - size;
                        }
                        else {
                            y = y + top;
                        }
                        ;
                        x = x < 0 ? 0 : x;
                        y = y < 0 ? 0 : y;
                        this.frames.css({
                            top: y + 'px',
                            left: x + 'px'
                        });
                        this.frames.offset.top = y;
                        this.frames.offset.left = x;
                    };
                    (function () {
                        var time;
                        function setFrames() {
                            var size = resizer.getDefaultSize();
                            resizer.setFrameSize(size);
                        }
                        ;
                        window.onresize = function () {
                            clearTimeout(time);
                            time = setTimeout(function () {
                                setFrames();
                            }, 1000);
                        };
                        resizer.setFrames = setFrames;
                    })();
                    (function () {
                        var lastPoint = null;
                        function getOffset(event) {
                            event = event.originalEvent;
                            var x, y;
                            if (event.touches) {
                                var touch = event.touches[0];
                                x = touch.clientX;
                                y = touch.clientY;
                            }
                            else {
                                x = event.clientX;
                                y = event.clientY;
                            }
                            if (!lastPoint) {
                                lastPoint = {
                                    x: x,
                                    y: y
                                };
                            }
                            ;
                            var offset = {
                                x: x - lastPoint.x,
                                y: y - lastPoint.y
                            };
                            lastPoint = {
                                x: x,
                                y: y
                            };
                            return offset;
                        }
                        ;
                        resizer.frames.on('touchstart mousedown', function (event) {
                            getOffset(event);
                        });
                        resizer.frames.on('touchmove mousemove', function (event) {
                            if (!lastPoint)
                                return;
                            var offset = getOffset(event);
                            resizer.moveFrames(offset);
                        });
                        resizer.frames.on('touchend mouseup', function (event) {
                            lastPoint = null;
                        });
                    })();
                    return resizer;
                };
                var resizer = $.imageResizer(), resizedImage;
                if (!resizer) {
                    resizer = $("<p>Your browser doesn't support these feature:</p><ul><li>canvas</li><li>Blob</li><li>Uint8Array</li><li>FormData</li><li>atob</li></ul>");
                }
                ;
                $('.containertsm').append(resizer);
                ///////////////////
                var fileInput = $element.find('input[type="file"]');
                fileInput.bind('change', function (e) {
                    var file = this.files[0];
                    resizer.resize(file, function (file) {
                        resizedImage = file;
                    });
                    $scope.notReady = e.target.files.length == 0;
                    $scope.files = [];
                    for (i in e.target.files) {
                        //Only push if the type is object for some stupid-ass reason browsers like to include functions and other junk
                        if (typeof e.target.files[i] == 'object')
                            $scope.files.push(e.target.files[i]);
                    }
                });
                // var file=this.files[0];
                // resizer.resize(file,function(file){
                //     resizedImage=file;
                // });
            }
        };
    });
    

      部分内容 请参照 html5 实现本地上传裁剪 http://www.webrube.com/jquery-html5-web_rube/4300

    html 代码

    <div class="col-sm-6">
                    <div>
                        <file-uploader></file-uploader>
                    </div>
    </div>

      

  • 相关阅读:
    flashdevelop生成swc库(转)
    SOG and COG spec
    [C]switch...case...一个case包含多个值的一种写法
    [C]赋值语句返回值
    Antenna知识整理
    SPI接口比IIC速度快的理解
    C语言代码的换行
    SRRC认证
    蓝牙协议分析(6)_BLE地址类型
    结构体变量和指针对成员的引用形式
  • 原文地址:https://www.cnblogs.com/wohenxion/p/4478541.html
Copyright © 2020-2023  润新知