Image crop directive for AngularJS
npm install ng-img-crop
bower install ngImgCrop
ng-img-crop.js
angular.js
html
$3 Add the image crop module as a dependancy to your application module:
js var myAppModule = angular.module('MyApp', ['ngImgCrop']);
Usage 1. Add the image crop directive
to the HTML file where you want to use an image crop control. Note: a container, you place the directive to, should have some pre-defined size (absolute or relative to its parent). That's required, because the image crop control fits the size of its container. 2. Bind the directive to a source image property (using image="" option). The directive will read the image data from that property and watch for updates. The property can be a url to an image, or a data uri. 3. Bind the directive to a result image property (using result-image="" option). On each update, the directive will put the content of the crop area to that property in the data uri format. 4. Set up the options that make sense to your application. 5. Done! Result image The result image will always be a square for the both circle and square area types. It's highly recommended to store the image as a square on your back-end, because this will enable you to easily update your pics later, if you decide to implement some design changes. Showing a square image as a circle on the front-end is not a problem - it is as easy as adding a border-radius style for that image in a css. Example The following code enables to select an image using a file input and crop it. The cropped image data is inserted into img each time the crop area updates.
html Select an image file: Cropped Image:
Options
html image="{string}" result-image="{string}" [change-on-fly="{boolean}"] [area-type="{circle|square}"] [area-min-size="{number}"] [result-image-size="{number}"] [on-change="{expression}"] [on-load-begin="{expression"] [on-load-done="{expression"] [on-load-error="{expression"] >