Skip to content

Instantly share code, notes, and snippets.

@caiguanhao
Created September 1, 2013 06:10
Show Gist options
  • Save caiguanhao/6402636 to your computer and use it in GitHub Desktop.
Save caiguanhao/6402636 to your computer and use it in GitHub Desktop.
scale images down to 500px and 150px width proportionally
desc 'scale images down to 500px and 150px width proportionally'
task :resize do
require 'fileutils'
Dir.chdir File.dirname(__FILE__)
FileUtils.mkdir '500px' unless File.directory?('500px')
FileUtils.mkdir '150px' unless File.directory?('150px')
images = []
(Dir['*.jpg'] - Dir['500px/**/*.jpg'].map{ |f| File.basename(f) }).each do |jpg|
status = system("convert -resize 500x500 #{jpg} 500px/#{jpg}")
puts "#{status ? 'Success' : 'Failed!'}: #{jpg} -> 500px/#{jpg}"
status = system("convert -resize 150x150 500px/#{jpg} 150px/#{jpg}") if status
puts "#{status ? 'Success' : 'Failed!'}: 500px/#{jpg} -> 150px/#{jpg}"
images << File.realpath("500px/#{jpg}")
images << File.realpath("150px/#{jpg}")
end
if images.length == 0
puts "Nothing to do!"
else
system "open -a ImageOptim #{images.join(' ')}"
end
end
desc 'finish uploading images, put them to done directory'
task :done do
require 'fileutils'
Dir.chdir File.dirname(__FILE__)
%w(500px 150px).each do |width|
FileUtils.mkdir width unless File.directory?(width)
FileUtils.mkdir "#{width}/done" unless File.directory?("#{width}/done")
Dir["#{width}/*.jpg"].map{ |f| File.basename(f) }.each do |file|
FileUtils.mv "#{width}/#{file}", "#{width}/done/#{file}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment