make_sprite.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/bash
  2. # Based of: https://gist.github.com/jaymzcd/342399 and https://github.com/buren/flag-sprite-maker
  3. # uses imagemagick to stich together all images in a folder and
  4. # then writes a css file with the correct offsets along with a
  5. # test html page for verification that its all good
  6. # Usage:
  7. # $ ./make_sprite.sh path class_name image_extension
  8. set -euo pipefail
  9. IFS=$'\n\t'
  10. name='output'; # output will be placed in a folder named this
  11. path="${1:-}" # Path to flag images
  12. classname=${2:-flag}"-sprite"
  13. ext="."${3:-png}; # the extension to iterate over for input files
  14. css="$name/$classname.css";
  15. html="$name/test.html";
  16. rm -fr $name;
  17. mkdir $name;
  18. touch $css $html;
  19. echo "Generating sprite file...";
  20. convert $path*$ext -append $name/$classname$ext;
  21. echo "Sprite complete! - Creating css & test output...";
  22. echo -e "<html>\n<head>\n\t<link rel=\"stylesheet\" href=\"`basename $css`\" />\n</head>\n<body>\n\t<h1>Sprite test page</h1>\n" >> $html
  23. echo -e ".$classname {\n\tbackground:url('$classname$ext') no-repeat top left; display:inline-block;\n}" >> $css;
  24. counter=0;
  25. offset=0;
  26. for file in $path*$ext
  27. do
  28. width=`identify -format "%[fx:w]" "$file"`;
  29. height=`identify -format "%[fx:h]" "$file"`;
  30. idname=`basename "$file" $ext`;
  31. clean=${idname// /-}
  32. echo -e ".$classname.$clean {" >> $css;
  33. echo -e "\tbackground-position:0 -${offset}px;" >> $css;
  34. echo -e "\twidth: ${width}px;" >> $css;
  35. echo -e "\theight: ${height}px;\n}" >> $css;
  36. echo -e "<div style=\"display:inline-block;width:100px;\"><div style=\"overflow-x:hidden;text-overflow:ellipsis;white-space:nowrap;\">$clean</div> <a href=\"#\" class=\"$classname $clean\"></a></div>\n" >> $html;
  37. let offset+=$height;
  38. let counter+=1;
  39. echo -e "\t#$counter done";
  40. done
  41. echo -e "<h2>Full sprite:</h2>\n<img src=\"$classname$ext\" />" >> $html;
  42. echo -e "</body>\n</html>" >> $html;
  43. echo -e "\nComplete! - $counter sprites created, css written & test page output.";