如何在 Node Jimp 中旋轉影像?


NodeJS – rotate() 是一個內建函式,用於旋轉影像。影像順時針旋轉,影像尺寸保持不變,並且不會對其進行任何更改。

語法

rotate ( r, mode, cb )

rotate() 引數的定義

  • r – 用於儲存影像將要旋轉的角度。

  • mode – 用於儲存影像的縮放方法。這是一個可選引數。

  • cb – 這是一個可選引數,可以在編譯完成後呼叫。

輸入影像

使用 Node JIMP – ROTATE()

在繼續使用 rotate() 函式之前,請檢查以下語句是否已執行以設定環境。

  • npm init -y // 初始化 Node 環境

  • npm install jimp --save // 安裝 jimp 依賴項

  • 建立一個 rotate.js 檔案,並將以下程式碼片段複製貼上到其中。

  • 使用 node rotate.js 執行程式碼。

注意 – 方法名稱應與 JS 檔名匹配。只有這樣,它才能呼叫所需的方法。

示例

const Jimp = require('jimp') ;

async function rotate() {
   // Reading Image
   const image = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   // Checking if any error occurs while rotating image
   image.rotate(300, function(err){
      if (err) throw err;
   })
   .write('/home/jimp/rotate.jpg');
}

rotate();
console.log("Image is processed successfully");

輸出

使用 Node JIMP – ROTATE() 以及 'cb' 可選引數

示例

const Jimp = require('jimp') ;

async function rotate() {
   // Reading Image
   const image = await Jimp.read
   ('/home/jimp/tutorials_point_img.jpg');
   // Checking if any error occurs while rotating image
   image.rotate(159, Jimp.RESIZE_BEZIER, function(err){
      if (err) throw err;
   })
   .write('/home/mayankaggarwal/mysql-test/rotate.jpg');
}

rotate();
console.log("Image is processed successfully");

輸出

更新於: 2021年4月27日

1K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告