• Stars
    star
    1,118
  • Rank 41,559 (Top 0.9 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Image Crop directive for AngularJS (THIS PROJECT IS NOT MAINTAINED ANYMORE)

ngImgCrop

WARNING

THIS PROJECT IS NOT MAINTAINED ANYMORE. You are free to fork it and start a new project.

Overview

Simple Image Crop directive for AngularJS. Enables to crop a circle or a square out of an image.

Screenshots

Circle Crop

Square Crop

Live demo

Live demo on JSFiddle

Requirements

  • AngularJS
  • Modern Browser supporting

Installing

Download

You have two options to get the files:

  • Download ngImgCrop files from GitHub.
  • Use Bower to download the files. Just run bower install ngImgCrop.

Add files

Add the scripts to your application. Make sure the ng-img-crop.js file is inserted after the angular.js library:

<script src="angular.js"></script>
<script src="ng-img-crop.js"></script>
<link rel="stylesheet" type="text/css" href="ng-img-crop.css">

Add a dependancy

Add the image crop module as a dependancy to your application module:

var myAppModule = angular.module('MyApp', ['ngImgCrop']);

Usage

  1. Add the image crop directive <img-crop> 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 code

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>
<head>
  <script src="angular.js"></script>
  <script src="ng-img-crop.js"></script>
  <link rel="stylesheet" type="text/css" href="ng-img-crop.css">
  <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>

Options

<img-crop
    image="{string}"
    result-image="{string}"
   [change-on-fly="{boolean}"]
   [area-type="{circle|square}"]
   [area-min-size="{number}"]
   [result-image-size="{number}"]
   [result-image-format="{string}"]
   [result-image-quality="{number}"]
   [on-change="{expression}"]
   [on-load-begin="{expression"]
   [on-load-done="{expression"]
   [on-load-error="{expression"]
></img-crop>

image

Assignable angular expression to data-bind to. NgImgCrop gets an image for cropping from it.

result-image

Assignable angular expression to data-bind to. NgImgCrop puts a data uri of a cropped image into it.

change-on-fly

Optional. By default, to reduce CPU usage, when a user drags/resizes the crop area, the result image is only updated after the user stops dragging/resizing. Set true to always update the result image as the user drags/resizes the crop area.

area-type

Optional. Type of the crop area. Possible values: circle|square. Default: circle.

area-min-size

Optional. Min. width/height of the crop area (in pixels). Default: 80.

result-image-size

Optional. Width/height of the result image (in pixels). Default: 200.

result-image-format

Optional. Format of result image. Possible values include image/jpeg, image/png, and image/webp. Browser support varies. Default: image/png.

result-image-quality

Optional. Quality of result image. Possible values between 0.0 and 1.0 inclusive. Default: browser default.

on-change

Optional. Expression to evaluate upon changing the cropped part of the image. The cropped image data is available as $dataURI.

on-load-begin

Optional. Expression to evaluate when the source image starts loading.

on-load-done

Optional. Expression to evaluate when the source image successfully loaded.

on-load-error

Optional. Expression to evaluate when the source image didn't load.

License

See the LICENSE file.

More Repositories

1

SVG-Morpheus

JavaScript library enabling SVG icons to morph from one to the other. It implements Material Design's Delightful Details transitions. (THIS PROJECT IS NOT MAINTAINED ANYMORE)
JavaScript
2,699
star
2

awesome-bitcoin-payment-processors

๐ŸŒŸ A curated list of Bitcoin payment processors enabling merchants, businesses and nonprofits to accept Bitcoin payments.
429
star
3

SVG-Circus

Create cool animated SVG spinners, loaders and other looped animations in seconds
JavaScript
316
star
4

DeBitpay

โ‡๏ธ DeBitpay is a simple app which decodes Bitpay payment links into Bitcoin transaction requirements, so that you could make the payments with Bitcoin wallets not supporting Bitpay.
JavaScript
125
star
5

node-red-node-typescript-starter

๐Ÿ Quick-start template repository for creating new Node-RED node sets in TypeScript.
JavaScript
85
star
6

One-Time-Address

๐Ÿท A better way to share your Bitcoin address.
Handlebars
71
star
7

HD-Wallet-Scanner

โ„๏ธ Find all used addresses in your Bitcoin HD wallets bypassing gap limits.
JavaScript
26
star
8

StoreOfTime.com

โŒš Comparison of Bitcoin, Local Currency, USD, Gold and Silver as a Store of Time.
JavaScript
19
star
9

store-of-something

๐Ÿ“ˆ Template repository for making "Store of {Something}" websites.
EJS
10
star
10

DeBitpay-Directory

๐Ÿช Alternative places to spend Bitcoin without BitPay.
Nunjucks
8
star
11

new-project

7
star
12

DeBitpay-nodejs

DeBitpay is a simple app which decodes Bitpay payment links into Bitcoin transaction requirements, so that you could make the payments with Bitcoin wallets not supporting Bitpay. **NodeJS edition**
HTML
4
star
13

StoreOfBigMacs.com

๐Ÿ”Find out how many Big Macs could a monetary value of a single Big Mac get over time when stored in Bitcoin, USD, Gold or Silver.
EJS
2
star
14

svgcircus.com

HTML
1
star