Skip to content

Instantly share code, notes, and snippets.

@johnlinvc
Forked from jtescher/gist:1487555
Last active December 20, 2015 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnlinvc/6077375 to your computer and use it in GitHub Desktop.
Save johnlinvc/6077375 to your computer and use it in GitHub Desktop.
capistrano task for optimize images after asset:precompile using optipng and jpegoptim
namespace :image_compression do
desc 'Optimize images with optipng and jpegoptim'
task :process do
# Check for optipng
if (!`which optipng`.empty? rescue false) # rescue on environments without `which` (windows)
# Crush all .png files inplace
run "find #{shared_path}/assets/ -type f -name '*.png' -print0 | xargs -0 optipng -quiet -o7 "
else
logger.info "WARNING: optipng not found. Skipping..."
end
# Check for jpegoptim
if (!`which jpegoptim`.empty? rescue false) # rescue on environments without `which` (windows)
# Crush all .jpg files in place
run "find #{shared_path}/assets/ -type f -name '*.jpg' -print0 | xargs -0 -n1 sh -c 'jpegoptim --quiet --strip-all $0'"
else
logger.info "WARNING: jpegoptim not found. Skipping..."
end
end
# Set to run after assets:precompile task
after "deploy:assets:precompile", "image_compression:process"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment