This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress_images.rb
executable file
·85 lines (69 loc) · 2.2 KB
/
compress_images.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env ruby -w
require 'fileutils'
config = {
"assets/images" =>
{
"source" => "originals",
"targets" => {
"desktop" => {
"-resize" => "3000x",
"-quality" => "50"
},
"mobile" => {
"-resize" => "1000x",
"-quality" => "50"
}
}
},
"assets/headshots" =>
{
"source" => "originals",
"targets" => {
"compressed" => {
"-resize" => "600x",
"-quality" => "50"
}
}
},
"assets/images/londonliving" =>
{
"source" => "originals",
"targets" => {
"compressed" => {
"-resize" => "600x",
"-quality" => "50"
}
}
}
}
filename = "*.jpg"
config.each do |source,source_config|
basedir = File.join(source,source_config['source'])
original_images = Dir.glob(File.join(basedir, filename))
source_config['targets'].each do |target_name,target_config|
puts "Optimising images in #{basedir} for #{target_name}"
target_directory = File.join(source,target_name)
FileUtils.mkdir_p(target_directory) unless File.directory?(target_directory)
options = target_config.map {|key,value| "#{key} #{value}"}.join(" ")
original_images.each do |original_image_path|
file_name = File.basename(original_image_path)
target_file = File.join(target_directory,file_name)
skip = false
if File.exist?(target_file)
compressed_file_mtime = File.mtime(target_file)
original_file_mtime = File.mtime(original_image_path)
if original_file_mtime < compressed_file_mtime
skip = true
puts "Original image is older - skipping"
else
puts "Original is newer than compressed - re-compressing"
end
end
unless skip
command = "convert #{original_image_path} #{options} #{target_file}"
puts command
`#{command}`
end
end
end
end