除了jquery,本插件还引用了UI库,包括ui.draggable.js

ImageCropper 演示需要asp.net支持。测试通过

ImageCropper 下载 https://www.jb51.net/jiaoben/25688.html

插件用法:

var imageCropper = $('#imgBackground').imageCropper();

要注意的是此插件只应用在有src属性的img标签上。

通过插件输出的参数,即可以通过服务器端代码截取图片,比如:

$('#imgCroppedImage').attr('src', 'CropImage.ashx?p=' + imageCropper.settings.imagePath + '&z=' + imageCropper.settings.zoomLevel + '&t=' + imageCropper.settings.top + '&l=' + imageCropper.settings.left + '&w=' + imageCropper.settings.width + '&h=' + imageCropper.settings.height + '&' + Math.random());

asp.net hander CropImage.ashx:

public class CropImage : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

string imgPath = Convert.ToString(context.Request["p"]);

float zoomLevel = Convert.ToSingle(context.Request["z"]);

int top = Convert.ToInt32(context.Request["t"]);

int left = Convert.ToInt32(context.Request["l"]);

int width = Convert.ToInt32(context.Request["w"]);

int height = Convert.ToInt32(context.Request["h"]);

context.Response.ContentType = "image/jpeg";

Crop(HttpContext.Current.Server.MapPath(imgPath), zoomLevel, top, left, width, height).WriteTo(context.Response.OutputStream);

}

public MemoryStream Crop(string imgPath, float zoomLevel, int top, int left, int width, int height)

{

Image img = Image.FromFile(imgPath);

Bitmap bitmap = new Bitmap(width, height);

Graphics g = Graphics.FromImage(bitmap);

g.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle((int)(left / zoomLevel), (int)(top / zoomLevel), (int)(width / zoomLevel), (int)(height / zoomLevel)), GraphicsUnit.Pixel);

MemoryStream ms = new MemoryStream();

bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

img.Dispose();

g.Dispose();

bitmap.Dispose();

return ms;

}

public bool IsReusable

{

get

{

return false;

}

}

}

重点是插件,因为源代码注释比较全,直接贴代码在这:

/**

* Copyright (c) 2010 Viewercq (http://www.cnblogs.com/viewercq/archive/2010/04/04/1704093.html)

* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)

* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.

*

* Version: 1.0

*

* Demo: https://dl.dropbox.com/u/4390741/ImageCropper.htm

*/

; (function($) {

$.fn.extend({

imageCropper: function(options) {

if (!this.is('img') || typeof this.attr('src') == 'undefined' || this.attr('src') == '') {

throw 'Please notice that this jquery plguin only could be applied to img and the src of img could not be null!';

}

var defaults = {

//原图路径

imagePath: this.attr('src'),

//缩放级别

zoomLevel: 1,

//图片相对于截取框是否居中

center: false,

//截取框与图片的相对位置

left: 0, top: 0,

//截取框的大小

width: 200, height: 200,

//工作区大小

cropWorkAreaSize: { width: 600, height: 400 },

//截取框相对于工作区的位置

cropFrameRect: { center: true, top: 0, left: 0 },

//缩放范围

zoom: { min: 0, max: 2, step: 0.01 },

//回调函数

callbacks: {

//移动图片后

dragging: false,

//缩放后

zoomed: false

}

};

if (options) {

defaults = $.extend(defaults, options);

}

return new imageCropper(this, defaults);

}

});

function imageCropper(image, settings) {

this.init(image, settings);

};

imageCropper.prototype = {

settings: false,

wrapper: $('

zoomWrapper: $('

img: false,

init: function(image, settings) {

var context = this;

this.settings = settings;

image.addClass('background-img');

//生成html

image.wrap(this.wrapper).wrap('

this.wrapper = $('.image-cropper-wrapper');

$('.crop-work-area', this.wrapper).append('

this.wrapper.append(this.zoomWrapper);

$('.image-cropper-wrapper', this.wrapper).disableSelection();

this.reset();

//图片的拖动

$('.crop-background', this.wrapper).draggable({

containment: $('.drag-containment', this.wrapper),

cursor: 'move',

drag: function(event, ui) {

var self = $(this).data('draggable');

//同时移动前景图

$('.foreground-img', this.wrapper).css({

left: (parseInt(self.position.left) - context.settings.cropFrameRect.left - 1) + 'px',

top: (parseInt(self.position.top) - context.settings.cropFrameRect.top - 1) + 'px'

});

//得到截图左上点坐标

context.settings.left = context.settings.cropFrameRect.left - parseInt($(this).css('left'));

context.settings.top = context.settings.cropFrameRect.top - parseInt($(this).css('top'));

//移动图片的callback

context.fireCallback(context.settings.callbacks.dragging);

}

});

$('.foreground-img', this.wrapper).draggable({

containment: $('.drag-containment', this.wrapper),

cursor: 'move',

drag: function(event, ui) {

var self = $(this).data('draggable');

//同时移动背景

$('.crop-background', this.wrapper).css({

left: (parseInt(self.position.left) + context.settings.cropFrameRect.left + 1) + 'px',

top: (parseInt(self.position.top) + context.settings.cropFrameRect.top + 1) + 'px'

});

//得到截图左上点坐标

context.settings.left = context.settings.cropFrameRect.left - parseInt($('.crop-background', this.wrapper).css('left'));

context.settings.top = context.settings.cropFrameRect.top - parseInt($('.crop-background', this.wrapper).css('top'));

//移动图片的callback

context.fireCallback(context.settings.callbacks.dragging);

}

});

//点击缩放

$('.zoom-out-button,.zoom-in-button', this.wrapper).click(function() {

var step = $(this).hasClass('zoom-in-button') ? context.settings.zoom.step : -context.settings.zoom.step;

var tempZoomLevel = context.formatNumber(context.settings.zoomLevel + step, 3);

//如果缩放级别超出范围 或者 缩放导致图片右下角没在截取框内 则取消缩放

if (context.settings.zoomLevel >= context.settings.zoom.min

&& context.settings.zoomLevel <= context.settings.zoom.max

&& parseInt($('.crop-background', this.wrapper).css('left')) + tempZoomLevel * context.img.width > context.settings.cropFrameRect.left + context.settings.width

&& parseInt($('.crop-background', this.wrapper).css('top')) + tempZoomLevel * context.img.height > context.settings.cropFrameRect.top + context.settings.height

) {

context.settings.zoomLevel = tempZoomLevel;

context.zoom(context.img.width * context.settings.zoomLevel, context.img.height * context.settings.zoomLevel);

$('.zoom-scroller', this.wrapper).css('left', context.settings.zoomLevel * 200 / (context.settings.zoom.max - context.settings.zoom.min) + 'px');

}

context.fireCallback(context.settings.callbacks.zoomed);

});

//滚动条缩放

var cancelZoomScroll = false;

$('.zoom-scroller', this.wrapper).draggable({

containment: $('.zoom-scrollbar', this.wrapper),

axis: 'x',

drag: function(event, ui) {

var tempZoomLevel = (context.settings.zoom.max - context.settings.zoom.min) * parseInt($(this).css('left')) / 200;

//如果缩放级别超出范围 或者 缩放导致图片右下角没在截取框内 则取消缩放

if (parseInt($('.crop-background', this.wrapper).css('left')) + tempZoomLevel * context.img.width > context.settings.cropFrameRect.left + context.settings.width

&& parseInt($('.crop-background', this.wrapper).css('top')) + tempZoomLevel * context.img.height > context.settings.cropFrameRect.top + context.settings.height

) {

context.settings.zoomLevel = tempZoomLevel;

context.zoom(context.img.width * context.settings.zoomLevel, context.img.height * context.settings.zoomLevel);

cancelZoomScroll = false;

context.fireCallback(context.settings.callbacks.zoomed);

}

else {

cancelZoomScroll = true;

}

},

stop: function(event, ui) {

//如果缩放级别无效 则重置滚动条的值

if (cancelZoomScroll) {

$('.zoom-scroller', this.wrapper).css('left', context.settings.zoomLevel * 200 / (context.settings.zoom.max - context.settings.zoom.min) + 'px');

}

}

});

},

reset: function() {

this.img = new Image();

this.img.src = this.settings.imagePath;

//截取框大于工作区,则放大工作区

var tempSize = {

width: Math.max(this.settings.cropWorkAreaSize.width, this.settings.width),

height: Math.max(this.settings.cropWorkAreaSize.height, this.settings.height)

};

//如果截取框在工作区中居中,则重新设置截取框的位置

if (this.settings.cropFrameRect.center) {

this.settings.cropFrameRect.left = (tempSize.width - this.settings.width) / 2;

this.settings.cropFrameRect.top = (tempSize.height - this.settings.height) / 2;

}

//如果截取框在图片中居中,则重新设置图片与截取框的相对位置

if (this.settings.center) {

this.settings.left = (this.img.width * this.settings.zoomLevel - this.settings.width) / 2;

this.settings.top = (this.img.height * this.settings.zoomLevel - this.settings.height) / 2;

}

this.wrapper.width(tempSize.width + 2).height(tempSize.height + 25);

$('.foreground-img,.background-img', this.wrapper).attr('src', this.settings.imagePath);

$('.crop-work-area', this.wrapper).width(tempSize.width).height(tempSize.height);

$('.crop-frame', this.wrapper).css({

left: this.settings.cropFrameRect.left + 'px',

top: this.settings.cropFrameRect.top + 'px',

width: this.settings.width + 'px',

height: this.settings.height + 'px'

});

$('.foreground-img', this.wrapper).css({

left: (-this.settings.cropFrameRect.left - 1) + 'px',

top: (-this.settings.cropFrameRect.top - 1) + 'px'

});

$('.zoom-scroller', this.wrapper).css('left', this.settings.zoomLevel * 200 / (this.settings.zoom.max - this.settings.zoom.min) + 'px');

$('.crop-background', this.wrapper).css({

opacity: 0.3,

left: this.settings.cropFrameRect.left - this.settings.left + 'px',

top: this.settings.cropFrameRect.top - this.settings.top + 'px'

});

$('.foreground-img', this.wrapper).css({

left: -this.settings.left + 'px',

top: -this.settings.top + 'px'

});

this.settings.left = this.settings.cropFrameRect.left - parseInt($('.crop-background', this.wrapper).css('left'));

this.settings.top = this.settings.cropFrameRect.top - parseInt($('.crop-background', this.wrapper).css('top'));

this.zoom(this.img.width * this.settings.zoomLevel, this.img.height * this.settings.zoomLevel);

},

zoom: function(width, height) {

$('.crop-background, .background-img, .foreground-img', this.wrapper).width(width).height(height);

//调整拖动限制框

$('.drag-containment', this.wrapper).css({

left: this.settings.cropFrameRect.left + this.settings.width - this.settings.zoomLevel * this.img.width + 1 + 'px',

top: this.settings.cropFrameRect.top + this.settings.height - this.settings.zoomLevel * this.img.height + 1 + 'px',

width: 2 * this.settings.zoomLevel * this.img.width - this.settings.width + 'px',

height: 2 * this.settings.zoomLevel * this.img.height - this.settings.height + 'px'

});

},

formatNumber: function(number, bit) {

return Math.round(number * Math.pow(10, bit)) / Math.pow(10, bit);

},

fireCallback: function(fn) {

if ($.isFunction(fn)) {

fn.call(this);

};

}

};

})(jQuery);

jquery实现截取pc图片_jquery 图片截取工具jquery.imagecropper.js相关推荐

  1. body div js 放大图片_jquery图片放大插件鼠标悬停图片放大效果

    都知道jquery都插件是非常强大的,最近分享点jquery插件效果,方便效果开发使用. 一.HTML代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...

  2. jq双击放大图片_Jquery图片点击放大

    jquery点击放大整理 身份证图片 $(function(){ $(".pimg").click(function(){ var _this = $(this);//将当前的pi ...

  3. jquery实现截取pc图片_Cropper.js 实现裁剪图片并上传(PC端)

    由于之前做项目的时候有需求是需要实现裁剪图片来做头像并上传到服务器,所以上网查询了很多资料,也试用了许多案例,发现cropper插件裁剪是比较完善的,所以结合之前的使用情况,编写了此案例.本案例是参考 ...

  4. jquery实现截取pc图片_[置顶] JQuery在线截取图片

    JQuery在线截取图片 ASP.NET结构开发 1.在线截取 前几天看织梦CMS,有个截图功能挺好的,可以在线选取需要截取的部分图片,然后后台截取 2.开始正文 首先构建文档,样式 JQuery截取 ...

  5. jquery插件:图片截取工具jquery.imagecropper.js

    工作需要参考网上的一些代码做了个图片截取工具,最后干脆封装成一个jquery的插件. 除了jquery,本插件还引用了UI库,包括ui.draggable.js ImageCropper 演示 Ima ...

  6. 用FFmpeg从视频截取任意一帧图片的解决办法~

    From: http://www.cnblogs.com/yao/archive/2006/01/17/318772.html 在我的另一篇日志中,说到利用FFmpeg从视频截图的命令,那天在找从视频 ...

  7. python测试脚本截图_Python+selenium实现截图图片并保存截取的图片

    这篇文章介绍如何利用Selenium的方法进行截图,在测试过程中,是有必要截图,特别是遇到错误的时候进行截图.在selenium for Python中主要有三个截图方法,我们挑选其中最常用的一种. ...

  8. js截取图片 裁剪图片之cropper.js插件用法详解

    js截取图片 裁剪图片之cropper.js插件用法详解 源码:https://github.com/fengyuanchen/cropper 引入+使用 <link href="/p ...

  9. 在线文件/文档预览/分页分片预览 之开源kkfileview(word转pdf,pdf截取,pdf转图片,Aspose jobConverter , OpenOffice ,libreoffice )

    前提说明 浏览器不能直接浏览word文件,但可以浏览pdf文件!!! 可以后台把word,excel 转成成pdf.然后给前端预览: 业界常用的开源工具有:Aspose jobConverter ,  ...

最新文章

  1. mysql查询表的数据大小
  2. 超过AttGAN,谷歌推出生成文本到图像的新框架 TReCS
  3. TortoiseGit 连接oschina不用每次输入username和password的方法
  4. iPhone开发各种图标大小
  5. EasyNetQ介绍
  6. 从人工到机器智能,盗版监测在 AI 时代如何破局?
  7. 新建网站与新建Asp.Net+Web+应用程序的区别
  8. [react] immutable的原理是什么
  9. JavaScript四大家族之scroll家族
  10. C++ 出现异常“.... \debug_heap.cpp Line:980 Expression:__acrt_first_block==header“
  11. 图文解说:Discuz论坛基础设置第一弹
  12. 下载centos(阿里云)
  13. 搜狗微信文章爬取(下)
  14. JavaScript笔记 03:数组
  15. 开源 == 文化:红帽社区开放日图文回顾
  16. 每日方法分享:免费一键抠图方法都有哪些?
  17. 修改安卓默认的系统button样式,以及其它系统控件的默认样式
  18. C#中File和FileInfo的区别和用法
  19. 阿里云-GPU/ASK/ACK/NAS/Docker
  20. 内存管理-内存池的实现

热门文章

  1. 汽车电子_EMC测试_CE试验整改
  2. 设置更换 PyCharm 的主题和字体与下载导入其他PyCharm主题
  3. java 递归和迭代的区别
  4. winpcap 查询 活动网卡_社保卡20周年惠民服务季之腾讯活动:社会保障卡的20年...
  5. 【讲清楚,说明白!】使用iperf3监测网络吞吐量
  6. 脑电参考之零参考技术
  7. 2022年湖南大学信息科学与工程学院计算机考博申博经验分享
  8. Swift 引入第三方字体库
  9. 手机SD卡数据恢复【图文教程】
  10. ​力扣解法汇总792. 匹配子序列的单词数