Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 9, 2023 02:35
Show Gist options
  • Save code-boxx/a47dbc84c9f3dc1d22461493c7519f6a to your computer and use it in GitHub Desktop.
Save code-boxx/a47dbc84c9f3dc1d22461493c7519f6a to your computer and use it in GitHub Desktop.
NodeJS Crop Images

NODEJS CROP IMAGES

https://code-boxx.com/crop-images-nodejs/

NOTES

  1. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Save the image below as demo.png
    • Install required modules - npm i sharp
    • Run the examples.

IMAGES

demo

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// (A) LOAD SHARP
const sharp = require("sharp");
// (B) OPEN IMAGE FILE
sharp("demo.png")
// (C) CROP 150 X 150 SQUARE FROM CENTER
.resize(150, 150)
// (D) SAVE TO FILE
.toFile("demoA.png")
// (E) OPTIONAL - ON OK & HANDLE ERRORS
.then(info => console.log(info))
.catch(err => console.log(err));
// (A) LOAD SHARP
const sharp = require("sharp");
// (B) OPEN IMAGE FILE
sharp("demo.png")
// (C) CROP
.resize(150, 150, {
// cover | contain | fill
fit: "cover",
// top | right top | right bottom | left top | left bottom
position: "left top"
})
// (D) SAVE TO FILE
.jpeg({ quality: 30 }) // optional
.toFile("demoB.jpg")
// (E) OPTIONAL - ON OK & HANDLE ERRORS
.then(info => console.log(info))
.catch(err => console.log(err));
// (A) LOAD SHARP
const sharp = require("sharp");
// (B) OPEN IMAGE FILE
sharp("demo.png")
// (C) EXTRACT FROM SPECIFIED COORDINATES
.extract({
left: 200, top: 80,
width: 160, height: 160
})
// (D) SAVE TO FILE
.toFile("demoC.png")
// (E) OPTIONAL - ON OK & HANDLE ERRORS
.then(info => console.log(info))
.catch(err => console.log(err));
curl https://user-images.githubusercontent.com/11156244/281596028-18c06b68-d913-49bf-887f-ffd2b5bf5b24.png --ssl-no-revoke --output demo.png
call npm i sharp
node 1-simple.js
node 2-more.js
node 3-exact.js
curl https://user-images.githubusercontent.com/11156244/281596028-18c06b68-d913-49bf-887f-ffd2b5bf5b24.png --ssl-no-revoke --output ./demo.png
source "npm i sharp"
node ./1-simple.js
node ./2-more.js
node ./3-exact.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment