jquery實(shí)現(xiàn)圖片裁剪思路及實(shí)現(xiàn)
更新時(shí)間:2013年08月16日 18:04:40 作者:
JS,jquery不能實(shí)現(xiàn)圖片的裁剪,只是顯示了一個(gè)假象,在服務(wù)器上用獲得的各個(gè)坐標(biāo)值,以及原始圖片,用JAVA進(jìn)行裁剪
思路:JS,jquery不能實(shí)現(xiàn)圖片的裁剪,只是顯示了一個(gè)假象
我的實(shí)現(xiàn)的方式大體如下:
1.將用戶選中的圖片上傳到服務(wù)器
2.將選中頭像的矩形框在圖片上的坐標(biāo)發(fā)送到服務(wù)器,包括,矩形框的寬,高,左上角的x,y坐標(biāo),圖片的高,寬等
3.在服務(wù)器上用獲得的各個(gè)坐標(biāo)值,以及原始圖片,用JAVA進(jìn)行裁剪。
-----------------------------------
jquery 裁剪效果顯示,利用imgAreaSelect
http://odyniec.net/projects/imgareaselect/
http://www.cnblogs.com/mizzle/archive/2011/10/13/2209891.html
效果如下:
JAVA實(shí)現(xiàn)裁剪圖片
http://blog.csdn.net/renfyjava/article/details/9942743
我的例子:
我的是同時(shí)顯示4個(gè)尺寸的頭像,部分代碼如下:
<script type="text/javascript">
var $thumb_width = 180; //縮略圖大小
var $thumb_height = 180;
function preview(img, selection) {
var scaleX = $thumb_width / selection.width;
var scaleY = $thumb_height / selection.height;
var scaleX2 = 100 / selection.width;
var scaleY2 = 100 / selection.height;
var scaleX3 = 60 / selection.width;
var scaleY3 = 60 / selection.height;
var scaleX4 = 30 / selection.width;
var scaleY4 = 30 / selection.height;
$('#avatar180 img').css({
width: Math.round(scaleX * 300) + 'px', //獲取圖像的實(shí)際寬度
height: Math.round(scaleY * 300) + 'px', //獲取圖像的實(shí)際高度
marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
});
$('#avatar100 img').css({
width: Math.round(scaleX2 * 300) + 'px', //獲取圖像的實(shí)際寬度
height: Math.round(scaleY2 * 300) + 'px', //獲取圖像的實(shí)際高度
marginLeft: '-' + Math.round(scaleX2 * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY2 * selection.y1) + 'px'
});
$('#avatar60 img').css({
width: Math.round(scaleX3 * 300) + 'px', //獲取圖像的實(shí)際寬度
height: Math.round(scaleY3 * 300) + 'px', //獲取圖像的實(shí)際高度
marginLeft: '-' + Math.round(scaleX3 * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY3 * selection.y1) + 'px'
});
$('#avatar30 img').css({
width: Math.round(scaleX4 * 300) + 'px', //獲取圖像的實(shí)際寬度
height: Math.round(scaleY4 * 300) + 'px', //獲取圖像的實(shí)際高度
marginLeft: '-' + Math.round(scaleX4 * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY4 * selection.y1) + 'px'
});
$('#x1').val(selection.x1);
$('#y1').val(selection.y1);
$('#x2').val(selection.x2);
$('#y2').val(selection.y2);
$('#w').val(selection.width);
$('#h').val(selection.height);
}
$(document).ready(function () {
$('#save_thumb').click(function() {
var x1 = $('#x1').val();
var y1 = $('#y1').val();
var x2 = $('#x2').val();
var y2 = $('#y2').val();
var w = $('#w').val();
var h = $('#h').val();
var jyduploadfile = $('#jyduploadfile').val();
if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h=="" || jyduploadfile==""){
alert("請(qǐng)先選擇上傳圖片");
return false;
}else{
return true;
}
});
});
$(window).load(function () {
$('#picView').imgAreaSelect({ selectionColor: 'blue', x1:60, y1:60, x2: 240,
maxWidth:300,minWidth:100,y2:240,minHeight:100,maxHeight:300,
selectionOpacity: 0.2 , aspectRatio: '1:'+($thumb_height/$thumb_width)+'', onSelectChange: preview });
});
</script>
顯示效果:
我的實(shí)現(xiàn)的方式大體如下:
1.將用戶選中的圖片上傳到服務(wù)器
2.將選中頭像的矩形框在圖片上的坐標(biāo)發(fā)送到服務(wù)器,包括,矩形框的寬,高,左上角的x,y坐標(biāo),圖片的高,寬等
3.在服務(wù)器上用獲得的各個(gè)坐標(biāo)值,以及原始圖片,用JAVA進(jìn)行裁剪。
-----------------------------------
jquery 裁剪效果顯示,利用imgAreaSelect
http://odyniec.net/projects/imgareaselect/
http://www.cnblogs.com/mizzle/archive/2011/10/13/2209891.html
效果如下:
JAVA實(shí)現(xiàn)裁剪圖片
http://blog.csdn.net/renfyjava/article/details/9942743
我的例子:
我的是同時(shí)顯示4個(gè)尺寸的頭像,部分代碼如下:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
var $thumb_width = 180; //縮略圖大小
var $thumb_height = 180;
function preview(img, selection) {
var scaleX = $thumb_width / selection.width;
var scaleY = $thumb_height / selection.height;
var scaleX2 = 100 / selection.width;
var scaleY2 = 100 / selection.height;
var scaleX3 = 60 / selection.width;
var scaleY3 = 60 / selection.height;
var scaleX4 = 30 / selection.width;
var scaleY4 = 30 / selection.height;
$('#avatar180 img').css({
width: Math.round(scaleX * 300) + 'px', //獲取圖像的實(shí)際寬度
height: Math.round(scaleY * 300) + 'px', //獲取圖像的實(shí)際高度
marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
});
$('#avatar100 img').css({
width: Math.round(scaleX2 * 300) + 'px', //獲取圖像的實(shí)際寬度
height: Math.round(scaleY2 * 300) + 'px', //獲取圖像的實(shí)際高度
marginLeft: '-' + Math.round(scaleX2 * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY2 * selection.y1) + 'px'
});
$('#avatar60 img').css({
width: Math.round(scaleX3 * 300) + 'px', //獲取圖像的實(shí)際寬度
height: Math.round(scaleY3 * 300) + 'px', //獲取圖像的實(shí)際高度
marginLeft: '-' + Math.round(scaleX3 * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY3 * selection.y1) + 'px'
});
$('#avatar30 img').css({
width: Math.round(scaleX4 * 300) + 'px', //獲取圖像的實(shí)際寬度
height: Math.round(scaleY4 * 300) + 'px', //獲取圖像的實(shí)際高度
marginLeft: '-' + Math.round(scaleX4 * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY4 * selection.y1) + 'px'
});
$('#x1').val(selection.x1);
$('#y1').val(selection.y1);
$('#x2').val(selection.x2);
$('#y2').val(selection.y2);
$('#w').val(selection.width);
$('#h').val(selection.height);
}
$(document).ready(function () {
$('#save_thumb').click(function() {
var x1 = $('#x1').val();
var y1 = $('#y1').val();
var x2 = $('#x2').val();
var y2 = $('#y2').val();
var w = $('#w').val();
var h = $('#h').val();
var jyduploadfile = $('#jyduploadfile').val();
if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h=="" || jyduploadfile==""){
alert("請(qǐng)先選擇上傳圖片");
return false;
}else{
return true;
}
});
});
$(window).load(function () {
$('#picView').imgAreaSelect({ selectionColor: 'blue', x1:60, y1:60, x2: 240,
maxWidth:300,minWidth:100,y2:240,minHeight:100,maxHeight:300,
selectionOpacity: 0.2 , aspectRatio: '1:'+($thumb_height/$thumb_width)+'', onSelectChange: preview });
});
</script>
顯示效果:
您可能感興趣的文章:
- JQuery Jcrop 實(shí)現(xiàn)圖片裁剪的插件
- jQuery插件jcrop+Fileapi完美實(shí)現(xiàn)圖片上傳+裁剪+預(yù)覽的代碼分享
- js+jquery實(shí)現(xiàn)圖片裁剪功能
- 基于JQuery實(shí)現(xiàn)的圖片自動(dòng)進(jìn)行縮放和裁剪處理
- 基于jQuery+HttpHandler實(shí)現(xiàn)圖片裁剪效果代碼(適用于論壇, SNS)
- jQuery實(shí)現(xiàn)圖片上傳和裁剪插件Croppie
- jquery.Jcrop結(jié)合JAVA后臺(tái)實(shí)現(xiàn)圖片裁剪上傳實(shí)例
- 利用jQuery插件imgAreaSelect實(shí)現(xiàn)圖片上傳裁剪(放大縮?。?/a>
- JQuery PHP圖片在線裁剪實(shí)例
- jquery實(shí)現(xiàn)自定義圖片裁剪功能【推薦】
相關(guān)文章
jQuery實(shí)現(xiàn)的鼠標(biāo)拖動(dòng)浮層功能示例【拖動(dòng)div等任何標(biāo)簽】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的鼠標(biāo)拖動(dòng)浮層功能,可實(shí)現(xiàn)拖動(dòng)div等任何標(biāo)簽的效果,涉及jQuery事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-12-12
JQuery模擬實(shí)現(xiàn)網(wǎng)頁中自定義鼠標(biāo)右鍵菜單功能
這篇文章主要給大家介紹了關(guān)于利用JQuery模擬實(shí)現(xiàn)網(wǎng)頁中自定義鼠標(biāo)右鍵菜單功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
jQuery實(shí)現(xiàn)切換頁面過渡動(dòng)畫效果
這是一款效果非??岬膉Query和CSS3通過AJAX調(diào)用切換頁面過渡動(dòng)畫特效插件。該頁面切換特效使用AJAX來動(dòng)態(tài)加載鏈接內(nèi)容,在頁面加載的時(shí)候,使用CSS3來制作非??岬捻撁孢^渡動(dòng)畫效果。插件中使用pushState方法來管理瀏覽器的瀏覽歷史,需要的朋友可以參考下2015-10-10
基于jQuery的360圖片展示實(shí)現(xiàn)代碼
基于jQuery的360圖片展示實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-06-06
JQueryEasyUI框架下的combobox的取值和綁定的方法
這篇文章主要介紹了JQueryEasyUI框架下的combobox的取值和綁定的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
jQuery表單獲取和失去焦點(diǎn)輸入框提示效果的實(shí)例代碼
這篇文章介紹了jQuery表單獲取和失去焦點(diǎn)輸入框提示效果的實(shí)例代碼,有需要的朋友可以參考一下2013-08-08

