Created
August 2, 2016 07:19
-
-
Save stekan/a6c50774f284862ab9da766e1b8a4635 to your computer and use it in GitHub Desktop.
Generate Images for srcset with ImageMagick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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