Skip to content

Instantly share code, notes, and snippets.

@stekan
Created August 2, 2016 07:19
Show Gist options
  • Save stekan/a6c50774f284862ab9da766e1b8a4635 to your computer and use it in GitHub Desktop.
Save stekan/a6c50774f284862ab9da766e1b8a4635 to your computer and use it in GitHub Desktop.
Generate Images for srcset with ImageMagick
#!/bin/bash
## define image directory
DIR=.build/src/webroot/uploads
## define image sizes
sizes=(320 640 1280)
## imagemagick function
## convert $1(image) $2(width) $3(newname)
resize() {
convert $1 -thumbnail $2 $3
}
## find all images
for image in $(find ${DIR} -iregex ".*\.\(jpg\|gif\|png\|jpeg\)");
do
## get image width
width=`convert $image -ping -format "%w" info:`
## get image path and name
dir=$(dirname "$image")
filename=$(basename "$image")
## set new image name
newname="$dir"/"$width"_"$filename"
## resize image with original width
resize "$image" $width "$newname"
## run through image sizes
for size in ${sizes[@]}; do
## set new image name
newname="$dir"/"$size"_"$filename"
## resize image with define widths
resize "$image" $size "$newname"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment