├── .gitignore ├── README.md ├── github ├── github.pub ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── images │ ├── car1.png │ ├── car2.png │ ├── car3.png │ └── car4.png ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.css ├── App.js ├── App.test.js ├── ImageCropDialog.js ├── cropImage.js ├── index.css ├── index.js ├── logo.svg ├── reportWebVitals.js └── setupTests.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learn How to Crop Images With React Easy Crop 2 | 3 | YouTube Link: https://youtu.be/E_AHkWHhUz4 4 | -------------------------------------------------------------------------------- /github: -------------------------------------------------------------------------------- 1 | -----BEGIN OPENSSH PRIVATE KEY----- 2 | b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW 3 | QyNTUxOQAAACB6Zxc4SaVqRCi7If4q0o/ZwkNkuwS2YcJ2SdEtwTTkZQAAAKByDGuvcgxr 4 | rwAAAAtzc2gtZWQyNTUxOQAAACB6Zxc4SaVqRCi7If4q0o/ZwkNkuwS2YcJ2SdEtwTTkZQ 5 | AAAEBPbZqv+e7ijejLEhEV4bLXMBc5zy2L8cX/j4+7wdlkeHpnFzhJpWpEKLsh/irSj9nC 6 | Q2S7BLZhwnZJ0S3BNORlAAAAGWFkYW1qYWxpbmF1c2thc0BnbWFpbC5jb20BAgME 7 | -----END OPENSSH PRIVATE KEY----- 8 | -------------------------------------------------------------------------------- /github.pub: -------------------------------------------------------------------------------- 1 | ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHpnFzhJpWpEKLsh/irSj9nCQ2S7BLZhwnZJ0S3BNORl adamjalinauskas@gmail.com 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "image-cropping-with-react-easy-crop", 3 | "homepage": "", 4 | "version": "0.1.0", 5 | "private": true, 6 | "dependencies": { 7 | "@testing-library/jest-dom": "^5.11.4", 8 | "@testing-library/react": "^11.1.0", 9 | "@testing-library/user-event": "^12.1.10", 10 | "gh-pages": "^3.2.3", 11 | "react": "^17.0.2", 12 | "react-dom": "^17.0.2", 13 | "react-easy-crop": "^3.5.2", 14 | "react-scripts": "4.0.3", 15 | "web-vitals": "^1.0.1" 16 | }, 17 | "scripts": { 18 | "predeploy": "npm run build", 19 | "deploy": "gh-pages -d build", 20 | "start": "react-scripts start", 21 | "build": "react-scripts build", 22 | "test": "react-scripts test", 23 | "eject": "react-scripts eject" 24 | }, 25 | "eslintConfig": { 26 | "extends": [ 27 | "react-app", 28 | "react-app/jest" 29 | ] 30 | }, 31 | "browserslist": { 32 | "production": [ 33 | ">0.2%", 34 | "not dead", 35 | "not op_mini all" 36 | ], 37 | "development": [ 38 | "last 1 chrome version", 39 | "last 1 firefox version", 40 | "last 1 safari version" 41 | ] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingWith-Adam/react-easy-crop-tutorial/26166d9bdce1ffdfa86b6deecc453492916f5ef8/public/favicon.ico -------------------------------------------------------------------------------- /public/images/car1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingWith-Adam/react-easy-crop-tutorial/26166d9bdce1ffdfa86b6deecc453492916f5ef8/public/images/car1.png -------------------------------------------------------------------------------- /public/images/car2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingWith-Adam/react-easy-crop-tutorial/26166d9bdce1ffdfa86b6deecc453492916f5ef8/public/images/car2.png -------------------------------------------------------------------------------- /public/images/car3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingWith-Adam/react-easy-crop-tutorial/26166d9bdce1ffdfa86b6deecc453492916f5ef8/public/images/car3.png -------------------------------------------------------------------------------- /public/images/car4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingWith-Adam/react-easy-crop-tutorial/26166d9bdce1ffdfa86b6deecc453492916f5ef8/public/images/car4.png -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingWith-Adam/react-easy-crop-tutorial/26166d9bdce1ffdfa86b6deecc453492916f5ef8/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingWith-Adam/react-easy-crop-tutorial/26166d9bdce1ffdfa86b6deecc453492916f5ef8/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .imageCard { 2 | text-align: center; 3 | } 4 | 5 | .imageCard img { 6 | width: 600px; 7 | } 8 | 9 | .backdrop { 10 | position: fixed; 11 | background-color: black; 12 | top: 0; 13 | left: 0; 14 | right: 0; 15 | bottom: 0; 16 | } 17 | 18 | .crop-container { 19 | position: fixed; 20 | top: 0; 21 | left: 0; 22 | right: 0; 23 | bottom: 80px; 24 | } 25 | 26 | .controls { 27 | position: fixed; 28 | bottom: 0px; 29 | width: 100%; 30 | height: 80px; 31 | } 32 | 33 | .controls-upper-area { 34 | text-align: center; 35 | } 36 | 37 | .slider { 38 | width: 50%; 39 | } 40 | 41 | .button-area { 42 | text-align: center; 43 | margin-top: 20px; 44 | } 45 | button { 46 | margin-left: 10px; 47 | margin-right: 10px; 48 | background-color: black; 49 | color: white; 50 | border: 1px solid yellow; 51 | font-size: 20px; 52 | } 53 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import "./App.css"; 2 | import { useState } from "react"; 3 | import ImageCropDialog from "./ImageCropDialog"; 4 | 5 | const initData = [ 6 | { 7 | id: 1, 8 | imageUrl: "images/car1.png", 9 | croppedImageUrl: null, 10 | }, 11 | { 12 | id: 2, 13 | imageUrl: "images/car2.png", 14 | croppedImageUrl: null, 15 | }, 16 | { 17 | id: 3, 18 | imageUrl: "images/car3.png", 19 | croppedImageUrl: null, 20 | }, 21 | { 22 | id: 4, 23 | imageUrl: "images/car4.png", 24 | croppedImageUrl: null, 25 | }, 26 | ]; 27 | 28 | function App() { 29 | const [cars, setCars] = useState(initData); 30 | const [selectedCar, setSelectedCar] = useState(null); 31 | 32 | const onCancel = () => { 33 | setSelectedCar(null); 34 | }; 35 | 36 | const setCroppedImageFor = (id, crop, zoom, aspect, croppedImageUrl) => { 37 | const newCarsList = [...cars]; 38 | const carIndex = cars.findIndex((x) => x.id === id); 39 | const car = cars[carIndex]; 40 | const newCar = { ...car, croppedImageUrl, crop, zoom, aspect }; 41 | newCarsList[carIndex] = newCar; 42 | setCars(newCarsList); 43 | setSelectedCar(null); 44 | }; 45 | 46 | const resetImage = (id) => { 47 | setCroppedImageFor(id); 48 | }; 49 | 50 | return ( 51 |
52 | {selectedCar ? ( 53 | 63 | ) : null} 64 | {cars.map((car) => ( 65 |
66 | { 70 | console.log(car); 71 | setSelectedCar(car); 72 | }} 73 | /> 74 |
75 | ))} 76 |
77 | ); 78 | } 79 | 80 | export default App; 81 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from '@testing-library/react'; 2 | import App from './App'; 3 | 4 | test('renders learn react link', () => { 5 | render(); 6 | const linkElement = screen.getByText(/learn react/i); 7 | expect(linkElement).toBeInTheDocument(); 8 | }); 9 | -------------------------------------------------------------------------------- /src/ImageCropDialog.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import Cropper from "react-easy-crop"; 3 | import getCroppedImg from "./cropImage"; 4 | 5 | const aspectRatios = [ 6 | { value: 4 / 3, text: "4/3" }, 7 | { value: 16 / 9, text: "16/9" }, 8 | { value: 1 / 2, text: "1/2" }, 9 | ]; 10 | 11 | const ImageCropDialog = ({ 12 | id, 13 | imageUrl, 14 | cropInit, 15 | zoomInit, 16 | aspectInit, 17 | onCancel, 18 | setCroppedImageFor, 19 | resetImage, 20 | }) => { 21 | if (zoomInit == null) { 22 | zoomInit = 1; 23 | } 24 | if (cropInit == null) { 25 | cropInit = { x: 0, y: 0 }; 26 | } 27 | if (aspectInit == null) { 28 | aspectInit = aspectRatios[0]; 29 | } 30 | const [zoom, setZoom] = useState(zoomInit); 31 | const [crop, setCrop] = useState(cropInit); 32 | const [aspect, setAspect] = useState(aspectInit); 33 | const [croppedAreaPixels, setCroppedAreaPixels] = useState(null); 34 | 35 | const onCropChange = (crop) => { 36 | setCrop(crop); 37 | }; 38 | 39 | const onZoomChange = (zoom) => { 40 | setZoom(zoom); 41 | }; 42 | 43 | const onAspectChange = (e) => { 44 | const value = e.target.value; 45 | const ratio = aspectRatios.find((ratio) => ratio.value == value); 46 | setAspect(ratio); 47 | }; 48 | 49 | const onCropComplete = (croppedArea, croppedAreaPixels) => { 50 | setCroppedAreaPixels(croppedAreaPixels); 51 | }; 52 | 53 | const onCrop = async () => { 54 | const croppedImageUrl = await getCroppedImg(imageUrl, croppedAreaPixels); 55 | setCroppedImageFor(id, crop, zoom, aspect, croppedImageUrl); 56 | }; 57 | 58 | const onResetImage = () => { 59 | resetImage(id); 60 | }; 61 | 62 | return ( 63 |
64 |
65 |
66 | 75 |
76 |
77 |
78 | { 85 | onZoomChange(e.target.value); 86 | }} 87 | className="slider" 88 | > 89 | 100 |
101 |
102 | 103 | 104 | 105 |
106 |
107 |
108 | ); 109 | }; 110 | 111 | export default ImageCropDialog; 112 | -------------------------------------------------------------------------------- /src/cropImage.js: -------------------------------------------------------------------------------- 1 | const createImage = (url) => 2 | new Promise((resolve, reject) => { 3 | const image = new Image(); 4 | image.addEventListener("load", () => resolve(image)); 5 | image.addEventListener("error", (error) => reject(error)); 6 | image.setAttribute("crossOrigin", "anonymous"); // needed to avoid cross-origin issues on CodeSandbox 7 | image.src = url; 8 | }); 9 | 10 | function getRadianAngle(degreeValue) { 11 | return (degreeValue * Math.PI) / 180; 12 | } 13 | 14 | /** 15 | * This function was adapted from the one in the ReadMe of https://github.com/DominicTobias/react-image-crop 16 | * @param {File} image - Image File url 17 | * @param {Object} pixelCrop - pixelCrop Object provided by react-easy-crop 18 | * @param {number} rotation - optional rotation parameter 19 | */ 20 | export default async function getCroppedImg(imageSrc, pixelCrop, rotation = 0) { 21 | const image = await createImage(imageSrc); 22 | const canvas = document.createElement("canvas"); 23 | const ctx = canvas.getContext("2d"); 24 | 25 | const maxSize = Math.max(image.width, image.height); 26 | const safeArea = 2 * ((maxSize / 2) * Math.sqrt(2)); 27 | 28 | // set each dimensions to double largest dimension to allow for a safe area for the 29 | // image to rotate in without being clipped by canvas context 30 | canvas.width = safeArea; 31 | canvas.height = safeArea; 32 | 33 | // translate canvas context to a central location on image to allow rotating around the center. 34 | ctx.translate(safeArea / 2, safeArea / 2); 35 | ctx.rotate(getRadianAngle(rotation)); 36 | ctx.translate(-safeArea / 2, -safeArea / 2); 37 | 38 | // draw rotated image and store data. 39 | ctx.drawImage( 40 | image, 41 | safeArea / 2 - image.width * 0.5, 42 | safeArea / 2 - image.height * 0.5 43 | ); 44 | const data = ctx.getImageData(0, 0, safeArea, safeArea); 45 | 46 | // set canvas width to final desired crop size - this will clear existing context 47 | canvas.width = pixelCrop.width; 48 | canvas.height = pixelCrop.height; 49 | 50 | // paste generated rotate image with correct offsets for x,y crop values. 51 | ctx.putImageData( 52 | data, 53 | Math.round(0 - safeArea / 2 + image.width * 0.5 - pixelCrop.x), 54 | Math.round(0 - safeArea / 2 + image.height * 0.5 - pixelCrop.y) 55 | ); 56 | 57 | // As Base64 string 58 | // return canvas.toDataURL('image/jpeg'); 59 | 60 | // As a blob 61 | return new Promise((resolve) => { 62 | canvas.toBlob((file) => { 63 | console.log(file); 64 | resolve(URL.createObjectURL(file)); 65 | }, "image/jpeg"); 66 | }); 67 | } 68 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from './reportWebVitals'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | --------------------------------------------------------------------------------