Imagemagickを使用してスクリプトで画像をサイズ変更する

Imagemagickを使用してスクリプトで画像をサイズ変更する

画像サイズを800pxに調整して比率を維持するbashスクリプトを作成したいと思います。

私のコードはbashでは動作しませんが、identify単一の画像では動作します。

#!/bin/bash
for file in ./**/public/uploads/*.*; do
  width = $(identify -format "%w" ${file})
  if [ width > 800 ]
  then
    echo $file // resize image
  fi
done
exit 0;

質問:私はPermission denied3号線を利用します。

以下のいずれかの回答で提供されたソリューションを試しました。

#!/bin/bash
shopt -s globstar
for file in ./**/public/uploads/*.*; do 
  width=$(identify -format "%w" "${file}")
  if [ "$width" -gt 800 ]
  then
    echo "$file"
  fi
done
exit 0;

これで、次のエラーメッセージが表示されます。

identify.im6: Image corrupted   ./folder/public/uploads/ffe92053ca8c61835aa5bc47371fd3e4.jpg @ error/gif.c/PingGIFImage/952.
./images.sh: line 6: width: command not   found
./images.sh: line 7: [integer expression expected

ベストアンサー1

あなたのスクリプトには2つの明らかな問題があります。まず、デフォルトでは有効になっていないオプション**として提供されます。globstar対話型シェルでこれを有効にした可能性がありますが、スクリプトに対してもこれを行う必要があります。

これは実際に比較するのではなく、$width文字列を比較することですwidth$必要[ ]

最後の問題は、実行中のファイルの一部が破損しているか、イメージではないことです。とにかくidentifyコマンドが失敗するので$width空です。簡単な解決策は$widthnull(-z "$width")をテストし、null(! -z "$width")のみを比較することです。

この試み:

#!/bin/bash
shopt -s globstar
for file in ./**/public/uploads/*.*; do 
  width=$(identify -format "%w" "${file}")
  if [[ ! -z "$width" && "$width" -gt 800 ]]
  then
    echo "$file"
  fi
done
exit 0;

おすすめ記事