class SrcsetImages::CreateImageVersion
Public Class Methods
call(*_)
click to toggle source
CreateImageVersion
.(source, destination, width: 800, height: 600, …)
# File lib/middleman-srcset_images/create_image_version.rb, line 8 def self.call(*_) new(*_).call end
new(source_path, destination_path, options = {})
click to toggle source
# File lib/middleman-srcset_images/create_image_version.rb, line 12 def initialize(source_path, destination_path, options = {}) @source = source_path @destination = destination_path @width = options[:width] @height = options[:height] @crop = !!options[:crop] @gravity = options.fetch :gravity, 'Center' @quality = options.fetch :quality, 90 @ratio = options.fetch :ratio, 1 end
Public Instance Methods
call()
click to toggle source
# File lib/middleman-srcset_images/create_image_version.rb, line 26 def call FileUtils.mkdir_p File.dirname(@destination) image = MiniMagick::Image.open(@source) if @crop crop image else resize image end image.write @destination true end
Private Instance Methods
crop(img)
click to toggle source
# File lib/middleman-srcset_images/create_image_version.rb, line 47 def crop(img) cols, rows = img[:dimensions] img.combine_options do |cmd| if @width != cols || @height != rows scale_x = @width/cols.to_f scale_y = @height/rows.to_f if scale_x >= scale_y cols = (scale_x * (cols + 0.5)).round rows = (scale_x * (rows + 0.5)).round cmd.resize "#{cols}" else cols = (scale_y * (cols + 0.5)).round rows = (scale_y * (rows + 0.5)).round cmd.resize "x#{rows}" end end cmd.gravity @gravity cmd.background "rgba(255,255,255,0.0)" cmd.extent "#{@width}x#{@height}" if cols != @width || rows != @height trim_down cmd end end
resize(img)
click to toggle source
# File lib/middleman-srcset_images/create_image_version.rb, line 40 def resize(img) img.combine_options do |cmd| cmd.resize "#{@width}x#{@height}>" trim_down cmd end end
trim_down(cmd)
click to toggle source
# File lib/middleman-srcset_images/create_image_version.rb, line 71 def trim_down(cmd) cmd.strip cmd.quality @quality cmd.depth "8" cmd.interlace "plane" end