JQuery-图片剪裁

来源:互联网 发布:淘宝金冠店铺跑路 编辑:程序博客网 时间:2024/05/12 19:05

imageselect.jsp

 

Html代码 复制代码
  1. <%@ page contentType="text/html; charset=UTF-8" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <base href="http://localhost:8080/dojodemo/" />  
  6.         <title>剪裁</title>  
  7.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  8.         <style type="text/css">  
  9.                 em {background: url(http://static.l99.com/skin/default/images/miniblog/hover.png) no-repeat;width:180px;height:60px;position:absolute;top:-85px;left:-85px;text-align:center;padding: 10px 12px 10px;font-style:normal;z-index:2;display:none;}  
  10.                 em button {background-color:#CCCCCC;border:1px solid #999999;color:#000000;cursor:pointer;font-size:12px;height:20px;width:50px;}  
  11.                 .adelete{position:relative;}  
  12.                 div.imgcutDiv {margin: 30px auto;width: 320px;text-align: center;}  
  13.                 div.imgcutDiv a {margin:0 10px;float:left;}               
  14.         </style>  
  15.     </head>  
  16.   
  17.     <body>  
  18.         <div class="tagBt_down">  
  19.             <div class="con_title">  
  20.                         剪裁头像  
  21.             </div>  
  22.             <div class="con_fill">  
  23.                 <div class="container">  
  24.                             <p>  
  25.                                 <img id="ferret" src="http://blog.btbbt.com/attachments/2008/07/1510243_200807062233495.jpg"  
  26.                                     alt="loading IMG ......"  
  27.                                     title="image select"  
  28.                                     style="float: left; margin-right: 10px;"/>  
  29.                             </p>  
  30.                 </div>  
  31.   
  32.                 <div class="clear"></div>  
  33.                 <div class="imgcutDiv">  
  34.                         <form action="cutImg.action" method="post" id="cutImgForm">  
  35.                             <input type="hidden" id="cutImageW"  name="imgWidth" />  
  36.                             <input type="hidden" id="cutImageH"  name="imgHeight" />  
  37.                             <input type="hidden" id="cutImageX"  name="imgTop" />  
  38.                             <input type="hidden" id="cutImageY"  name="imgLeft" />                                                                                          
  39.                             <input type="hidden" value="avatars.id" name="avatarsId" />                                 
  40.                             <a id="img_cut_fb" href="javascript:void(0);" class="bt_22">确认剪裁</a>  
  41.                             <a href="javascript:history.go(-1);" class="bt_2">取消剪裁</a>  
  42.                         </form>  
  43.                 </div>  
  44.             </div>  
  45.         </div>  
  46.             <script type="text/javascript" src="js/jquery-1.2.6.pack.js?ver=20080814"></script>  
  47.             <script type="text/javascript" src="js/jquery.imgareaselect-0.5.js?ver=20080913"></script>  
  48.             <script type="text/javascript">  
  49.                 var imageW = 537;  
  50.                 var imageH = 681;  
  51.                 function preview(img, selection){   
  52.                     showCut(selection.width,selection.height,selection.x1,selection.y1);  
  53.                 }  
  54.                   
  55.                 function showCut(w,h,x,y){  
  56.                     var scaleX = 200 / w;  
  57.                     var scaleY = 222 / h;  
  58.                     $('#ferret + div > img').css({ width: Math.round(scaleX * imageW) + 'px', height: Math.round(scaleY * imageH) + 'px', marginLeft: '-' + Math.round(scaleX * x) + 'px', marginTop: '-' + Math.round(scaleY * y) + 'px' });                  
  59.                     $('input#cutImageW').val(w);  
  60.                     $('input#cutImageH').val(h);  
  61.                     $('input#cutImageX').val(x);  
  62.                     $('input#cutImageY').val(y);  
  63.                 }  
  64.                   
  65.                 $(document).ready(function () {  
  66.                     $('<div><img src="http://blog.btbbt.com/attachments/2008/07/1510243_200807062233495.jpg" style="position: relative;" /></div>')  
  67.                     .css({ float: 'left', position: 'relative', overflow: 'hidden', width: '200px', height: '222px' })  
  68.                     .insertAfter($('#ferret'));  
  69.                       
  70.                     imageW = $('#ferret + div > img').width();  
  71.                     imageH = $('#ferret + div > img').height();  
  72.                       
  73.                     $("form > a#img_cut_fb").click(function(){  
  74.                         if(confirm('确任剪切吗?')){  
  75.                             alert("width: " + $('input#cutImageW').val() + "height: " + $('input#cutImageH').val() + "left: " + $('input#cutImageX').val() + "top: " + $('input#cutImageY').val());  
  76.                         }  
  77.                     });  
  78.                 });   
  79.                    
  80.                 $(window).load(function () {  
  81.                    $('img#ferret').imgAreaSelect({ selectionOpacity: 0,minWidth:90,minHeight:100,outerOpacity:'0.5', selectionColor: 'blue', aspectRatio:'9:10',x1: 10, y1: 10, x2: 100, y2: 110,onSelectChange: preview });  
  82.                    showCut(90,100,10,10);  
  83.                  });  
  84.             </script>  
  85.     </body>  
  86. </html>  

 

 

jquery.imgareaselect-0.5.js

Js代码 复制代码
  1. /* 
  2.  * imgAreaSelect jQuery plugin 
  3.  * version 0.5 
  4.  * 
  5.  * Copyright (c) 2008 Michal Wojciechowski (odyniec.net) 
  6.  * 
  7.  * Dual licensed under the MIT (MIT-LICENSE.txt)  
  8.  * and GPL (GPL-LICENSE.txt) licenses. 
  9.  * 
  10.  * http://odyniec.net/projects/imgareaselect/ 
  11.  * 
  12.  */  
  13.   
  14. jQuery.imgAreaSelect = function (img, options) {  
  15.     var $area = jQuery('<div></div>'),  
  16.         $border1 = jQuery('<div></div>'),  
  17.         $border2 = jQuery('<div></div>'),  
  18.         $outLeft = jQuery('<div></div>'),  
  19.         $outTop = jQuery('<div></div>'),  
  20.         $outRight = jQuery('<div></div>'),  
  21.         $outBottom = jQuery('<div></div>'),  
  22.         left, top, imgOfs, imgWidth, imgHeight, parent, parOfs, adjusted,  
  23.         zIndex = 0, fixed, $p,  
  24.         startX, startY, moveX, moveY,  
  25.         resizeMargin = 10, resize = [ ], V = 0, H = 1,  
  26.         d, aspectRatio,  
  27.         x1, x2, y1, y2, x, y,  
  28.         selection = { x1: 0, y1: 0, x2: 0, y2: 0, width: 0, height: 0 };  
  29.   
  30.     var $a = $area.add($border1).add($border2);  
  31.     var $o = $outLeft.add($outTop).add($outRight).add($outBottom);  
  32.       
  33.     function viewX(x)  
  34.     {  
  35.         return x + imgOfs.left + parent.scrollLeft - parOfs.left;  
  36.     }  
  37.   
  38.     function viewY(y)  
  39.     {  
  40.         return y + imgOfs.top + parent.scrollTop - parOfs.top;  
  41.     }  
  42.   
  43.     function selX(x)  
  44.     {  
  45.         return x - imgOfs.left - parent.scrollLeft + parOfs.left;  
  46.     }  
  47.       
  48.     function selY(y)  
  49.     {  
  50.         return y - imgOfs.top - parent.scrollTop + parOfs.top;  
  51.     }  
  52.       
  53.     function evX(event)  
  54.     {  
  55.         return event.pageX + parent.scrollLeft - parOfs.left;   
  56.     }  
  57.   
  58.     function evY(event)  
  59.     {  
  60.         return event.pageY + parent.scrollTop - parOfs.top;   
  61.     }  
  62.       
  63.     function adjust()  
  64.     {  
  65.         imgOfs = jQuery(img).offset();  
  66.         imgWidth = jQuery(img).width();  
  67.         imgHeight = jQuery(img).height();   
  68.           
  69.         parOfs = jQuery(parent).is('body') ? { left: 0, top: 0 } :  
  70.             jQuery(parent).offset();  
  71.               
  72.         left = viewX(0);  
  73.         top = viewY(0);  
  74.     }  
  75.   
  76.     function update()  
  77.     {  
  78.         $a.css({  
  79.             left: viewX(selection.x1) + 'px',  
  80.             top: viewY(selection.y1) + 'px',  
  81.             width: Math.max(selection.width - options.borderWidth * 2, 0) + 'px',  
  82.             height: Math.max(selection.height - options.borderWidth * 2, 0) + 'px'  
  83.         });  
  84.         $outLeft.css({ left: left + 'px', top: top + 'px',  
  85.             width: selection.x1 + 'px', height: imgHeight + 'px' });  
  86.         $outTop.css({ left: left + selection.x1 + 'px', top: top + 'px',  
  87.             width: selection.width + 'px', height: selection.y1 + 'px' });  
  88.         $outRight.css({ left: left + selection.x2 + 'px', top: top + 'px',  
  89.             width: imgWidth - selection.x2 + 'px', height: imgHeight + 'px' });  
  90.         $outBottom.css({ left: left + selection.x1 + 'px', top: top + selection.y2 + 'px',  
  91.             width: selection.width + 'px', height: imgHeight - selection.y2 + 'px' });  
  92.     }  
  93.   
  94.     function areaMouseMove(event)  
  95.     {  
  96.         if (!adjusted) {  
  97.             adjust();  
  98.             adjusted = true;  
  99.               
  100.             $a.one('mouseout'function () {  
  101.                 adjusted = false;  
  102.             });  
  103.         }  
  104.       
  105.         x = selX(evX(event)) - selection.x1;  
  106.         y = selY(evY(event)) - selection.y1;  
  107.           
  108.         resize = [ ];  
  109.   
  110.         if (options.resizable) {  
  111.             if (y <= resizeMargin)  
  112.                 resize[V] = 'n';  
  113.             else if (y >= selection.height - resizeMargin)  
  114.                 resize[V] = 's';  
  115.             if (x <= resizeMargin)  
  116.                 resize[H] = 'w';  
  117.             else if (x >= selection.width - resizeMargin)  
  118.                 resize[H] = 'e';  
  119.         }  
  120.   
  121.         $border2.css('cursor', resize.length ? resize.join('') + '-resize' :  
  122.             options.movable ? 'move' : '');  
  123.     }  
  124.   
  125.     function areaMouseDown(event)  
  126.     {  
  127.         if (event.which != 1) return false;  
  128.           
  129.         adjust();  
  130.   
  131.         if (options.resizable && resize.length > 0) {  
  132.             jQuery('body').css('cursor', resize.join('') + '-resize');  
  133.   
  134.             x1 = viewX(resize[H] == 'w' ? selection.x2 : selection.x1);  
  135.             y1 = viewY(resize[V] == 'n' ? selection.y2 : selection.y1);  
  136.   
  137.             jQuery(document).mousemove(selectingMouseMove);  
  138.             $border2.unbind('mousemove', areaMouseMove);  
  139.   
  140.             jQuery(document).one('mouseup'function () {  
  141.                 resize = [ ];  
  142.   
  143.                 jQuery('body').css('cursor''');  
  144.   
  145.                 if (options.autoHide)  
  146.                     $a.add($o).hide();  
  147.   
  148.                 options.onSelectEnd(img, selection);  
  149.   
  150.                 jQuery(document).unbind('mousemove', selectingMouseMove);  
  151.                 $border2.mousemove(areaMouseMove);  
  152.             });  
  153.         }  
  154.         else if (options.movable) {  
  155.             moveX = selection.x1 + left;  
  156.             moveY = selection.y1 + top;  
  157.             startX = evX(event);  
  158.             startY = evY(event);  
  159.   
  160.             jQuery(document)  
  161.                 .mousemove(movingMouseMove)  
  162.                 .one('mouseup'function () {  
  163.                     options.onSelectEnd(img, selection);  
  164.   
  165.                     jQuery(document).unbind('mousemove', movingMouseMove);  
  166.                 });  
  167.         }  
  168.         else  
  169.             jQuery(img).mousedown(event);  
  170.   
  171.         return false;  
  172.     }  
  173.   
  174.     function aspectRatioXY()  
  175.     {  
  176.         x2 = Math.max(left, Math.min(left + imgWidth,  
  177.             x1 + Math.abs(y2 - y1) * aspectRatio * (x2 > x1 ? 1 : -1)));  
  178.         y2 = Math.round(Math.max(top, Math.min(top + imgHeight,  
  179.             y1 + Math.abs(x2 - x1) / aspectRatio * (y2 > y1 ? 1 : -1))));  
  180.         x2 = Math.round(x2);  
  181.     }  
  182.   
  183.     function aspectRatioYX()  
  184.     {  
  185.         y2 = Math.max(top, Math.min(top + imgHeight,  
  186.             y1 + Math.abs(x2 - x1) / aspectRatio * (y2 > y1 ? 1 : -1)));  
  187.         x2 = Math.round(Math.max(left, Math.min(left + imgWidth,  
  188.             x1 + Math.abs(y2 - y1) * aspectRatio * (x2 > x1 ? 1 : -1))));  
  189.         y2 = Math.round(y2);  
  190.     }  
  191.   
  192.     function selectingMouseMove(event)  
  193.     {  
  194.         x2 = !resize.length || resize[H] || aspectRatio ? evX(event) : viewX(selection.x2);  
  195.         y2 = !resize.length || resize[V] || aspectRatio ? evY(event) : viewY(selection.y2);  
  196.   
  197.         if (options.minWidth && Math.abs(x2 - x1) < options.minWidth) {  
  198.             x2 = x1 - options.minWidth * (x2 < x1 ? 1 : -1);  
  199.   
  200.             if (x2 < left)  
  201.                 x1 = left + options.minWidth;  
  202.             else if (x2 > left + imgWidth)  
  203.                 x1 = left + imgWidth - options.minWidth;  
  204.         }  
  205.   
  206.         if (options.minHeight && Math.abs(y2 - y1) < options.minHeight) {  
  207.             y2 = y1 - options.minHeight * (y2 < y1 ? 1 : -1);  
  208.   
  209.             if (y2 < top)  
  210.                 y1 = top + options.minHeight;  
  211.             else if (y2 > top + imgHeight)  
  212.                 y1 = top + imgHeight - options.minHeight;  
  213.         }  
  214.   
  215.         x2 = Math.max(left, Math.min(x2, left + imgWidth));  
  216.         y2 = Math.max(top, Math.min(y2, top + imgHeight));  
  217.   
  218.         if (aspectRatio)  
  219.             if (Math.abs(x2 - x1) / aspectRatio > Math.abs(y2 - y1))  
  220.                 aspectRatioYX();  
  221.             else  
  222.                 aspectRatioXY();  
  223.   
  224.         if (options.maxWidth && Math.abs(x2 - x1) > options.maxWidth) {  
  225.             x2 = x1 - options.maxWidth * (x2 < x1 ? 1 : -1);  
  226.             if (aspectRatio) aspectRatioYX();  
  227.         }  
  228.   
  229.         if (options.maxHeight && Math.abs(y2 - y1) > options.maxHeight) {  
  230.             y2 = y1 - options.maxHeight * (y2 < y1 ? 1 : -1);  
  231.             if (aspectRatio) aspectRatioXY();  
  232.         }  
  233.           
  234.         selection.x1 = selX(Math.min(x1, x2));  
  235.         selection.x2 = selX(Math.max(x1, x2));  
  236.         selection.y1 = selY(Math.min(y1, y2));  
  237.         selection.y2 = selY(Math.max(y1, y2));  
  238.         selection.width = Math.abs(x2 - x1);  
  239.         selection.height = Math.abs(y2 - y1);  
  240.                   
  241.         update();  
  242.   
  243.         options.onSelectChange(img, selection);  
  244.   
  245.         return false;          
  246.     }  
  247.   
  248.     function movingMouseMove(event)  
  249.     {  
  250.         x1 = Math.max(left, Math.min(moveX + evX(event) - startX,  
  251.             left + imgWidth - selection.width));  
  252.         y1 = Math.max(top, Math.min(moveY + evY(event) - startY,  
  253.             top + imgHeight - selection.height));  
  254.         x2 = x1 + selection.width;  
  255.         y2 = y1 + selection.height;  
  256.   
  257.         selection.x1 = selX(x1);  
  258.         selection.y1 = selY(y1);  
  259.         selection.x2 = selX(x2);  
  260.         selection.y2 = selY(y2);  
  261.           
  262.         update();  
  263.   
  264.         options.onSelectChange(img, selection);  
  265.         event.preventDefault();  
  266.   
  267.         return false;  
  268.     }  
  269.   
  270.     function imgMouseDown(event)  
  271.     {  
  272.         if (event.which != 1) return false;  
  273.           
  274.         adjust();  
  275.   
  276.         selection.x1 = selection.x2 = selX(startX = x1 = x2 = evX(event));  
  277.         selection.y1 = selection.y2 = selY(startY = y1 = y2 = evY(event));  
  278.         selection.width = 0;  
  279.         selection.height = 0;  
  280.   
  281.         resize = [ ];  
  282.   
  283.         update();  
  284.         $a.add($o).show();  
  285.   
  286.         jQuery(document).mousemove(selectingMouseMove);  
  287.         $border2.unbind('mousemove', areaMouseMove);  
  288.   
  289.         options.onSelectStart(img, selection);  
  290.   
  291.         jQuery(document).one('mouseup'function () {  
  292.             if (options.autoHide)  
  293.                 $a.add($o).hide();  
  294.   
  295.             options.onSelectEnd(img, selection);  
  296.   
  297.             jQuery(document).unbind('mousemove', selectingMouseMove);  
  298.             $border2.mousemove(areaMouseMove);  
  299.         });  
  300.   
  301.         return false;  
  302.     }  
  303.       
  304.     function windowResize()  
  305.     {  
  306.         adjust();  
  307.         update();  
  308.     }  
  309.   
  310.     this.setOptions = function(newOptions)  
  311.     {  
  312.         options = jQuery.extend(options, newOptions);  
  313.   
  314.         if (newOptions.x1 != null) {  
  315.             selection.x1 = newOptions.x1;  
  316.             selection.y1 = newOptions.y1;  
  317.             selection.x2 = newOptions.x2;  
  318.             selection.y2 = newOptions.y2;  
  319.             newOptions.show = true;  
  320.         }  
  321.               
  322.         parent = jQuery(options.parent).get(0);  
  323.           
  324.         adjust();  
  325.               
  326.         $p = jQuery(img);  
  327.   
  328.         while ($p.length && !$p.is('body')) {  
  329.             if (!isNaN($p.css('z-index')) && $p.css('z-index') > zIndex)  
  330.                 zIndex = $p.css('z-index');  
  331.             if ($p.css('position') == 'fixed') fixed = true;  
  332.   
  333.             $p = $p.parent();  
  334.         }  
  335.       
  336.         x1 = viewX(selection.x1);  
  337.         y1 = viewY(selection.y1);  
  338.         x2 = viewX(selection.x2);  
  339.         y2 = viewY(selection.y2);  
  340.         selection.width = x2 - x1;  
  341.         selection.height = y2 - y1;  
  342.           
  343.         update();  
  344.           
  345.         if (newOptions.hide)  
  346.             $a.add($o).hide();  
  347.         else if (newOptions.show)  
  348.             $a.add($o).show();  
  349.               
  350.         $o.addClass(options.classPrefix + '-outer');  
  351.         $area.addClass(options.classPrefix + '-selection');  
  352.         $border1.addClass(options.classPrefix + '-border1');  
  353.         $border2.addClass(options.classPrefix + '-border2');  
  354.   
  355.         $a.css({ borderWidth: options.borderWidth + 'px' });  
  356.         $area.css({ backgroundColor: options.selectionColor, opacity: options.selectionOpacity });         
  357.         $border1.css({ borderStyle: 'solid', borderColor: options.borderColor1 });  
  358.         $border2.css({ borderStyle: 'dashed', borderColor: options.borderColor2 });  
  359.         $o.css({ opacity: options.outerOpacity, backgroundColor: options.outerColor });  
  360.   
  361.         aspectRatio = options.aspectRatio && (d = options.aspectRatio.split(/:/)) ?  
  362.             d[0] / d[1] : null;  
  363.   
  364.         if (options.disable || options.enable === false) {  
  365.             $a.unbind('mousemove', areaMouseMove).unbind('mousedown', areaMouseDown);  
  366.             jQuery(img).add($o).unbind('mousedown', imgMouseDown);  
  367.             jQuery(window).unbind('resize', windowResize);  
  368.         }  
  369.         else if (options.enable || options.disable === false) {  
  370.             if (options.resizable || options.movable)  
  371.                 $a.mousemove(areaMouseMove).mousedown(areaMouseDown);  
  372.   
  373.             jQuery(img).add($o).mousedown(imgMouseDown);  
  374.             jQuery(window).resize(windowResize);  
  375.         }  
  376.           
  377.         jQuery(options.parent).append($o.add($a));  
  378.   
  379.         options.enable = options.disable = undefined;  
  380.     };  
  381.   
  382.     if (jQuery.browser.msie)  
  383.         jQuery(img).attr('unselectable''on');  
  384.           
  385.     $a.add($o).css({ display: 'none', position: fixed ? 'fixed' : 'absolute',  
  386.         overflow: 'hidden', zIndex: zIndex > 0 ? zIndex : null });  
  387.     $area.css({ borderStyle: 'solid' });  
  388.   
  389.     initOptions = {  
  390.         borderColor1: '#000',  
  391.         borderColor2: '#fff',  
  392.         borderWidth: 1,  
  393.         classPrefix: 'imgareaselect',  
  394.         movable: true,  
  395.         resizable: true,  
  396.         selectionColor: '#fff',  
  397.         selectionOpacity: 0.2,  
  398.         outerColor: '#000',  
  399.         outerOpacity: 0.2,  
  400.         parent: 'body',  
  401.         onSelectStart: function () {},  
  402.         onSelectChange: function () {},  
  403.         onSelectEnd: function () {}  
  404.     };  
  405.   
  406.     options = jQuery.extend(initOptions, options);  
  407.     this.setOptions(options);  
  408. };  
  409.   
  410. jQuery.fn.imgAreaSelect = function (options) {  
  411.     options = options || {};  
  412.   
  413.     this.each(function () {  
  414.         if (jQuery(this).data('imgAreaSelect'))  
  415.             jQuery(this).data('imgAreaSelect').setOptions(options);  
  416.         else {  
  417.             if (options.enable === undefined && options.disable === undefined)  
  418.                 options.enable = true;  
  419.   
  420.             jQuery(this).data('imgAreaSelect'new jQuery.imgAreaSelect(this, options));  
  421.         }  
  422.     });  
  423.   
  424.     return this;  
  425. }; 
原创粉丝点击