摘要:在裁剪框外拖動(dòng)鼠標(biāo)會生成一個(gè)新的裁剪框。這個(gè)是裁剪框的縱橫比,默認(rèn)是不限制的。初始化完成后是否自動(dòng)顯示裁剪框自動(dòng)顯示的裁剪框的大小。方法的使用格式為手動(dòng)顯示裁剪框。
插件介紹
這是一個(gè)我在寫以前的項(xiàng)目的途中發(fā)現(xiàn)的一個(gè)國人寫的jQuery圖像裁剪插件,當(dāng)時(shí)想實(shí)現(xiàn)用戶資料的頭像上傳功能,并且能夠預(yù)覽圖片,和對圖片進(jìn)行簡單的裁剪、旋轉(zhuǎn),花了不少時(shí)間才看到了這個(gè)插件,感覺功能挺全面,代碼實(shí)現(xiàn)起來也挺簡單,再加上用的是Bootstrap,對移動(dòng)端操作也有適配,于是就用了。現(xiàn)在稍微有點(diǎn)時(shí)間就記錄一下,方便以后再用的時(shí)候查閱。另外也有對應(yīng)的js版本。
官方文檔(英文)
jQuery
GitHub項(xiàng)目地址
官方示例
js
GitHub項(xiàng)目地址
兼容性兼容所有支持了Canvas的瀏覽器(IE9+),一小部分功能例外,具體請查看官方文檔。
參數(shù) viewModeType: Number
Default: 0
Options: 0,1,2,3
這個(gè)具體每個(gè)值對應(yīng)的效果我也不是很清楚,推薦在上面的官方示例里都試一試,我都是比較喜歡2。
dragModeType: String
Default: "crop"
Options:
"crop": 在裁剪框外拖動(dòng)鼠標(biāo)會生成一個(gè)新的裁剪框。
"move": 在裁剪框外拖動(dòng)鼠標(biāo)會移動(dòng)原圖。
"none": 在裁剪框外拖動(dòng)鼠標(biāo)則什么也不做。
aspectRatioType: Number
Default: NaN
這個(gè)是裁剪框的縱橫比,默認(rèn)是不限制的。例如1:1的頭像就寫1,16:9可寫成16 / 9。
dataType: Object
Default: null
The previous cropped data if you had stored, will be passed to setData method automatically.
(沒怎么用過,都是直接用setData方法)
previewType: String (jQuery selector)
Default: ""
預(yù)覽圖的位置,用jQuery選擇器表示。
responsiveType: Boolean
Default: true
在更改窗口大小后是否重新渲染cropper。
restoreType: Boolean
Default: true
在更改窗口大小后是否恢復(fù)裁剪區(qū)域。
checkCrossOriginType: Boolean
Default: true
檢查圖像是否是跨域圖像。(具體查看官方文檔)
checkOrientationType: Boolean
Default: true
(具體查看官方文檔)
modalType: Boolean
Default: true
非裁剪區(qū)域是否用黑罩遮蓋。
guidesType: Boolean
Default: true
裁剪區(qū)域是否顯示虛線。
centerType: Boolean
Default: true
裁剪區(qū)域正中央是否顯示+號。
highlightType: Boolean
Default: true
裁剪區(qū)域是否高亮顯示。
backgroundType: Boolean
Default: true
是否顯示背景的黑白方格(類似PS里透明圖層的顯示方式)。
autoCropType: Boolean
Default: true
cropper初始化完成后是否自動(dòng)顯示裁剪框
autoCropAreaType: Number
Default: 0.8 (80% of the image)
自動(dòng)顯示的裁剪框的大小。因此,數(shù)字應(yīng)當(dāng)在0~1之間。
movableType: Boolean
Default: true
是否允許移動(dòng)原圖。(如果這里填false那么盡管dragMode的值是move,在裁剪框外拖動(dòng)也不會移動(dòng)原圖)
rotatableType: Boolean
Default: true
是否可以旋轉(zhuǎn)原圖。
scalableType: Boolean
Default: true
是否可以對原圖進(jìn)行縱橫拉伸。
例如把原圖寬度拉長為原來的2倍或者拉長為原來的-1倍(即水平翻轉(zhuǎn))。
zoomableType: Boolean
Default: true
是否可以對原圖進(jìn)行縮小放大。
zoomOnTouchType: Boolean
Default: true
是否允許在移動(dòng)端上使用雙指觸摸縮放原圖。
zoomOnWheelType: Boolean
Default: true
是否允許使用鼠標(biāo)滾輪縮放原圖。
wheelZoomRatioType: Number
Default: 0.1
當(dāng)使用鼠標(biāo)滾輪縮放時(shí)的比例。
cropBoxMovableType: Boolean
Default: true
是否允許移動(dòng)裁剪框。
cropBoxResizableType: Boolean
Default: true
是否允許通過拖動(dòng)裁剪框的邊框來調(diào)整裁剪框的大小。
toggleDragModeOnDblclickType: Boolean
Default: true
是否允許通過雙擊來在crop和move之間切換dragMode。
minContainerWidthType: Number
Default: 200
容器寬度最小值。
minContainerHeightType: Number
Default: 100
容器高度最小值。
minCanvasWidthType: Number
Default: 0
canvas(原圖)寬度最小值。
minCanvasHeightType: Number
Default: 0
canvas(原圖)高度最小值。
minCropBoxWidthType: Number
Default: 0
剪切框?qū)挾茸钚≈怠?/p>
Note: This size is relative to the page, not the image.
minCropBoxHeightType: Number
Default: 0
剪切框高度最小值。
Note: This size is relative to the page, not the image.
readyType: Function
Default: null
A shortcut of the "ready" event.
cropstartType: Function
Default: null
A shortcut of the "cropstart" event.
cropmoveType: Function
Default: null
A shortcut of the "cropmove" event.
cropendType: Function
Default: null
A shortcut of the "cropend" event.
cropType: Function
Default: null
A shortcut of the "crop" event.
zoomType: Function
Default: null
A shortcut of the "zoom" event.
常用方法除了"setAspectRatio","replace"和"destroy"以外,所有的方法都要在ready后才能使用。這里只介紹幾個(gè)常用的方法,全部的方法請到官方文檔查閱。
方法的使用格式為
$().cropper("method",arg0,arg1,arg2,...);crop()
手動(dòng)顯示裁剪框。
$().cropper({ autoCrop: false, ready: function () { // Do something here // ... // And then $(this).cropper("crop"); } });reset()
恢復(fù)全部到初始狀態(tài)。
replace(url[, onlyColorChanged])
url:
Type: String
A new image url.
onlyColorChanged (optional):
Type: Boolean
If only change the color, not the size, then the cropper only need to change the srcs of all related images, not need to rebuild the cropper. This can be used for applying filters.
If not present, its default value is false.
替換cropper中的圖像文件,通常第二個(gè)參數(shù)不管。
destroy()銷毀cropper,并且會移除img標(biāo)簽的src屬性的值。
getCroppedCanvas([options])
options (optional):
Type: Object
Properties:
width: the destination width of the output canvas.
height: the destination height of the output canvas.
minWidth: the minimum destination width of the output canvas, the default value is 0.
minHeight: the minimum destination height of the output canvas, the default value is 0.
maxWidth: the maximum destination width of the output canvas, the default value is Infinity.
maxHeight: the maximum destination height of the output canvas, the default value is Infinity.
fillColor: a color to fill any alpha values in the output canvas, the default value is transparent.
imageSmoothingEnabled: set to change if images are smoothed (true, default) or not (false).
imageSmoothingQuality: set the quality of image smoothing, one of "low" (default), "medium", or "high".
(return value):
Type: HTMLCanvasElement
A canvas drawn the cropped image.
Notes:
輸出的canvas的縱橫比會自動(dòng)適應(yīng)于裁剪框的縱橫比.
如果打算得到JPEG圖像,那么應(yīng)該先設(shè)置fillColor參數(shù),否則裁剪后的透明部分默認(rèn)會由黑色填充。
Browser support:
Basic image: requires Canvas support (IE 9+).
Rotated image: requires CSS3 2D Transforms support (IE 9+).
Cross-origin image: requires HTML5 CORS settings attributes support (IE 11+).
得到裁剪到的圖像的canvas,如果沒有裁剪,那么就返回的是整個(gè)原圖圖像的canvas。
這是最重要的一個(gè)方法,通過這個(gè)方法就可以得到裁剪后的圖像,再使用toDataURL()得到base64 dataURL(不指定格式的話會是png格式)或者toBlob()得到Blob,然后就可以很輕松地將圖片上傳至服務(wù)器上或者顯示在某個(gè)img標(biāo)簽中了。例如:
// 轉(zhuǎn)換為png格式的dataURL var dataURL = $().cropper("getCroppedCanvas", { width:100, height:100 }).toDataURL("image/png"); // 轉(zhuǎn)換為Blob后顯示在img標(biāo)簽中 var URL = window.URL || window.webkitURL; $().cropper("getCroppedCanvas", { width:100, height:100 }).toBlob(function (blob) { $().attr("src",URL.createObjectURL(blob)); });簡單實(shí)例 在頁面直接使用cropper
接下來只是實(shí)現(xiàn)一個(gè)簡單的功能:網(wǎng)頁中可以上傳圖片,然后對圖片進(jìn)行裁剪,點(diǎn)擊確定后會顯示出裁剪后的圖片。
代碼如下:
裁剪圖片 使用cropper來上傳圖片到服務(wù)器結(jié)果:
![]()
由于cropper可以得到兩種裁剪后圖片的數(shù)據(jù)(即blob和dataURL),所以對應(yīng)的上傳到后臺也會有兩種方法,在這里我只寫一種使用ajax上傳base64 dataURL的,另一種方法如果有興趣,可以自己嘗試。
頁面中,將上面的sendPhoto方法改為:
var sendPhoto = function () { // 得到PNG格式的dataURL var photo = $("#photo").cropper("getCroppedCanvas", { width: 300, height: 300 }).toDataURL("image/png"); $.ajax({ url: "上傳地址", // 要上傳的地址 type: "post", data: { "imgData": photo }, dataType: "json", success: function (data) { if (data.status == 0) { // 將上傳的頭像的地址填入,為保證不載入緩存加個(gè)隨機(jī)數(shù) $(".user-photo").attr("src", "頭像地址?t=" + Math.random()); $("#changeModal").modal("hide"); } else { alert(data.info); } } }); }后臺中,Java的主要代碼如下:(使用了jdk8的Base64,,如果是低版本請自行替換)
/** * 將Base64位編碼的圖片進(jìn)行解碼,并保存到指定目錄 */ public static void decodeBase64DataURLToImage(String dataURL, String path, String imgName) throws IOException { // 將dataURL開頭的非base64字符刪除 String base64 = dataURL.substring(dataURL.indexOf(",") + 1); FileOutputStream write = new FileOutputStream(new File(path + imgName)); byte[] decoderBytes = Base64.getDecoder().decode(base64); write.write(decoderBytes); write.close(); }小結(jié)cropper能做到的事情還很多,這里只是簡單使用了一下,更多功能可以在有想法的再研究下。
這是針對以前項(xiàng)目用的cropper的一個(gè)整理,結(jié)果因?yàn)楫?dāng)初沒有看官方例子,途中發(fā)現(xiàn)了在模態(tài)框中使用的一個(gè)bug,以后會注意這方面。另外,整理這部分資料時(shí)也參考了不少的網(wǎng)絡(luò)資料,在這里就不一一記錄了。
最后,由于本人能力有限,若發(fā)現(xiàn)錯(cuò)誤希望能指出,本人會及時(shí)改正,非常感謝。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/90285.html
相關(guān)文章
JQuery 插件圖片裁剪插件cropper.js使用,上傳
摘要:圖片裁剪,壓縮是上傳圖片一定會遇到的問題。如何獲得裁剪的圖片呢獲取裁剪后的圖片信息首先我們可以獲得裁剪框的節(jié)點(diǎn)然后調(diào)用圖片質(zhì)量圖片質(zhì)量越好圖片大小越大這樣就得到了你裁剪的圖片了可以通過,放到你想要的節(jié)點(diǎn)里展示。 圖片裁剪,壓縮是上傳圖片一定會遇到的問題。這里把我測試cropper.js這款jquery插件的心得分享一下,可以給新手做參考。 引入插件相關(guān)文件,你們down到本地也可以。這...
基于 Vue的圖片裁剪插件
摘要:預(yù)覽項(xiàng)目地址一些想法網(wǎng)上看了看基于的圖片裁剪的插件很少,剛剛好項(xiàng)目需要使用到圖片裁剪,于是便有了這個(gè)插件的誕生。 vue-crpopper 預(yù)覽項(xiàng)目地址 一些想法 網(wǎng)上看了看基于vue的圖片裁剪的插件很少,剛剛好項(xiàng)目需要使用到圖片裁剪,于是便有了這個(gè)插件的誕生。 實(shí)現(xiàn)的原理 通過canvas生成新的圖片, 可以上傳到后臺保存。 效果圖 showImg(https://segmentfa...
立志做一個(gè)最好用的移動(dòng)端圖片裁剪插件
摘要:基于的圖片壓縮裁剪插件功能說明圖片壓縮,圖片裁剪,圖片壓縮,圖片裁剪,,解決了部分機(jī)型方向不對的問題,同時(shí)可以對圖片進(jìn)行尺寸和質(zhì)量的壓縮效果演示調(diào)試模式下或者手機(jī)打開本地運(yùn)行使用本項(xiàng)目的文件需要引入在需要使用的頁面直接引入 vue-image-cropper 基于vue的圖片壓縮裁剪插件 1.功能說明 圖片壓縮,圖片裁剪,vue圖片壓縮,vue圖片裁剪,vue-image-croppe...
發(fā)表評論
0條評論
閱讀 3224·2021-11-22 15:25
閱讀 3946·2021-11-17 09:33
閱讀 3429·2021-11-08 13:15
閱讀 3120·2021-09-22 10:56
閱讀 627·2021-08-31 09:45
閱讀 2820·2019-08-30 13:49
閱讀 3134·2019-08-30 12:52
閱讀 1210·2019-08-29 17:05