Skip to content

Instantly share code, notes, and snippets.

@novogrammer
Created June 7, 2025 01:52
Show Gist options
  • Select an option

  • Save novogrammer/7964ffb02aa70ae164de36640c501232 to your computer and use it in GitHub Desktop.

Select an option

Save novogrammer/7964ffb02aa70ae164de36640c501232 to your computer and use it in GitHub Desktop.
画像の解像度を調べてCSSのwidthとheightを表示する
#!/bin/bash
scale=1 # デフォルト値
while getopts "s:" opt; do
case "$opt" in
s) scale="$OPTARG" ;;
esac
done
shift $((OPTIND - 1))
file="$1"
if [[ -z "$file" || ! -f "$file" ]]; then
echo "Usage: $0 [-s scale] image_file"
exit 1
fi
width=$(identify -format "%w" "$file")
height=$(identify -format "%h" "$file")
scaled_width=$(printf "%.0f" "$(echo "$width * $scale" | bc)")
scaled_height=$(printf "%.0f" "$(echo "$height * $scale" | bc)")
echo "background-image: url('$file');"
echo "width: ${scaled_width}px;"
echo "height: ${scaled_height}px;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment