头像裁剪工具目前比较流行的是flash和jquery的。个人觉得用jquery的比较好,因为代码仔细研究一下,基本上能明白怎么回事,想改的话也比较容易。
有一个例子,请参考:jcrop例子demo,是根jcrop的例子改的,添加以下二个特点:
1,居中显示,并且可拖拉,改变截取的大小
2,预览的图片,根拖拉的大小成比例。
以下是js代码,作了简单的封装
- <script type="text/javascript">
- new cutImage().init();
- function cutImage(){
- var oop = this;
- this.option = {
- x:170,
- y:110,
- w:350,
- h:200,
- t:'target',
- p:'preview',
- o:'aa'
- }
- this.init = function(){
- oop.target();
- }
- this.target = function(){
- $('#'+oop.option['t']).Jcrop({
- onChange: oop.updatePreview,
- onSelect: oop.updatePreview,
- aspectRatio: 1,
- setSelect: [ oop.option['x'], oop.option['y'], oop.option['w'],oop.option['h'] ],
- bgFade: true,
- bgOpacity: .5
- });
- }
- this.updatePreview = function(obj){
- if (parseInt(obj.w) > 0)
- {
- var rx = $('#'+oop.option['o']).width()/ obj.w;
- var ry = $('#'+oop.option['o']).height()/ obj.h;
- $('#'+oop.option['p']).css({
- width: Math.round(rx*$('#'+oop.option['t']).width()) + 'px',
- height: Math.round(ry*$('#'+oop.option['t']).height()) + 'px',
- marginLeft: '-' + Math.round(rx * obj.x) + 'px',
- marginTop: '-' + Math.round(ry * obj.y) + 'px'
- });
- }
- }
- }
- </script>
jquery jcrop结合jquery ajax upload的话,可以使头像上传截取功能,很人性化。
转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/jsjquery/1456.html
这个只有预览,没有截取功能,截取功能怎么做呢?如果为传入1880*1440的大型图片,截取一部分,怎么保证显示在180*180的框内保证图片不会萎缩?