Skip to content

Instantly share code, notes, and snippets.

@jtescher
Created December 16, 2011 19:31
Show Gist options
  • Save jtescher/1487555 to your computer and use it in GitHub Desktop.
Save jtescher/1487555 to your computer and use it in GitHub Desktop.
Capistrano task: Optimize images with pngcrush and jpegoptim
namespace :image_compression do
desc 'Optimize images with pngcrush and jpegoptim'
task :process do
# Check for pngcrush
if (!`which pngcrush`.empty? rescue false) # rescue on environments without `which` (windows)
# Crush all .png files
run "find #{shared_path}/assets/ -type f -name '*.png' -print0 | xargs -0 pngcrush -q -e .crushed"
# Replace original with crushed version
run "find #{shared_path}/assets/ -type f -name '*.crushed' -print0 | xargs -0 -n1 sh -c 'mv $0 ${0%.crushed}.png'"
else
logger.info "WARNING: pngcrush 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