Sha256: 76ede656be3d1c17d4c3aa2f2785e40e349bea7dbe0f12320954e59b7504a00e

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module MiddlemanSimpleThumbnailer
  class Image
    @@all_objects = []
    attr_accessor :img_path, :middleman_config, :resize_to

    def initialize(img_path, middleman_config)
      @@all_objects << self

      @img_path = img_path
      @middleman_config = middleman_config
    end

    def mime_type
      image.mime_type
    end

    def resized_img_path
      img_path.gsub(image_name, resized_image_name)
    end

    def base64_data
      Base64.encode64(image.to_blob)
    end

    def resize!(resize_to)
      self.resize_to = resize_to
      image.resize(resize_to)
    end

    def save!
      image.write(resized_img_abs_path)
    end

    def self.all_objects
      @@all_objects
    end
    
    
    private

    def image
      @image ||= MiniMagick::Image.open(abs_path)
    end

    def image_name
      File.basename(abs_path)
    end

    def resized_image_name
      image_name.split('.').tap { |a| a.insert(-2, resize_to) }.join('.')      
    end

    def abs_path
      File.join(source_dir, middleman_abs_path)
    end

    def middleman_abs_path
      img_path.start_with?('/') ? img_path : File.join(images_dir, img_path) 
    end

    def resized_img_abs_path
      File.join(build_dir, middleman_abs_path).gsub(image_name, resized_image_name)
    end

    def source_dir
      middleman_config[:source]
    end

    def images_dir
      middleman_config[:images_dir]
    end

    def build_dir
      middleman_config[:build_dir]
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
middleman-simple-thumbnailer-0.0.7 lib/middleman-simple-thumbnailer/image.rb