-
-
Save johnlinvc/6077375 to your computer and use it in GitHub Desktop.
capistrano task for optimize images after asset:precompile using optipng and jpegoptim
This file contains 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
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