jQuery and Canvas Based Image Cropping Plugin - Simple Cropper

File Size: 75.2 KB
Views Total: 5695
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
jQuery and Canvas Based Image Cropping Plugin - Simple Cropper

Simple Cropper is an easy-to-use jQuery image cropper which allows you to crop a local image while keeping the aspect ratio of it's container. When you click on the confirm button, the plugin will generate a new cropped (Base64 encoded) image using Html5 canvas and insert it into your web page/application. Supports both portrait and landscape views. Jcrop image cropping plugin is required for the basic image cropping functionality.

How to use it:

1. Include jQuery library and other required resources in your web page.

<link rel="stylesheet" href="css/jquery.Jcrop.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery.Jcrop.js"></script>
<script type="text/javascript" src="scripts/jquery.SimpleCropper.js"></script>

2. Create a container element for the image cropper.

<div class="cropme" id="landscape" style="width: 1136px; height: 720px;"></div>

3. The core CSS styles for the image cropper.

#fileInput {
  width: 0;
  height: 0;
  overflow: hidden;
}

#modal {
  z-index: 10;
  position: fixed;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  background-color: #5F5F5F;
  opacity: 0.95;
  display: none;
}

#preview {
  z-index: 11;
  position: fixed;
  top: 0px;
  left: 0px;
  display: none;
  border: 4px solid #A5A2A2;
  border-radius: 4px;
  float: left;
  font-size: 0px;
  line-height: 0px;
}

#preview .buttons {
  width: 36px;
  position: absolute;
  bottom: 0px;
  right: -44px;
}

#preview .buttons .ok {
  border: 4px solid #F5F5F5;
  border-radius: 4px;
  width: 36px;
  height: 36px;
  line-height: 0px;
  font-size: 0px;
  background-image: url('../images/Ok.png');
  background-repeat: no-repeat;
}

#preview .buttons .ok:hover { background-image: url('../images/OkGreen.png'); }

#preview .buttons .cancel {
  margin-bottom: 4px;
  border: 4px solid #F5F5F5;
  border-radius: 4px;
  width: 36px;
  height: 36px;
  line-height: 0px;
  font-size: 0px;
  background-image: url('../images/Cancel.png');
  background-repeat: no-repeat;
}

#preview .buttons .cancel:hover { background-image: url('../images/CancelRed.png'); }

4. Add custom CSS styles to the image cropper.

.cropme{
  float: left;
  background-color: #f1f1f1;
  margin-bottom: 5px;
  margin-right: 5px;
  background-image: url('../images/UploadLight.png');
  background-position: center center;
  background-repeat: no-repeat;
  cursor: pointer;
}

.cropme:hover{
  background-image: url('../images/UploadDark.png');
}

5. Initialize the plugin on the container element you just created and done.

$('.cropme').simpleCropper();

This awesome jQuery plugin is developed by matt-alter. For more Advanced Usages, please check the demo page or visit the official website.