欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery 圖片截取工具jquery.imagecropper.js

 更新時(shí)間:2010年04月09日 16:44:29   作者:  
工作需要參考網(wǎng)上的一些代碼做了個圖片截取工具,最后干脆封裝成一個jquery的插件。
除了jquery,本插件還引用了UI庫,包括ui.draggable.js
ImageCropper 演示需要asp.net支持。測試通過
ImageCropper 下載 http://www.dbjr.com.cn/jiaoben/25688.html
插件用法:
復(fù)制代碼 代碼如下:

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

要注意的是此插件只應(yīng)用在有src屬性的img標(biāo)簽上。
通過插件輸出的參數(shù),即可以通過服務(wù)器端代碼截取圖片,比如:
復(fù)制代碼 代碼如下:

$('#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:
復(fù)制代碼 代碼如下:

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;
}
}
}

重點(diǎn)是插件,因?yàn)樵创a注釋比較全,直接貼代碼在這:
復(fù)制代碼 代碼如下:

/**
* 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,
//工作區(qū)大小
cropWorkAreaSize: { width: 600, height: 400 },
//截取框相對于工作區(qū)的位置
cropFrameRect: { center: true, top: 0, left: 0 },
//縮放范圍
zoom: { min: 0, max: 2, step: 0.01 },
//回調(diào)函數(shù)
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: $('<div class="image-cropper-wrapper"/>'),
zoomWrapper: $('<div class="zoom-wrapper"><div class="zoom-out-button"/><div class="zoom-scrollbar"><div class="zoom-scroller"/></div><div class="zoom-in-button"/></div>'),
img: false,
init: function(image, settings) {
var context = this;
this.settings = settings;
image.addClass('background-img');
//生成html
image.wrap(this.wrapper).wrap('<div class="crop-work-area"/>').wrap('<div class="crop-background"/>');
this.wrapper = $('.image-cropper-wrapper');
$('.crop-work-area', this.wrapper).append('<div class="crop-frame"><img class="foreground-img" src="" /></div><div class="drag-containment"/>');
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');
//同時(shí)移動前景圖
$('.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'
});
//得到截圖左上點(diǎn)坐標(biāo)
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');
//同時(shí)移動背景
$('.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'
});
//得到截圖左上點(diǎn)坐標(biāo)
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);
}
});
//點(diǎn)擊縮放
$('.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);
//如果縮放級別超出范圍 或者 縮放導(dǎo)致圖片右下角沒在截取框內(nèi) 則取消縮放
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;
//如果縮放級別超出范圍 或者 縮放導(dǎo)致圖片右下角沒在截取框內(nèi) 則取消縮放
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;
//截取框大于工作區(qū),則放大工作區(qū)
var tempSize = {
width: Math.max(this.settings.cropWorkAreaSize.width, this.settings.width),
height: Math.max(this.settings.cropWorkAreaSize.height, this.settings.height)
};
//如果截取框在工作區(qū)中居中,則重新設(shè)置截取框的位置
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;
}
//如果截取框在圖片中居中,則重新設(shè)置圖片與截取框的相對位置
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);
//調(diào)整拖動限制框
$('.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);

相關(guān)文章

  • jQuery Pagination分頁插件_動力節(jié)點(diǎn)Java學(xué)院整理

    jQuery Pagination分頁插件_動力節(jié)點(diǎn)Java學(xué)院整理

    此jQuery插件為Ajax分頁插件,一次性加載,故分頁切換時(shí)無刷新與延遲,如果數(shù)據(jù)量較大不建議用此方法,因?yàn)榧虞d會比較慢。下面通過本文給大家分享jQuery Pagination分頁插件的使用方法及參數(shù)介紹,感興趣的朋友一起看看吧
    2017-07-07
  • jQuery實(shí)現(xiàn)王者榮耀手風(fēng)琴效果

    jQuery實(shí)現(xiàn)王者榮耀手風(fēng)琴效果

    這篇文章主要介紹了jQuery實(shí)現(xiàn)王者榮耀手風(fēng)琴效果的代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-01-01
  • jQuery獲取樣式中顏色值的方法

    jQuery獲取樣式中顏色值的方法

    這篇文章主要介紹了jQuery獲取樣式中顏色值的方法,可實(shí)現(xiàn)針對IE與Chrome、Firefox等不同瀏覽器都可獲取css樣式中background-color值的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-01-01
  • 20個非常棒的Jquery實(shí)用工具 國外文章

    20個非常棒的Jquery實(shí)用工具 國外文章

    網(wǎng)站設(shè)計(jì)者往往會設(shè)計(jì)一些小的工具類(widgets)或者一些可復(fù)用的程序,從而使頁面更楚楚動人,更吸引瀏覽者駐足。這里收集了20個常用Jquery工具類,這些小的工具可以幫助網(wǎng)站設(shè)計(jì)人員和站長非常容易地創(chuàng)建漂亮的站點(diǎn)。
    2010-01-01
  • jquery select2的使用心得(推薦)

    jquery select2的使用心得(推薦)

    下面小編就為大家?guī)硪黄猨query select2的使用心得(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • jQuery Layer彈出層傳值到父頁面的實(shí)現(xiàn)代碼

    jQuery Layer彈出層傳值到父頁面的實(shí)現(xiàn)代碼

    這篇文章主要介紹了jQuery Layer彈出層傳值到父頁面的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2017-08-08
  • jQuery 源碼分析筆記(4) Ready函數(shù)

    jQuery 源碼分析筆記(4) Ready函數(shù)

    接下來回到最開始的jQuery.extend函數(shù)(268行)中的ready(fn)的定義。這個函數(shù)用來處理DOM加載完成的事件。差不多是jQuery最常用的函數(shù)之一了。
    2011-06-06
  • jquery數(shù)組之存放checkbox全選值示例代碼

    jquery數(shù)組之存放checkbox全選值示例代碼

    使用jquery數(shù)組可以存放checkbox全選值,下面有個不錯的示例,感興趣的朋友可以參考下
    2013-12-12
  • jQuery移動web開發(fā)中的頁面初始化與加載事件

    jQuery移動web開發(fā)中的頁面初始化與加載事件

    這篇文章主要介紹了jQuery移動web開發(fā)中的頁面初始化與加載事件,是JavaScript移動端頁面開發(fā)學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-12-12
  • checkbox:click事件觸發(fā)span元素內(nèi)容改變的方法

    checkbox:click事件觸發(fā)span元素內(nèi)容改變的方法

    下面小編就為大家?guī)硪黄猚heckbox:click事件觸發(fā)span元素內(nèi)容改變的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09

最新評論