Jcrop简单实用

今天有一个项目的功能需求 “在上传照片的时候能进行裁剪”,网上找了下,发现有Jcrop这款插件,自己试了下,感觉很不错,蛮好用的。又能增加用户体验,测试了兼容性也很好,所以在这里分享下

 首先,可以到官网上下载这款插件 htttp://www.jquery.com ,下载后有需要的话可以看下里面的demo

在项目中,只要在head中引用

    <link href="jquery.Jcrop.css" rel="stylesheet" type="text/css" />
    <script src="jquery-1.7.1.js" type="text/javascript"></script>
    <script src="jquery.Jcrop.js" type="text/javascript"></script>

前台页面,要加上4个隐藏域,分别记录剪切后的x,y坐标和长,宽。附上代码

    <div>
    <!--需要剪切的图片-->
        <img src="gq_nav.jpg" alt="" id="TestImage" style="float: left;">
    </div>
    <form id="AvatarForm" action="">
    <input id="x" name="x" type="hidden"/>
    <input id="y" name="y" type="hidden"/>
    <input id="w" name="w" type="hidden"/>
    <input id="h" name="h" type="hidden"/>
    <input class="input_btn" id="BtnSaveAvatar" value="确定保存" type="submit"/>
    </form>
    <!--简介后显示的图片-->
    <img id="CutImage" />

接着实例化Jcrop

 

   <script>
        $(document).ready(function () {
            $('#TestImage').Jcrop({
                onChange: updateCoords,
                onSelect: updateCoords
            });
            $("#BtnSaveAvatar").click(function () {
                $.ajax({
                    url: 'Handler.ashx',
                    data: { 'x': $("#x").val(), 'y': $("#y").val(), 'w': $("#w").val(), 'h': $("#h").val() },
                    datatype: "text/json",
                    type: 'post',
                    success: function (imgUrl) {
                        $("#CutImage").attr("src", imgUrl);
                    }
                });
                return false;
            });
        });
        function updateCoords(c) {
            $('#x').val(c.x);
            $('#y').val(c.y);
            $('#w').val(c.w);
            $('#h').val(c.h);
        };
    </script>

 

之后看下运行效果图

好了,效果出来了,然后要保存截取后的图片了,这样在刚刚的脚本中可以看到用ajax提交到Handler.ashx上去处理,附上代码

 

public void ProcessRequest(HttpContext context)
    {
        
        //接收位置x,y。
        //接收长宽w,h。
        string xstr = context.Request["x"];
        string ystr = context.Request["y"];
        string wstr = context.Request["w"];
        string hstr = context.Request["h"];
        //获取原始图片
        string sourceFile = context.Server.MapPath("gq_nav.jpg");
        //图片路径
        string savePath = "CutImage/" + Guid.NewGuid() + ".jpg";
        int x = 0;
        int y = 0;
        int w = 1;
        int h = 1;
        try
        {
            x = int.Parse(xstr);
            y = int.Parse(ystr);
            w = int.Parse(wstr);
            h = int.Parse(hstr);
        }
        catch { }
        ImageCut ic = new ImageCut(x, y, w, h);
        System.Drawing.Bitmap cuted = ic.KiCut(new System.Drawing.Bitmap(sourceFile));
        string cutPath = context.Server.MapPath(savePath); 
        cuted.Save(cutPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        context.Response.Write(savePath);    //输出保存的路径,以便页面端接收路径显示切割后的图片
    }

Handler.ashx中可以看到生成新的图片是交给ImageCut这个类去做的,这里也附上代码

 

public class ImageCut
{
    ///<summary>
    /// 剪裁 -- 用GDI+
    ///</summary>
    ///<param name="b">原始Bitmap</param>
    ///<param name="StartX">开始坐标X</param>
    ///<param name="StartY">开始坐标Y</param>
    ///<param name="iWidth">宽度</param>
    ///<param name="iHeight">高度</param>
    ///<returns>剪裁后的Bitmap</returns>
    public Bitmap KiCut(Bitmap b)
    {
        if (b == null)
        {
            return null;
        }

        int w = b.Width;
        int h = b.Height;

        if (X >= w || Y >= h)
        {
            return null;
        }

        if (X + Width > w)
        {
            Width = w - X;
        }

        if (Y + Height > h)
        {
            Height = h - Y;
        }

        try
        {
            Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);

            Graphics g = Graphics.FromImage(bmpOut);
            g.DrawImage(b, new Rectangle(0, 0, Width, Height), new Rectangle(X, Y, Width, Height), GraphicsUnit.Pixel);
            g.Dispose();

            return bmpOut;
        }
        catch
        {
            return null;
        }
    }

    public int X = 0;
    public int Y = 0;
    public int Width = 120;
    public int Height = 120;
    public ImageCut(int x, int y, int width, int heigth)
    {
        X = x;
        Y = y;
        Width = width;
        Height = heigth;
    }
}

 

这样就可以实现在线剪裁图片的效果了。因为觉得这款插件还算不错,所以特此分享下,希望大师们不要取笑,如果哪位有更好的建议或其他类似的插件,也希望
能分享给小弟。

另:附上这个插件的属性

allowSelecttrue允许新选框
allowMovetrue允许选框移动
allowResizetrue允许选框缩放
trackDocumenttrue 
baseClass“jcrop”基础样式名前缀。说明:class=”jcrop-holder”,更改的只是其中的 jcrop。
addClassnull添加样式。例:假设值为 “test”,那么会添加样式到
bgColor“black”背景颜色。颜色关键字、HEX、RGB 均可。
bgOpacity0.6背景透明度
bgFadefalse使用背景过渡效果
borderOpacity0.4选框边框透明度
handleOpacity0.5缩放按钮透明度
handleSize9缩放按钮大小
handleOffset5缩放按钮与边框的距离
aspectRatio0选框宽高比。说明:width/height
keySupporttrue支持键盘控制。按键列表:上下左右(移动)、Esc(取消)、Tab(跳出裁剪框,到下一个)
cornerHandlestrue允许边角缩放
sideHandlestrue允许四边缩放
drawBorderstrue绘制边框
dragEdgestrue允许拖动边框
fixedSupporttrue 
touchSupportnull 
boxWidth0画布宽度
boxHeight0画布高度
boundary2边界。说明:可以从边界开始拖动鼠标选择裁剪区域
fadeTime400过度效果的时间
animationDelay20动画延迟
swingSpeed3过渡速度
minSelect[0,0]选框最小选择尺寸。说明:若选框小于该尺寸,则自动取消选择
maxSize[0,0]选框最大尺寸
minSize[0,0]选框最小尺寸
onChangefunction(){}选框改变时的事件
onSelectfunction(){}选框选定时的事件
onReleasefunction(){}取消选框时的事件

 

API 接口

 

名称说明
setImage(string)设定(或改变)图像。例:jcrop_api.setImage(“newpic.jpg”)
setOptions(object)设定(或改变)参数,格式与初始化设置参数一样
setSelect(array)创建选框,参数格式为:[x,y,x2,y2]
animateTo(array)用动画效果创建选框,参数格式为:[x,y,x2,y2]
release()取消选框
disable()禁用 Jcrop。说明:已有选框不会被清除。
enable()启用 Jcrop
destroy()移除 Jcrop
tellSelect()获取选框的值(实际尺寸)。例子:console.log(jcrop_api.tellSelect())
tellScaled()获取选框的值(界面尺寸)。例子:console.log(jcrop_api.tellScaled())
getBounds()获取图片实际尺寸,格式为:[w,h]
getWidgetSize()获取图片显示尺寸,格式为:[w,h]
getScaleFactor()获取图片缩放的比例,格式为:[w,h]

 

转载于:https://www.cnblogs.com/qtqq/p/3711076.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值