9
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【n8n】Google Driveに画像を置くだけで自動リサイズ(1280×720)するワークフロー

9
Posted at

はじめに

画像のリサイズって地味に面倒じゃないですか?

  • サムネ用に1280×720にしたい
  • 毎回手作業でリサイズしてる
  • まとめて処理したい

そんなときに便利なのが n8n × Google Drive の自動化です。

この記事では、

Google Driveに画像をアップするだけで自動で1280×720にリサイズして保存する仕組み

を紹介します。

できること

このワークフローでできること

  • 指定フォルダに画像をアップ
  • 自動で検知
  • 画像だけフィルタ
  • 1280×720にリサイズ&中央クロップ
  • 別フォルダに保存

完全放置でOKです。

全体の流れ

Google Drive監視
   ↓
画像だけ通す
   ↓
ダウンロード
   ↓
サイズ取得
   ↓
リサイズ計算
   ↓
リサイズ
   ↓
中央クロップ
   ↓
アップロード

各機能の解説

① Google Drive監視

特定フォルダを監視して
新しいファイルが追加されたらトリガー発火します。

② 画像だけフィルタ

mimeType startsWith "image/"

画像以外(PDFとか)を除外します。

③ ファイルダウンロード

Google Driveから対象ファイルを取得

④ 画像サイズ取得

ここで

  • width
  • height

を取得

⑤ リサイズ&クロップ計算(重要)

const targetW = 1280;
const targetH = 720;

const originalW = $json.width;
const originalH = $json.height;

// 比率を維持して拡大
const scale = Math.max(targetW / originalW, targetH / originalH);

const resizedW = Math.round(originalW * scale);
const resizedH = Math.round(originalH * scale);

// 中央クロップ位置
const cropX = Math.max(0, Math.floor((resizedW - targetW) / 2));
const cropY = Math.max(0, Math.floor((resizedH - targetH) / 2));

ポイント

  • アスペクト比を崩さない
  • 小さい辺に合わせて拡大
  • はみ出た部分を中央でカット

⑥ リサイズ処理

resizeOption: minimumArea

1280×720を覆うように拡大します。

⑦ 中央クロップ

width: 1280
height: 720
position: 中央

最終的にピッタリ1280×720になるようにします。

⑧ Google Driveに保存

  • ファイル名に _1280x720 を付与
  • 別フォルダに保存

例:
image.jpg → image_1280x720.jpg

この構成の良いところ

✔ 完全自動
アップするだけでOK

✔ アスペクト比維持
画像が歪まない

✔ サムネに最適
YouTubeやブログにそのまま使える

✔ n8nだけで完結
外部API不要

まとめ

n8nを使えば、
「面倒な画像処理」を完全自動化できます

特に今回のような

  • 定型サイズ
  • 繰り返し作業

は自動化との相性が抜群でした。

9
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?