class Inkcite::ImageMinifier

Constants

IMAGE_CACHE

Directory of optimized images

IMAGE_OPTIM_PATH

Path to ImageOption app on OS X

OPTIMIZED_EXTENSION

Extension to put on files tha have been optimized already in the images/ source folder

Public Class Methods

compressed_percent(original_size, compressed_size) click to toggle source
# File lib/inkcite/image_minifier.rb, line 90
def self.compressed_percent original_size, compressed_size
  ((1.0 - (compressed_size / original_size.to_f)) * 100).round(1)
end
minify(email, img_name, force=false) click to toggle source
# File lib/inkcite/image_minifier.rb, line 14
def self.minify email, img_name, force=false

  # Original, unoptimized source image
  source_img = File.join(email.image_dir, img_name)

  # Cached, optimized path for this image.
  cache_path = email.project_file(IMAGE_CACHE)
  cached_img = File.join(cache_path, File.basename(img_name))

  unless force

    # Get the last-modified date of the actual image.  If the source
    # image is newer than the cached version, we'll need to run it
    # through optimization again, too.
    cache_last_modified = Util.last_modified(cached_img)
    source_last_modified = Util.last_modified(source_img)

    # Nothing to do unless the image in the cache is older than the
    # source or the config file.
    return unless source_last_modified > cache_last_modified

  end

  # Make sure the image cache directory exists
  FileUtils.mkpath(cache_path)

  # Copy the original image to the cache where it can be processed.
  FileUtils.copy(source_img, cached_img)

  # Can only optimize the image if ImageOptim app is installed.
  # @TODO Need to do the right thing on Windows.
  if File.file?(IMAGE_OPTIM_PATH)

    original_size = File.size(source_img)

    # Check to see if there is a .optimized marker file indicating that a file
    # shouldn't be further optimized.
    if File.exists?("#{source_img}#{OPTIMIZED_EXTENSION}")
      msg = "Copying #{img_name} #{Util.pretty_file_size(original_size)}"

      compressed_size = original_size

    else
      msg = "Compressing #{img_name} #{Util.pretty_file_size(original_size)}"

      Util.exec("#{IMAGE_OPTIM_PATH} #{cached_img}")

      compressed_size = File.size(cached_img)
      msg << " → #{Util.pretty_file_size(compressed_size)}"

    end

    # Get the final compressed size of the image so we can print the
    # resulting compression ratio.
    msg << " (#{self.compressed_percent(original_size, compressed_size)}%)"

    Util.log msg
  end


end
minify_all(email, force=false) click to toggle source

Minifies all of the images in the provided email's project directory.

# File lib/inkcite/image_minifier.rb, line 77
def self.minify_all email, force=false

  images_path = File.join(email.image_dir, '*.*')

  # Iterate through all of the images in the project and optimize them
  # if necessary.
  Dir.glob(images_path).each do |img|
    next if img.to_s.end_with?(OPTIMIZED_EXTENSION)
    self.minify(email, File.basename(img), force)
  end

end