Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Select Image, Crop Then Upload Or Save Using Pure JavaScript

Select Image, Crop Then Upload Or Save Using Pure JavaScript


This JavaScript code snippet is based on a simple idea to crop an image and save/download them instantly. It uses the JavaScript image Cropper library (cropper.js) to perform image cropping.

Basically, this code snippet just focuses on just image cropping and lets users download it. But you can modify the Cropper JS configurations to upload the image to the server. Similarly, you can crop images programmatically or create a functionality to allows users to crop images before upload. To do so, read the Cropper JS docs.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.


  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Let User To Select Image, Crop Then Upload Or Save Using JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

Recommended For You:
How To Reload/Refresh The Web-Page With Pure JavaScript?

External Files:

<!-- Normalize CSS -->
<link rel="stylesheet" href="">
<!-- Cropper CSS -->
<link rel='stylesheet' href=''>
<!-- Cropper JS -->
<script src=''></script>


<style type="text/css">
.page {
margin: 1em auto;
max-width: 768px;
display: flex;
align-items: flex-start;
flex-wrap: wrap;
height: 100%;

.box {
padding: 0.5em;
width: 100%;
margin: 0.5em;

.box-2 {
padding: 0.5em;
width: calc(100%/2 - 1em);

.options label,
.options input {
width: 4em;
padding: 0.5em 1em;

.btn {
background: white;
color: black;
border: 1px solid black;
padding: 0.5em 1em;
text-decoration: none;
margin: 0.8em 0.3em;
display: inline-block;
cursor: pointer;

.hide {
display: none;

img {
max-width: 100%;


<main class="page">
<h2>Upload ,Crop and save.</h2>
<!-- input file -->
<div class="box">
<input type="file" id="file-input">
<!-- leftbox -->
<div class="box-2">
<div class="result"></div>
<div class="box-2 img-result hide">
<!-- result of crop -->
<img class="cropped" src="" alt="">
<!-- input file -->
<div class="box">
<div class="options hide">
<label> Width</label>
<input type="number" class="img-w" value="300" min="100" max="1200" />
<!-- save btn -->
<button class="btn save hide">Save</button>
<!-- download btn -->
<a href="" class="btn download hide">Download</a>


<script type="text/javascript">
// vars
let result = document.querySelector('.result'),
img_result = document.querySelector('.img-result'),
img_w = document.querySelector('.img-w'),
img_h = document.querySelector('.img-h'),
options = document.querySelector('.options'),
save = document.querySelector('.save'),
cropped = document.querySelector('.cropped'),
dwn = document.querySelector('.download'),
upload = document.querySelector('#file-input'),
cropper = '';

// on change show image with crop options
upload.addEventListener('change', e => {
if ( {
// start file reader
const reader = new FileReader();
reader.onload = e => {
if ( {
// create new image
let img = document.createElement('img'); = 'image';
img.src =;
// clean result before
result.innerHTML = '';
// append new image
// show save btn and options
// init cropper
cropper = new Cropper(img);

// save on click
save.addEventListener('click', e => {
// get result to data uri
let imgSrc = cropper.getCroppedCanvas({
width: img_w.value // input value
// remove hide class of img
// show image cropped
cropped.src = imgSrc;
dwn.classList.remove('hide'); = 'imagename.png';
dwn.setAttribute('href', imgSrc);


No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
Modify The Browser Address Bar URL Without Reloading Using JavaScript

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *