免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2286 | 回复: 1
打印 上一主题 下一主题

JS简易图片裁剪-多点移动 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-19 16:57 |只看该作者 |倒序浏览
JS简易图片裁剪-多点移动






最近天天都是加班,好不容易年前休息了,就抽点时间谢谢,自认为还是比较简单易懂的,没有用什么复杂牛叉的算法,因为我也会,呵呵。(没有对图片大小边界做判断,只对容器做了判断,请注意~ )



懒得详细说明了,先上前端代码:



Html代码
  1. 1.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. 2.<html xmlns="http://www.w3.org/1999/xhtml">  
  3. 3.<head>  
  4. 4.<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. 5.<title>无标题文档</title>  
  6. 6.<style type="text/css">  
  7. 7.*{margin:0; padding:0;}   
  8. 8.#picDiv{width:703px; height:338px; background:#fff; border:1px solid #999; margin:100px auto; position:relative; overflow:hidden;}   
  9. 9.#picDiv ul{width:200px; height:200px; border:1px dashed #fff; cursor:move; list-style:none; position:relative; z-index:200;}   
  10. 10.#picDiv li{width:6px; height:6px; background:#fff; border:1px solid #333; margin:-4px 0 0 -4px; overflow:hidden; position:absolute; opacity:0.5; filter:alpha(opacity=50);}   
  11. 11.#picDiv li.tl{cursor:nw-resize; top:0%; left:0%;}   
  12. 12.#picDiv li.tc{cursor:n-resize; top:0%; left:50%;}   
  13. 13.#picDiv li.tr{cursor:ne-resize; top:0%; left:100%;}   
  14. 14.#picDiv li.ml{cursor:w-resize; top:50%; left:0%;}   
  15. 15.#picDiv li.mr{cursor:e-resize; top:50%; left:100%;}   
  16. 16.#picDiv li.fl{cursor:sw-resize; top:100%; left:0%;}   
  17. 17.#picDiv li.fc{cursor:s-resize; top:100%; left:50%;}   
  18. 18.#picDiv li.fr{cursor:se-resize; top:100%; left:100%;}   
  19. 19.</style>  
  20. 20.</head>  
  21. 21.  
  22. 22.<body>  
  23. 23.<div id="picDiv">  
  24. 24.    <ul>  
  25. 25.        <li class="tl"></li>  
  26. 26.        <li class="tc"></li>  
  27. 27.        <li class="tr"></li>  
  28. 28.        <li class="ml"></li>  
  29. 29.        <li class="mr"></li>  
  30. 30.        <li class="fl"></li>  
  31. 31.        <li class="fc"></li>  
  32. 32.        <li class="fr"></li>  
  33. 33.    </ul>  
  34. 34.    <img src="../images/2.jpg" style="position:absolute; top:0; left:0; opacity:0.3; filter:alpha(opacity=30); z-index:10;" />  
  35. 35.    <img src="../images/2.jpg" style="position:absolute; top:0; left:0; clip:rect(0 0 0 0); z-index:100;" />  
  36. 36.</div>  
  37. 37.<form action="41.php" method="post">  
  38. 38.    <input type="submit" value=" 提 交 " />  
  39. 39.    <input type="hidden" id="imgInfor" name="imgInfor" />  
  40. 40.</form>  
  41. 41.<script type="text/javascript">  
  42. 42.(function(){   
  43. 43.    var div=document.getElementById('picDiv'),   
  44. 44.        ul=div.getElementsByTagName('ul')[0],   
  45. 45.        li=ul.getElementsByTagName('li');   
  46. 46.        divdivW=div.offsetWidth-2,   
  47. 47.        divdivH=div.offsetHeight-2,   
  48. 48.        see=function(){   
  49. 49.            var t=ul.offsetTop,   
  50. 50.                l=ul.offsetLeft,   
  51. 51.                r=l+ul.offsetWidth,   
  52. 52.                f=t+ul.offsetHeight;   
  53. 53.            div.getElementsByTagName('img')[1].style.clip='rect('+t+'px '+r+'px '+f+'px '+l+'px)';   
  54. 54.            document.getElementById('imgInfor').value="{'x':'"+l+"','y':'"+t+"','w':'"+ul.offsetWidth+"','h':'"+ul.offsetHeight+"','url':'"+div.getElementsByTagName('img')[1].src+"'}";   
  55. 55.            window.getSelection?window.getSelection().removeAllRanges():document.selection.empty();   
  56. 56.        },   
  57. 57.        clear=function(o){   
  58. 58.            if(!-[1,]){o.setCapture();}//学增加丢失焦点事件   
  59. 59.            document.onmouseup=function(){   
  60. 60.                if(!-[1,]){o.releaseCapture();}   
  61. 61.                document.onmousemove=null;   
  62. 62.                document.onmouseup=null;   
  63. 63.            };   
  64. 64.            return false;//webkit中去掉默认事件,使鼠标不会变成默认文本光标   
  65. 65.        };   
  66. 66.    if(!-[1,]){   
  67. 67.        var cDiv=document.createElement('div');   
  68. 68.        cDiv.style.cssText='width:100%; height:100%; background:#fff; opacity:0; filter:alpha(opacity=0); font-size:0;';   
  69. 69.        ul.appendChild(cDiv);   
  70. 70.    }   
  71. 71.    ul.onmousedown=function(e){   
  72. 72.        ee=e||window.event;   
  73. 73.        var oldX=e.clientX-ul.offsetLeft,   
  74. 74.            oldY=e.clientY-ul.offsetTop,   
  75. 75.            maxW=divW-ul.offsetWidth,   
  76. 76.            maxH=divH-ul.offsetHeight;   
  77. 77.        document.onmousemove=function(e){   
  78. 78.            ee=e||window.event;   
  79. 79.            var newX=e.clientX-oldX,newY=e.clientY-oldY;   
  80. 80.            newXnewX=newX<0?0:newX>maxW?maxW:newX;   
  81. 81.            newYnewY=newY<0?0:newY>maxH?maxH:newY;   
  82. 82.            ul.style.top=newY+'px';   
  83. 83.            ul.style.left=newX+'px';   
  84. 84.            see();   
  85. 85.        };   
  86. 86.        clear(this);   
  87. 87.    };   
  88. 88.    for(var i in li){   
  89. 89.        li[i].onmousedown=function(e){   
  90. 90.            ee=e||window.event;   
  91. 91.            var oldX=e.clientX,   
  92. 92.                oldY=e.clientY,   
  93. 93.                oldT=ul.offsetTop+2,   
  94. 94.                oldL=ul.offsetLeft+2,   
  95. 95.                oldW=ul.offsetWidth,   
  96. 96.                oldH=ul.offsetHeight,   
  97. 97.                minW=50,minH=50,   
  98. 98.                _method=this;   
  99. 99.            if(e.stopPropagation){   
  100. 100.                e.stopPropagation();   
  101. 101.            }else{   
  102. 102.                e.cancelBubble=true;   
  103. 103.            }   
  104. 104.            document.onmousemove=function(e){   
  105. 105.                ee=e||window.event;   
  106. 106.                var lis={'w':'tr,mr,fr','h':'fl,fc,fr','l':'tl,ml,fl','t':'tl,tc,tr'};   
  107. 107.                if(lis.w.indexOf(_method.className)>-1){   
  108. 108.                    var w=e.clientX-oldX+oldW;   
  109. 109.                    ww=w<minW?minW:w>divW-oldL?divW-oldL:w;   
  110. 110.                    ul.style.width=w+'px';   
  111. 111.                }   
  112. 112.                if(lis.h.indexOf(_method.className)>-1){   
  113. 113.                    var h=e.clientY-oldY+oldH;   
  114. 114.                    hh=h<minH?minH:h>divH-oldT?divH-oldT:h;   
  115. 115.                    ul.style.height=h+'px';   
  116. 116.                    if(!-[1,]&&!window.XMLHttpRequest){cDiv.style.height=h+'px';}//ie6下高度不变bug   
  117. 117.                }   
  118. 118.                if(lis.l.indexOf(_method.className)>-1){   
  119. 119.                    var l=e.clientX-oldX+oldL;   
  120. 120.                    ll=l<0?0:l>oldW+oldL-minW?oldW+oldL-minW:l;   
  121. 121.                    ul.style.left=l+'px';   
  122. 122.                    ul.style.width=oldW+oldL-l+'px';   
  123. 123.                }   
  124. 124.                if(lis.t.indexOf(_method.className)>-1){   
  125. 125.                    var t=e.clientY-oldY+oldT;   
  126. 126.                    tt=t<0?0:t>oldH+oldT-minH?oldH+oldT-minH:t;   
  127. 127.                    ul.style.top=t+'px';   
  128. 128.                    ul.style.height=oldH+oldT-t+'px';   
  129. 129.                    if(!-[1,]&&!window.XMLHttpRequest){cDiv.style.height=oldH+oldT-t+'px';}//ie6下高度不变bug   
  130. 130.                }   
  131. 131.                see();   
  132. 132.            };   
  133. 133.            clear(this);   
  134. 134.        };   
  135. 135.    }   
  136. 136.    see();   
  137. 137.})();   
  138. 138.</script>  
  139. 139.</body>  
  140. 140.</html>  
  141. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  142. <html xmlns="http://www.w3.org/1999/xhtml">
  143. <head>
  144. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  145. <title>无标题文档</title>
  146. <style type="text/css">
  147. *{margin:0; padding:0;}
  148. #picDiv{width:703px; height:338px; background:#fff; border:1px solid #999; margin:100px auto; position:relative; overflow:hidden;}
  149. #picDiv ul{width:200px; height:200px; border:1px dashed #fff; cursor:move; list-style:none; position:relative; z-index:200;}
  150. #picDiv li{width:6px; height:6px; background:#fff; border:1px solid #333; margin:-4px 0 0 -4px; overflow:hidden; position:absolute; opacity:0.5; filter:alpha(opacity=50);}
  151. #picDiv li.tl{cursor:nw-resize; top:0%; left:0%;}
  152. #picDiv li.tc{cursor:n-resize; top:0%; left:50%;}
  153. #picDiv li.tr{cursor:ne-resize; top:0%; left:100%;}
  154. #picDiv li.ml{cursor:w-resize; top:50%; left:0%;}
  155. #picDiv li.mr{cursor:e-resize; top:50%; left:100%;}
  156. #picDiv li.fl{cursor:sw-resize; top:100%; left:0%;}
  157. #picDiv li.fc{cursor:s-resize; top:100%; left:50%;}
  158. #picDiv li.fr{cursor:se-resize; top:100%; left:100%;}
  159. </style>
  160. </head>

  161. <body>
  162. <div id="picDiv">
  163.         <ul>
  164.             <li class="tl"></li>
  165.             <li class="tc"></li>
  166.             <li class="tr"></li>
  167.             <li class="ml"></li>
  168.             <li class="mr"></li>
  169.             <li class="fl"></li>
  170.             <li class="fc"></li>
  171.             <li class="fr"></li>
  172.     </ul>
  173.     <img src="../images/2.jpg" style="position:absolute; top:0; left:0; opacity:0.3; filter:alpha(opacity=30); z-index:10;" />
  174.     <img src="../images/2.jpg" style="position:absolute; top:0; left:0; clip:rect(0 0 0 0); z-index:100;" />
  175. </div>
  176. <form action="41.php" method="post">
  177.     <input type="submit" value=" 提 交 " />
  178.     <input type="hidden" id="imgInfor" name="imgInfor" />
  179. </form>
  180. <script type="text/javascript">
  181. (function(){
  182.         var div=document.getElementById('picDiv'),
  183.                 ul=div.getElementsByTagName('ul')[0],
  184.                 li=ul.getElementsByTagName('li');
  185.                 divW=div.offsetWidth-2,
  186.                 divH=div.offsetHeight-2,
  187.                 see=function(){
  188.                         var t=ul.offsetTop,
  189.                                 l=ul.offsetLeft,
  190.                                 r=l+ul.offsetWidth,
  191.                                 f=t+ul.offsetHeight;
  192.                         div.getElementsByTagName('img')[1].style.clip='rect('+t+'px '+r+'px '+f+'px '+l+'px)';
  193.                         document.getElementById('imgInfor').value="{'x':'"+l+"','y':'"+t+"','w':'"+ul.offsetWidth+"','h':'"+ul.offsetHeight+"','url':'"+div.getElementsByTagName('img')[1].src+"'}";
  194.                         window.getSelection?window.getSelection().removeAllRanges():document.selection.empty();
  195.                 },
  196.                 clear=function(o){
  197.                         if(!-[1,]){o.setCapture();}//学增加丢失焦点事件
  198.                         document.onmouseup=function(){
  199.                                 if(!-[1,]){o.releaseCapture();}
  200.                                 document.onmousemove=null;
  201.                                 document.onmouseup=null;
  202.                         };
  203.                         return false;//webkit中去掉默认事件,使鼠标不会变成默认文本光标
  204.                 };
  205.         if(!-[1,]){
  206.                 var cDiv=document.createElement('div');
  207.                 cDiv.style.cssText='width:100%; height:100%; background:#fff; opacity:0; filter:alpha(opacity=0); font-size:0;';
  208.                 ul.appendChild(cDiv);
  209.         }
  210.         ul.onmousedown=function(e){
  211.                 e=e||window.event;
  212.                 var oldX=e.clientX-ul.offsetLeft,
  213.                         oldY=e.clientY-ul.offsetTop,
  214.                         maxW=divW-ul.offsetWidth,
  215.                         maxH=divH-ul.offsetHeight;
  216.                 document.onmousemove=function(e){
  217.                         e=e||window.event;
  218.                         var newX=e.clientX-oldX,newY=e.clientY-oldY;
  219.                         newX=newX<0?0:newX>maxW?maxW:newX;
  220.                         newY=newY<0?0:newY>maxH?maxH:newY;
  221.                         ul.style.top=newY+'px';
  222.                         ul.style.left=newX+'px';
  223.                         see();
  224.                 };
  225.                 clear(this);
  226.         };
  227.         for(var i in li){
  228.                 li[i].onmousedown=function(e){
  229.                         e=e||window.event;
  230.                         var oldX=e.clientX,
  231.                                 oldY=e.clientY,
  232.                                 oldT=ul.offsetTop+2,
  233.                                 oldL=ul.offsetLeft+2,
  234.                                 oldW=ul.offsetWidth,
  235.                                 oldH=ul.offsetHeight,
  236.                                 minW=50,minH=50,
  237.                                 _method=this;
  238.                         if(e.stopPropagation){
  239.                                 e.stopPropagation();
  240.                         }else{
  241.                                 e.cancelBubble=true;
  242.                         }
  243.                         document.onmousemove=function(e){
  244.                                 e=e||window.event;
  245.                                 var lis={'w':'tr,mr,fr','h':'fl,fc,fr','l':'tl,ml,fl','t':'tl,tc,tr'};
  246.                                 if(lis.w.indexOf(_method.className)>-1){
  247.                                         var w=e.clientX-oldX+oldW;
  248.                                         w=w<minW?minW:w>divW-oldL?divW-oldL:w;
  249.                                         ul.style.width=w+'px';
  250.                                 }
  251.                                 if(lis.h.indexOf(_method.className)>-1){
  252.                                         var h=e.clientY-oldY+oldH;
  253.                                         h=h<minH?minH:h>divH-oldT?divH-oldT:h;
  254.                                         ul.style.height=h+'px';
  255.                                         if(!-[1,]&&!window.XMLHttpRequest){cDiv.style.height=h+'px';}//ie6下高度不变bug
  256.                                 }
  257.                                 if(lis.l.indexOf(_method.className)>-1){
  258.                                         var l=e.clientX-oldX+oldL;
  259.                                         l=l<0?0:l>oldW+oldL-minW?oldW+oldL-minW:l;
  260.                                         ul.style.left=l+'px';
  261.                                         ul.style.width=oldW+oldL-l+'px';
  262.                                 }
  263.                                 if(lis.t.indexOf(_method.className)>-1){
  264.                                         var t=e.clientY-oldY+oldT;
  265.                                         t=t<0?0:t>oldH+oldT-minH?oldH+oldT-minH:t;
  266.                                         ul.style.top=t+'px';
  267.                                         ul.style.height=oldH+oldT-t+'px';
  268.                                         if(!-[1,]&&!window.XMLHttpRequest){cDiv.style.height=oldH+oldT-t+'px';}//ie6下高度不变bug
  269.                                 }
  270.                                 see();
  271.                         };
  272.                         clear(this);
  273.                 };
  274.         }
  275.         see();
  276. })();
  277. </script>
  278. </body>
  279. </html>
  280.   
复制代码
这里是php处理的代码,具体图片自己弄吧~哈哈~后期会出功能多一点的



Php代码
  1. 1.<?php   
  2. 2.  
  3. 3.$img=json_decode(str_replace("\\'",'"',$_POST['imgInfor']),true);   
  4. 4.  
  5. 5.$old = imagecreatefromstring(file_get_contents($img['url']));   
  6. 6.  
  7. 7.if(function_exists("imagecreatetruecolor")){   
  8. 8.  $new=imagecreatetruecolor($img['w'],$img['h']); // 创建目标图gd2   
  9. 9.}else{   
  10. 10.  $new=imagecreate($img['w'],$img['h']); // 创建目标图gd1   
  11. 11.}   
  12. 12.imagecopyresampled ($new,$old,0,0,$img['x'],$img['y'],$img['w'],$img['h'],$img['w'],$img['h']);   
  13. 13.  
  14. 14.imagejpeg($new,'img\\'.getip().'_'.time().'.jpg');   
  15. 15.  
  16. 16.@header("Content-type: image/jpeg");   
  17. 17.  
  18. 18.imagejpeg($new);   
  19. 19.  
  20. 20.imagedestroy($new);   
  21. 21.      
  22. 22.function getip(){   
  23. 23.    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")){   
  24. 24.        $ip = getenv("HTTP_CLIENT_IP");   
  25. 25.    }else if(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){   
  26. 26.        $ip = getenv("HTTP_X_FORWARDED_FOR");   
  27. 27.    }else if(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")){   
  28. 28.        $ip = getenv("REMOTE_ADDR");   
  29. 29.    }else if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){   
  30. 30.        $ip = $_SERVER['REMOTE_ADDR'];   
  31. 31.    }else{   
  32. 32.        $ip = "unknown";   
  33. 33.    }   
  34. 34.    return ($ip);   
  35. 35.}   
  36. 36.?>  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-01-19 22:24 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP