From: Nick Bowler Date: Tue, 7 Jul 2020 01:16:36 +0000 (-0400) Subject: Don't regenerate smaller images so often. X-Git-Url: http://git.draconx.ca/gitweb/homepage.git/commitdiff_plain/00a9b42301aeb19c08c43f3774dd4e2e27945b5e Don't regenerate smaller images so often. It is a bit annoying how nanoc always runs the rule to regenerate resized images, even if none of the inputs are changed at all. Since the imgresize filter is quite expensive to run, let's try hacking around the problem by passing in the final output filename and just re-using that if it exists and its mtime is newer than the source image. This is definitely not the most accurate as it will not automatically regenerate images if the rules change, but I expect any problems due to this will waste less of my time than regenerating dozens of redundant images every single time the site is compiled. --- diff --git a/Rules b/Rules index a881607..e00c5f3 100644 --- a/Rules +++ b/Rules @@ -149,8 +149,9 @@ compile '/license/cc*.xhtml' do end compile '/images/*.jpg', rep: :large do - filter :imgresize, width: 1200, height: 1200 - write item.identifier.without_ext + '-t1200.' + item.identifier.ext + filename = item.identifier.without_ext + '-t1200.' + item.identifier.ext + filter :imgresize, width: 1200, height: 1200, cache: filename + write filename end compile '/images/*.jpg', rep: :info do diff --git a/lib/imgresize.rb b/lib/imgresize.rb index e539f37..b14530b 100644 --- a/lib/imgresize.rb +++ b/lib/imgresize.rb @@ -20,6 +20,27 @@ class ImgResize < Nanoc::Filter identifier :imgresize type :binary + def fetch_from_cache(filename, params) + return unless params[:cache] + + cachefile = File.join(@config[:output_dir], params[:cache]) + begin + s = File.stat(cachefile) + rescue Errno::ENOENT + return + end + + return unless s.mtime >= File.stat(filename).mtime + + begin + File.link(cachefile, output_filename) + rescue + FileUtils.copy_file(cachefile, output_filename) + end + + return true + end + def run(filename, params = {}) w = if params[:width] then params[:width].to_i end h = if params[:height] then params[:height].to_i end @@ -32,6 +53,8 @@ class ImgResize < Nanoc::Filter end args << output_filename - system('gm', 'convert', *args) + unless fetch_from_cache(filename, params) + system('gm', 'convert', *args) + end end end