class FindDupeImages::Image

Attributes

image[RW]

Public Class Methods

new(image) click to toggle source
# File lib/find_dupe_images/image.rb, line 6
def initialize(image)
  @raw_image    = image
  @image_types  = %w(GIF JPEG PNG TIFF BMP ICO CUR PSD SVG WEBP)
  return unless is_image_and_not_too_big?
  read_image
end

Public Instance Methods

is_image?() click to toggle source
# File lib/find_dupe_images/image.rb, line 13
def is_image?
  @image.is_a?(Magick::Image)
end

Private Instance Methods

image_is_too_big?() click to toggle source
# File lib/find_dupe_images/image.rb, line 23
def image_is_too_big?
  if is_too_big?
    $too_big += 1
    FindDupeImages.logger.log({error: "The image #{@raw_image} is too big!"}.to_json)
    return true
  end
  false
end
image_type() click to toggle source
# File lib/find_dupe_images/image.rb, line 47
def image_type
  @image.format
end
is_image_and_not_too_big?() click to toggle source
# File lib/find_dupe_images/image.rb, line 19
def is_image_and_not_too_big?
  !image_is_too_big? && mime_is_image?
end
is_too_big?() click to toggle source
# File lib/find_dupe_images/image.rb, line 43
def is_too_big?
  File.size?(@raw_image).to_f / (1024) > FindDupeImages::MAX_FILE_SIZE
end
mime_is_image?() click to toggle source
# File lib/find_dupe_images/image.rb, line 32
def mime_is_image?
  fm = ::FileMagic.new(::FileMagic::MAGIC_MIME)
  mime_type = fm.file(@raw_image).split(';').first
  if FindDupeImages::IMAGE_MIME_TYPES.include?(mime_type)
    return true
  else
    $not_an_image += 1
    false
  end
end
read() click to toggle source
# File lib/find_dupe_images/image.rb, line 55
def read
  begin
    Magick::Image.read(@raw_image).first
  rescue Magick::ImageMagickError => e
    FindDupeImages.logger.log({error: e.message}.to_json)
  end
end
read_image() click to toggle source
# File lib/find_dupe_images/image.rb, line 51
def read_image
  @image = read
end