module TheImage

Public Instance Methods

auto_orient(image) click to toggle source
# File lib/the_image.rb, line 87
def auto_orient image
  image.auto_orient
  image
end
biggest_side_not_bigger_than(image, size) click to toggle source
# File lib/the_image.rb, line 72
def biggest_side_not_bigger_than image, size
  if landscape?(image)
    image.resize("#{ size }x") if image[:width] > size.to_i
  else
    image.resize("x#{ size }") if image[:height] > size.to_i
  end

  image
end
create_path_for_file(file_path) click to toggle source
# File lib/the_image.rb, line 18
def create_path_for_file file_path
  FileUtils.mkdir_p path_to_file(file_path)
end
crop(image, x0 = 0, y0 = 0, w = 100, h = 100, scale = 1) click to toggle source

scale = original_iamge.to_f / image_on_screen.to_f usually scale should be 1

# File lib/the_image.rb, line 137
def crop image, x0 = 0, y0 = 0, w = 100, h = 100, scale = 1
  x0 = (x0.to_f * scale).to_i
  y0 = (y0.to_f * scale).to_i

  w = (w.to_f * scale).to_i
  h = (h.to_f * scale).to_i

  image.crop "#{ w }x#{ h }+#{ x0 }+#{ y0 }"
  image
end
destroy_dir(dir_path) click to toggle source
# File lib/the_image.rb, line 14
def destroy_dir dir_path
  FileUtils.rm_rf dir_path
end
destroy_dir_of_file(file_path) click to toggle source
# File lib/the_image.rb, line 22
def destroy_dir_of_file file_path
  FileUtils.rm_rf path_to_file(file_path)
end
destroy_file(file_path) click to toggle source
# File lib/the_image.rb, line 10
def destroy_file file_path
  FileUtils.rm(file_path , force: true)
end
landscape?(image) click to toggle source

Main methods

# File lib/the_image.rb, line 27
def landscape? image
  image[:width] > image[:height]
end
manipulate(opts = {}) click to toggle source
# File lib/the_image.rb, line 35
def manipulate opts = {}, &block
  src    = opts[:src]
  dest   = opts[:dest]
  format = opts[:format]

  image  = ::MiniMagick::Image.open src
  image.format(format.to_s.downcase) if format

  image = instance_exec(image, opts, &block)
  image.write dest
end
path_to_file(file_path) click to toggle source

Helpers

# File lib/the_image.rb, line 6
def path_to_file file_path
  file_path.split('/')[0...-1].join('/')
end
portrait?(image) click to toggle source
# File lib/the_image.rb, line 31
def portrait? image
  image[:width] < image[:height]
end
resize(image, w, h) click to toggle source
# File lib/the_image.rb, line 54
def resize image, w, h
  image.resize "#{ w }x#{ h }"
  image
end
rotate(image, angle) click to toggle source
# File lib/the_image.rb, line 59
def rotate image, angle
  image.rotate angle
  image
end
rotate_left(image) click to toggle source
# File lib/the_image.rb, line 64
def rotate_left image
  rotate image, '-90'
end
rotate_right(image) click to toggle source
# File lib/the_image.rb, line 68
def rotate_right image
  rotate image, '90'
end
strict_resize(image, w, h) click to toggle source

Image manipulate Resizing can be wrong when .EXT of file is wrong

# File lib/the_image.rb, line 49
def strict_resize image, w, h
  image.resize "#{ w }x#{ h }!"
  image
end
strip(image) click to toggle source
# File lib/the_image.rb, line 82
def strip image
  image.strip
  image
end
to_rect(image, width, height, opts = {}) click to toggle source
# File lib/the_image.rb, line 92
def to_rect image, width, height, opts = {}
  default_opts = { valign: :center, align: :center }
  opts = default_opts.merge(opts)

  align  = opts[:align].to_sym
  valign = opts[:valign].to_sym

  w0, h0 = image[:width].to_f, image[:height].to_f
  w1, h1 = width.to_f, height.to_f
  fw, fh = w0, h0

  scale = ((w1 / w0) > (h1 / h0)) ? (w1 / w0) : (h1 / h0)

  fw = (w1 / scale).to_i
  fh = (h1 / scale).to_i

  x0 = case align
    when :center
      ((w0 - fw) / 2).to_i
    when :right
      (w0 - fw).to_i
    else
      0
  end

  y0 = case valign
    when :center
      ((h0 - fh) / 2).to_i
    when :bottom
      (h0 - fh).to_i
    else
      0
  end

  image.crop   "#{ fw }x#{ fh }+#{ x0 }+#{ y0 }"
  image.resize "#{ width }x#{ height }!"
  image
end
to_square(image, size, opts = {}) click to toggle source
# File lib/the_image.rb, line 131
def to_square image, size, opts = {}
  to_rect image, size, size, opts
end