Angular實現(xiàn)圖片裁剪工具ngImgCrop實踐
ngImgCrop是AngularJS的一個圖片裁剪插件,它實際上是一個封裝好的AngularJs指令,可以讓用戶以圓框或者方框來裁剪圖片
1、使用效果截圖

2、demo演示
demo演示地址 http://jsfiddle.net/alexk111/rw6q9/
3、下載安裝
可以使用兩種方式來下載ngImgCrop插件
a、GitHub下載:git clone https://github.com/alexk111/ngImgCrop.git
b、bower安裝,如果項目中使用了bower,使用命令bower install ngImgCrop即可
4、添加js和css依賴到項目中
<script src="angular.js"></script> <script src="ng-img-crop.js"></script> <link rel="stylesheet" type="text/css" href="ng-img-crop.css" rel="external nofollow" rel="external nofollow" >
5、添加AngularJs依賴
var myAppModule = angular.module('MyApp', ['ngImgCrop']);
6、使用樣例
<html>
<head>
<script src="angular.js"></script>
<script src="ng-img-crop.js"></script>
<link rel="stylesheet" type="text/css" href="ng-img-crop.css" rel="external nofollow" rel="external nofollow" >
<style>
.cropArea {
background: #E4E4E4;
overflow: hidden;
width:500px;
height:350px;
}
</style>
<script>
angular.module('app', ['ngImgCrop'])
.controller('Ctrl', function($scope) {
$scope.myImage='';
$scope.myCroppedImage='';
var handleFileSelect=function(evt) {
var file=evt.currentTarget.files[0];
var reader = new FileReader();
reader.onload = function (evt) {
$scope.$apply(function($scope){
$scope.myImage=evt.target.result;
});
};
reader.readAsDataURL(file);
};
angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);
});
</script>
</head>
<body ng-app="app" ng-controller="Ctrl">
<div>Select an image file: <input type="file" id="fileInput" /></div>
<div class="cropArea">
<img-crop image="myImage" result-image="myCroppedImage"></img-crop>
</div>
<div>Cropped Image:</div>
<div><img ng-src="{{myCroppedImage}}" /></div>
</body>
</html>
7、屬性介紹
<img-crop
image="{string}" 需要進行裁剪的圖片文件 如$scope.myImage
result-image="{string}" 保存裁剪結(jié)果的圖片文件 如$scope.myCroppedImage
[change-on-fly="{boolean}"] 可選項:表示是否在拖拽裁剪區(qū)域時實時更新結(jié)果文件
[area-type="{circle|square}"] 可選項:表示裁剪窗口是方的還是圓的,默認(rèn)是圓的
[area-min-size="{number}"] 可選項,表示裁剪結(jié)果的最小大小,默認(rèn)是80,即結(jié)果最小是高80像素、寬80像素
[result-image-size="{number}"] 可選項,表示裁剪結(jié)果大小,默認(rèn)是200,即高200像素、寬200像素
[result-image-format="{string}"] 可選項,表示裁剪結(jié)果保存的文件類型,可以選擇image/jpeg、image/png、image/webp,默認(rèn)是image/png
[result-image-quality="{number}"] 可選項,表示裁剪結(jié)果的質(zhì)量,取值在0.0到1.0之間
[on-change="{expression}"] 可選項,檢測到圖片修改后執(zhí)行的表達(dá)式
[on-load-begin="{expression"] 可選項,圖片開始加載執(zhí)行的表達(dá)式
[on-load-done="{expression"] 可選項,圖片加載完成執(zhí)行的表達(dá)式
[on-load-error="{expression"] 可選項,圖片加載失敗執(zhí)行的表達(dá)式
></img-crop>
8、注意點
結(jié)果文件是base64的格式,如果是直接展示的話沒有問題,如果是以文件格式要將圖片上傳給后臺服務(wù)器,那么還需要將base64轉(zhuǎn)換成圖片文件格式,附上我自己的轉(zhuǎn)換代碼
$scope.file可直接作為File文件格式上傳至后臺服務(wù)器
function getBlobBydataURL(dataURI,type){
var binary = atob(dataURI.split(',')[1]);
var array = [];
for(var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], {type:type });
}
var $Blob = getBlobBydataURL($scope.myCroppedImage,"image/png");
$scope.file = $Blob;
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angular2學(xué)習(xí)教程之ng中變更檢測問題詳解
這篇文章主要給大家介紹了Angular2學(xué)習(xí)教程之ng中變更檢測問題的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05
Angular8引入百度Echarts進行圖表分析的實現(xiàn)代碼
這篇文章主要介紹了Angular8引入百度Echarts進行圖表分析的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
AngularJS Bootstrap詳細(xì)介紹及實例代碼
本文主要介紹AngularJS Bootstrap,這兩對AngularJS Bootstrap的基礎(chǔ)知識做了詳細(xì)講解,并提供簡單示例,有需要的小伙伴可以參考下2016-07-07
Angular 4依賴注入學(xué)習(xí)教程之FactoryProvider配置依賴對象(五)
這篇文章主要給大家介紹了關(guān)于Angular 4依賴注入之FactoryProvider配置依賴對象的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-06-06
AngularJS獲取json數(shù)據(jù)的方法詳解
這篇文章主要介紹了AngularJS獲取json數(shù)據(jù)的方法,結(jié)合實例形式詳細(xì)分析了AngularJS獲取json數(shù)據(jù)的詳細(xì)步驟、操作技巧與相關(guān)注意事項,需要的朋友可以參考下2017-05-05
AngularJS控制器controller給模型數(shù)據(jù)賦初始值的方法
這篇文章主要介紹了AngularJS控制器controller給模型數(shù)據(jù)賦初始值的方法,涉及AngularJS控制器controller簡單賦值操作實現(xiàn)技巧,需要的朋友可以參考下2017-01-01

