Skip to content

Commit

Permalink
Rename optimize script
Browse files Browse the repository at this point in the history
  • Loading branch information
rossta committed Dec 15, 2024
1 parent 39105c9 commit 8bb7f4d
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions script/convert-webp → script/convert-images
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,27 @@
# require "bundler/setup"

require "fastimage"
require "optparse"

force = false
options = {
quality: 90,
force: false
}
OptionParser.new do |opts|
opts.banner = "Usage: script/convert-webp.rb [options]"

opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end

opts.on("-f", "--force", "Force conversion") do |f|
options[:force] = f
end

opts.on("-q", "--quality [QUALITY]", "Quality of the output image") do |q|
options[:quality] = q
end
end.parse!

class ImageFile
def exists?
Expand Down Expand Up @@ -56,8 +75,8 @@ module Cmd
system cmd
end

def self.webp(img, force: false, quality: 90, tries: 0)
dest = img.as_webp.to_s
def self.webp(img, dest: nil, force: false, quality: 90, tries: 0, **)
dest ||= img.as_webp.to_s

if force || !File.exist?(dest)
cmd = "magick #{img}"
Expand All @@ -83,7 +102,7 @@ module Cmd
end
end

def self.jpg(img, dest: nil, force: false, quality: 90, tries: 0)
def self.jpg(img, dest: nil, force: false, quality: 90, tries: 0, **)
dest ||= img.as_opt_jpg.to_s
if force || !File.exist?(dest)
run "magick #{img} -strip -interlace Plane -quality #{quality}% #{dest}"
Expand All @@ -101,15 +120,15 @@ ARGV.each do |file|
img = ImageFile.new(file)
next unless %w[.jpg .jpeg .png].include?(img.ext)
next if img.to_s.include?("-opt.jpg")
Cmd.webp img, force: force
Cmd.webp img, **options
end

Dir.glob(file).each do |file|
img = ImageFile.new(file)
next unless %w[.jpg .jpeg].include?(img.ext)
next if img.to_s.include?("-opt.jpg")
next if File.size(img) < 32_768 # 32kB
Cmd.jpg img, force: force
Cmd.jpg img, **options
end

Dir.glob(file).each do |file|
Expand Down

0 comments on commit 8bb7f4d

Please sign in to comment.