Jquery插件-Html5图片上传并裁剪

/**
 * 图片裁剪
 * @author yanglizhe
 * 2015/11/16
 */
(function($){

    /**
     * Drag
     */
    var Drag={obj:null,init:function(elementHeader,element){elementHeader.onmousedown=Drag.start;elementHeader.obj=element;if(isNaN(parseInt(element.style.left))){element.style.left="0px"}if(isNaN(parseInt(element.style.top))){element.style.top="0px"}element.onDragStart=new Function();element.onDragEnd=new Function();element.onDrag=new Function()},start:function(event){var element=Drag.obj=this.obj;event=Drag.fixE(event);if(event.which!=1){return true}element.onDragStart();element.lastMouseX=event.clientX;element.lastMouseY=event.clientY;document.onmouseup=Drag.end;document.onmousemove=Drag.drag;return false},drag:function(event){event=Drag.fixE(event);if(event.which==0){return Drag.end()}var element=Drag.obj;var _clientX=event.clientY;var _clientY=event.clientX;if(element.lastMouseX==_clientY&&element.lastMouseY==_clientX){return false}var _lastX=parseInt(element.style.top);var _lastY=parseInt(element.style.left);var newX,newY;newX=_lastY+_clientY-element.lastMouseX;newY=_lastX+_clientX-element.lastMouseY;element.style.left=newX+"px";element.style.top=newY+"px";element.lastMouseX=_clientY;element.lastMouseY=_clientX;element.onDrag(newX,newY);return false},end:function(event){event=Drag.fixE(event);document.onmousemove=null;document.onmouseup=null;var _onDragEndFuc=Drag.obj.onDragEnd();Drag.obj=null;return _onDragEndFuc},fixE:function(ig_){if(typeof ig_=="undefined"){ig_=window.event}if(typeof ig_.layerX=="undefined"){ig_.layerX=ig_.offsetX}if(typeof ig_.layerY=="undefined"){ig_.layerY=ig_.offsetY}if(typeof ig_.which=="undefined"){ig_.which=ig_.button}return ig_}};

    /**
     * 创建样式表
     * @param String cssStr
     */
    function createStyle(cssStr){
        if( document.createStyleSheet){
            var style = document.createStyleSheet();
            style.cssText = cssStr;
        }
        else{
            var style = document.createElement("style");
            style.type = "text/css";
            style.textContent = cssStr;
            document.getElementsByTagName("head").item(0).appendChild(style);
        }
    }

    var MAX_SIZE = 200;

    function ImageCrop(config){
        this.zoomMin = 0.1; //最小比例
        this.zoomMax = 10; //最大比例
        this.wrapWidth = 280;
        this.wrapHeight = 260;
        this.gripMinX = 0;
        this.gripMaxX = 189;
        this.originalWidth = 0;
        this.originalHeight = 0;
        this.originalLeft = 0;
        this.origianlTop = 0;
        this._construct(config);
    }

    ImageCrop.prototype = {
        /**
         * 构建方法
         * @param config
         * @private
         */
        _construct:function(config){
            this.eventTarget = config.target;
            this.cropWidth = config.width || 140;
            this.cropHeight = config.height || 140;
            this.ratio = 1;
            if(this.cropWidth > MAX_SIZE || this.cropHeight > MAX_SIZE){
                if(this.cropWidth > this.cropHeight){
                    this.ratio = MAX_SIZE/this.cropWidth;
                    this.cropWidth = MAX_SIZE;
                    this.cropHeight *= this.ratio;
                }
                else{
                    this.ratio = MAX_SIZE / this.cropHeight;
                    this.cropHeight = MAX_SIZE;
                    this.cropWidth *= this.ratio;
                }
            }
            this.offsetX = (this.wrapWidth - this.cropWidth ) / 2;
            this.offsetY = (this.wrapHeight - this.cropHeight) / 2;
            this.destWidth = config.destWidth || this.cropWidth;
            this.destHeight = config.destHeight || this.cropHeight;
            this.onConfirm = config.onConfirm || function(){};
            var parent = config.container || $(document.body);
            this.initHTML(parent);
            this.bindEvent();

        },
        initHTML:function(parent){
            var html = ‘<div class="crop-container" style="display:block;">‘+
                ‘    <div class="left-box">‘+
                ‘        <div class="crop-wrapper">‘+
                ‘            <div class="crop-preview"><div class="crop-preview-box"></div></div>‘+
                ‘            <img class="crop-target" style="position:relative;" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />‘+
                ‘        </div>‘+
                ‘        <div class="crop-tool-bar">‘+
                ‘            <span><a href="javascript:;" class="zoom-tool btn-zoom zoom-out" alt="图片缩小"></a></span>‘+
                ‘            <span class="bar-zoom-box">‘+
                ‘                <span class="zoom-tool bar-zoom"></span>‘+
                ‘                <a href="javascript:;" class= "zoom-tool crop-grip"></a>‘+
                ‘            </span>‘+
                ‘            <span><a href="javascript:;" class="zoom-tool btn-zoom zoom-in"   alt="图片放大"></a></span>‘+
                ‘        </div>‘+
                ‘    </div>‘+
                ‘    <div class="right-box">‘+
                ‘        <div class="image-preview"><img src="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" /></div>‘+
                ‘        <div class="btn-box"><a class="btn-upload-image" href="javascript:;">图片上传</a></div>‘+
                ‘        <div class="btn-box"><a class="btn-confirm-crop" href="javascript:;">确认裁剪</a></div>‘+
                ‘    </div>‘+
                ‘</div>‘;
            this.container = $(html);

            this.find(".crop-preview").css({
                width : (this.cropWidth + 4) +"px",
                height: (this.cropHeight + 4) + "px",
                "border-width": (this.offsetY -2) +"px " + (this.offsetX - 2) +"px"
            });

            this.find(".crop-preview-box").css({
                width : (this.cropWidth) +"px",
                height: (this.cropHeight) + "px"
            });

            parent.append(this.container);
        },
        bindEvent:function(){
            this.cropTarget = this.find(".crop-target");
            this.cropGrip = this.find(".crop-grip");
            this.wrapper = this.find(".crop-wrapper");
            var _this = this;
            this.eventTarget.bind("click", function(){
                _this.show();
            });

            this.bindCropGrip();
            this.bindUpload();
            this.find(".btn-zoom").bind("click", function(){
               if($(this).hasClass("zoom-out")){
                    _this.scale(false);
               }
               else if($(this).hasClass("zoom-in")){
                   _this.scale(true);
               }
            });

            var canvas = document.createElement("canvas");
            canvas.width = this.destWidth;
            canvas.height = this.destHeight;

            var context = canvas.getContext("2d");
            this.find(".btn-confirm-crop").bind("click", function(){
                var target = _this.cropTarget.get(0);
                var top = parseInt(target.style.top);
                var left = parseInt(target.style.left);
                var offsetLeft = left - _this.originalLeft;
                var offsetTop = top - _this.origianlTop;
                var scale =  _this.zoom;
                var _left = (_this.zoom * _this.originalWidth - _this.cropWidth) / 2;
                var _top = (_this.zoom * _this.originalHeight - _this.cropHeight) / 2;
                var x = (-_left + offsetLeft) / scale;
                var y = (-_top + offsetTop) /scale;
                context.clearRect(0, 0, canvas.width, canvas.height);
                context.save();
                context.scale(scale, scale);
                context.drawImage(_this.cropTarget.get(0), x, y);
                context.restore();
                var base64Url = canvas.toDataURL("image/png");
                _this.find(".image-preview").find("img").attr("src", base64Url);
                _this.onConfirm(base64Url)
            });

        },
        bindUpload:function(){
            var _this = this;

            this.fileInput = $(‘<input type="file" accept="image/*" style="position: absolute;left: -999999px;" />‘);
            this.fileInput.bind("change", function(e){
                if($.trim($(this).val())==""){
                    return ;
                }
                var files = e.target.files || e.dataTransfer.files;
                var file = files[0];

                if(!/^image\//.test(file.type)){
                    alert("请上传图片文件");
                    _this.fileInput.val("");
                    return false;
                }

                //对路径进行压缩
                var reader = new FileReader();
                reader.onload = function(e) {
                    var image = new Image();
                    image.onload = function(){
                        _this.cropTarget.attr("src", this.src);
                        _this.initImage(this.width, this.height);

                    };
                    image.src = e.target.result;
                }
                reader.readAsDataURL(file);
            });

            var _this = this;
            this.find(".btn-upload-image").bind("click", function(){
                _this.fileInput.trigger("click");
            });
        },
        initImage:function(width, height){

            var _this = this;
            var top = -(height - this.wrapHeight) / 2;
            var left =  -(width - this.wrapWidth) / 2;
            this.originalLeft = left;
            this.origianlTop = top;
            this.originalWidth = width;
            this.originalHeight = height;
            this.cropTarget.get(0).style.transform = "";
            this.cropTarget.css({
                top:top,
                left:left
            });
            this.zoomMin = 1;
            this.gripPos = 5;
            this.zoom = 1;
            if (width > this.cropWidth) {
                this.zoomMin = this.cropWidth / width;
            }
            this.zoomMax = this.zoomMin > 0.25 ? 8.0 : 4.0 / Math.sqrt(this.zoomMin);
            this.cropGrip.css({
                "left":(this.gripMinX + (this.gripPos / 10 * (this.gripMaxX - this.gripMinX))) + "px"
            });

            var target = this.cropTarget.get(0);
            Drag.init(this.wrapper.get(0), target);
            target.onDrag = function(){
                _this.rePosition();
            };
        },
        rePosition:function(){
            var target = this.cropTarget.get(0);
            var width = this.originalWidth;
            var height = this.originalHeight;
            var offsetLeft = parseInt(this.cropTarget.css("left"));
            var offsetTop = parseInt(this.cropTarget.css("top"));
            var _width = width * this.zoom;
            var _height = height * this.zoom;
            var _x = (_width - width ) / 2;
            var _y = (_height - height) / 2;

            var maxX = Math.max(this.offsetX, this.offsetX + this.cropWidth - _width) + _x;
            var minX = Math.min(this.offsetX + this.cropWidth - _width, this.offsetX) + _x;

            if (offsetLeft > maxX) target.style.left = maxX + ‘px‘;
            else if (offsetLeft < minX) target.style.left = minX + ‘px‘;

            var maxY = Math.max(this.offsetY, this.offsetY + this.cropHeight - _height) + _y;
            var minY = Math.min(this.offsetY + this.cropHeight - _height, this.offsetY) + _y;

            if (offsetTop > maxY) target.style.top = maxY + ‘px‘;
            else if (offsetTop < minY) target.style.top = minY + ‘px‘;
        },
        scale:function(flag){
            if (flag) {
                this.zoom = this.zoom * 1.5;
            } else {
                this.zoom = this.zoom / 1.5;
            }
            if (this.zoom < this.zoomMin) this.zoom = this.zoomMin;
            if (this.zoom > this.zoomMax) this.zoom = this.zoomMax;
            this.imageResize(this.zoom);
            this.gripPos = 5 * (Math.log(this.zoom * this.zoomMax) / Math.log(this.zoomMax));
            this.cropGrip.css({
                left:(this.gripMinX + (this.gripPos / 10 * (this.gripMaxX - this.gripMinX))) + "px"
            });
            this.rePosition();
        },
        bindCropGrip:function(){
            var _this = this;

            var cropGrip = this.cropGrip.get(0);
            Drag.init(cropGrip, cropGrip);
            cropGrip.onDrag = function (clientX, clientY) {
                var offsetX = clientX;
                if (clientX < _this.gripMinX) {
                    cropGrip.style.left = _this.gripMinX + "px";
                    offsetX = _this.gripMinX;
                }
                if (clientX > _this.gripMaxX) {
                    cropGrip.style.left = _this.gripMaxX + "px";
                    offsetX = _this.gripMaxX;
                }
                cropGrip.style.top = 0;
                var x = (offsetX - _this.gripMinX) * 10 / (_this.gripMaxX - _this.gripMinX);
                var zoom = Math.pow(_this.zoomMax, x / 5) / _this.zoomMax;
                if (zoom < _this.zoomMin) zoom = _this.zoomMin;
                if (zoom > _this.zoomMax) zoom = _this.zoomMax;
                _this.imageResize(zoom);
                _this.rePosition();
            }
        },
        imageResize:function(zoom){
            this.zoom = zoom;
            this.cropTarget.get(0).style.transform = "scale("+zoom+")";
        },
        show:function(){
            this.container.show();
        },
        hide:function(){
            this.container.hide();
        },
        find:function(selector){
            return $(selector, this.container);
        }
    };

    $.fn.imageCrop = function(config){
        return this.each(function(){
            new ImageCrop($.extend({
                target:$(this)
            },config||{}));
        });
    };

    createStyle(‘.crop-container{width:480px;height:300px;overflow:hidden;margin:10px auto;border:1px solid #e0e0e0;border-radius:5px;padding:10px}.zoom-tool{background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAASCAYAAAGWfdOKAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoV2luZG93cykiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6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ow8LOqR4BlJTpr5J0JZWe54ofCHis8ue5iwGIwJCAubMjH599R79O3CBl2/tdRnZHMQV8Q0jG+w60KxRKPRYV6lsWL+73luvSro2yXIK8sNNthIMwdvdrkqfTfMhMVgTKqwXIzLPiwH+AsrIpcGf3e4WQAAAABJRU5ErkJggg==);background-repeat:no-repeat}.left-box{float:left;height:320px;width:292px;overflow:hidden}.crop-wrapper{border:2px solid #888;width:280px;height:260px;overflow:hidden;position:relative;margin:4px;cursor:pointer;float:left}.crop-preview{width:144px;height:144px;border-color:#ccc;border-style:solid;border-width:58px 68px;position:absolute;z-index:1;opacity:.75}.crop-preview-box{width:140px;height:140px;border:2px solid #e0e0e0}.crop-tool-bar{width:252px;padding:4px 20px;height:20px;position:relative;overflow:hidden}.bar-zoom-box{position:relative;width:200px;display:inline-block}.crop-grip{position:absolute;z-index:100;left:100%;top:0;cursor:pointer;width:11px;height:18px;display:inline-block;background-position:-287px 0}.right-box{float:right;width:144px;padding:8px;height:280px}.right-box .image-preview{height:166px;width:144px;margin:8px auto;overflow:hidden;text-align:center;}.image-preview img{border:2px #eee solid;max-height:100%;max-width:140px;}.right-box .btn-box{height:50px;text-align:center}.right-box .btn-box a{display:inline-block;width:100px;padding:8px;text-align:center;border-radius:8px;text-decoration:none;border:1px solid #efefef;color:#FFF;font-size:14px;background:#80b6e7}.right-box .btn-box a:hover{background:#5498db}.btn-zoom{width:18px;height:18px;display:inline-block}.bar-zoom{width:200px;height:18px;display:inline-block}.zoom-out{background-position:-224px 0}.zoom-out:hover{background-position:-203px 0}.zoom-in{background-position:-266px 0}.zoom-in:hover{background-position:-245px 0}‘);

})(jQuery);

时间: 2024-10-05 04:24:52

Jquery插件-Html5图片上传并裁剪的相关文章

JQuery插件:图片上传本地预览插件,改进案例一则。

/* *名称:图片上传本地预览插件 v1.1 *作者:周祥 *时间:2013年11月26日 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *插件网站:http://keleyi.com/keleyi/phtml/image/16.htm *参数说明: Img:图片ID;Width:预览宽度;Height:预览高度;ImgType:支持文件类型;Callback:选择文件显示图片后回调方法; *使用方法: <div> <img id

jquery插件fileupload图片上传(前端如何处理)

1.页面首先引入jquery,版本不要低于1.6 <script src="../js/jquery.min.js"></script>2.其次页面引入对应的这个五个js,按顺序放置 <script src="../js/jquery.ui.widget.js"></script> <script src="../js/jquery.iframe-transport.js"></s

jQuery插件-ajaxFileUpload异步上传文件

ajaxFileUpload是前两天学习过程中接触到的一个JQ插件,功能很简单,就是实现异步上传文件的功能. 1.语法介绍 $.ajaxFileUpload([setting]); 参数说明: (1)url——执行上传处理的地址: (2)secureuri——是否启用安全提交,默认为false: (3)fileElementId——需要上传<input type=”file”>控件id属性值: (4)data——自定义参数,即需要额外传递的JSON格式数据: (5)type——请求方式,提交自

【转】JQuery插件ajaxFileUpload 异步上传文件(PHP版)

前几天想在手机端做个异步上传图片的功能,平时用的比较多的JQuery图片上传插件是Uploadify这个插件,效果很不错,但是由于手机不支持flash,所以不得不再找一个文件上传插件来用了.后来发现ajaxFileUpload这个插件挺不错,所以就用这个插件来做异步上传文件的效果.网上也有很多对ajaxFileUpload插件的使用的文章,不过我发现没有PHP版,所以这次服务器那边的处理就使用PHP语言来处理. 一.先对ajaxFileUpload插件的语法参数进行讲解 原理:ajaxfileu

JQuery插件ajaxFileUpload 异步上传文件(PHP版)

太久没写博客了,真的是太忙了.善于总结,进步才会更快啊.不多说,直接进入主题. 前几天想在手机端做个异步上传图片的功能,平时用的比较多的JQuery图片上传插件是Uploadify这个插件,效果很不错,但是由于手机不支持flash,所以不得不再找一个文件上传插件来用了.后来发现ajaxFileUpload这个插件挺不错,所以就用这个插件来做异步上传文件的效果.网上也有很多对ajaxFileUpload插件的使用的文章,不过我发现没有PHP版,所以这次服务器那边的处理就使用PHP语言来处理. 一.

html5 图片上传,支持图片预览、压缩、及进度显示,兼容IE6+及标准浏览器

原文:html5 图片上传,支持图片预览.压缩.及进度显示,兼容IE6+及标准浏览器 以前写过上传组件,见 打造 html5 文件上传组件,实现进度显示及拖拽上传,兼容IE6+及其它标准浏览器,对付一般的上传没有问题,不过如果是上传图片,且需要预览的话,就力有不逮了,趁着闲暇时间,给上传组件添加了单独的图片上传UI,支持图片预览和缩放(通过调整图片的大小以实现图片压缩). 上传组件特点 轻量级,不依赖任何JS库,核心代码(Q.Uploader.js)仅约700行,min版本加起来不到12KB 纯

Java往事之《百度UEditor插件配置图片上传问题》

百度UEditor插件配置图片上传问题 前言:之前第一次用到UEditor插件的时候,一脸懵逼!没错就是一脸懵逼,去UEditor插件官网下载了一个开发版的.然后对于新鲜技术好奇,本人就迫不及待的把它copy到了我的项目里,运行之后发现鼠标点到输入框里面的时候,什么鬼啊,握了棵草!上传图片的插件置灰了如下图1,一看还有一个多图片上传的插件还在,就赶紧点了一下,点开的那一瞬间我吃了一鲸,如下图2! 图1 图2 很好!下面看一下如何解决这个问题. 1.首先你可以到官网Ueditor,查看很详细的文档

jQuery:[1]实现图片上传并预览

jQuery:[1]实现图片上传并预览 原理 预览思路 1.当上传对象的input被触发并选择本地图片之后获取要上传的图片对象的URL: 2.把对象URL赋值给实现写好的img标签的src属性 File对象 File对象可以用来获取文件的信息,还可以用来读取这个文件的内容,通常情况下,File对象是来自用户在一个input元素上选择文件后返回的FileList对象,也可以是来自由拖放操作生成的DataTransfer对象. Blob对象 Blob对象是一个包含由只读原始数据的类文件对象,Blob

图片上传和裁剪,bitmapcutter组件的使用

图片上传在上篇博文中讲过. 这里主要是裁剪的实现,需要用到bitmapcutter组件. jquery.bitmapcutter该插件由Jericho开发,它的主要作用是客户端裁图. 引入jquery库 <script language="javascript" type="text/javascript" src="/js/jquery1.4.2.min.js"></script> 引入jquery.bitmapcutt