module AttachmentSaver::Processors::Image

shared code for all image processors

Constants

DEFAULT_VALID_IMAGE_TYPES

Public Class Methods

from_geometry_string(geom) click to toggle source

unpacks a resize geometry string into an array contining the corresponding image operation method name (see Operations below, plus any from your chosen image processor) followed by the arguments to that method.

# File lib/processors/image.rb, line 116
def self.from_geometry_string(geom)
  match, w, cross, h, flag = geom.match(/^(\d+\.?\d*)?(?:([xX])(\d+\.?\d*)?)?([!%<>#*])?$/).to_a
  raise "couldn't parse geometry string '#{geom}'" if match.nil? || (w.nil? && h.nil?)
  h = w unless cross # there's <w>x<h>, there's <w>x, there's x<h>, and then there's just plain <n>, which means <w>=<h>=<n>
  return [:scale_by, (w || h).to_f/100, (h || w).to_f/100] if flag == '%'
  operation = case flag
    when nil then :scale_to_fit
    when '!' then w && h ? :squish : :scale_to_fit
    when '>' then :shrink_to_fit
    when '<' then :expand_to_fit
    when '*' then :scale_to_cover
    when '#' then :cover_and_crop
  end
  [operation, w ? w.to_i : nil, h ? h.to_i : nil]
end

Public Instance Methods

before_validate_attachment() click to toggle source
# File lib/processors/image.rb, line 39
def before_validate_attachment
  unless uploaded_file.nil? || derived_image?
    return false unless valid_image_type
    examine_image
  end
rescue ImageProcessorError
  # we examine all files, regardless of whether the client browser labelled them an
  # image, because they may be an image with the wrong extension or content type.
  # but this will raise for non-image files, so ignore such errors but make sure
  # image? will return false, if it doesn't already.
  self.content_type = "application/octet-stream" if image?
end
build_derived(attributes) click to toggle source

builds a new derived image model instance, but doesn't save it. provided so apps can easily override this step.

# File lib/processors/image.rb, line 100
def build_derived(attributes)
  formats.build(attributes)
end
derived_image?() click to toggle source

determines if this is a derived image. used to prevent infinite recursion when storing the derived images in the same model as the originals (and as a secondary benefit avoid unnecessary work examining images for derived images, for which the full metadata is already filled in by the resizing code).

# File lib/processors/image.rb, line 60
def derived_image?
  respond_to?(:format_name) && !format_name.blank?
end
image?() click to toggle source
# File lib/processors/image.rb, line 12
def image?
  return false if content_type.blank?
  parts = content_type.split(/\//)
  parts.size == 2 && parts.first.strip == 'image'
end
process_attachment(filename) click to toggle source
# File lib/processors/image.rb, line 72
def process_attachment(filename)
  with_image(filename) do |original_image|
    unless self.class.attachment_options[:formats].blank?
      old_children = new_record? ? {} : formats.group_by(&:format_name)
      self.class.attachment_options[:formats].each do |derived_name, resize_format|
        derived_attributes = process_image(original_image, derived_name, resize_format)
        if derived_attributes
          if old_children[derived_name.to_s]
            update_derived(old_children[derived_name.to_s].pop, derived_attributes)
          else
            build_derived(derived_attributes)
          end
        end
      end
      old_children = old_children.values.flatten
      formats.destroy(old_children) unless old_children.blank? # remove any old derived images for formats for which want_format? now returned false
    end
  end
rescue AttachmentSaverError
  raise
rescue NotImplementedError
  raise
rescue Exception => ex
  raise ImageProcessorError, "#{ex.class}: #{ex.message}", ex.backtrace
end
process_attachment?() click to toggle source
# File lib/processors/image.rb, line 52
def process_attachment?
  image? && !self.class.attachment_options[:formats].blank? && !derived_image?
end
update_derived(derived, attributes) click to toggle source

updates an existing derived image model instance, and queues it for save when this model is saved. provided so apps can easily override this step.

# File lib/processors/image.rb, line 106
def update_derived(derived, attributes)
  derived.attributes = attributes
  @updated_derived_children ||= []
  @updated_derived_children << derived # we don't want to save it just yet in case processing subsequent images fail; rails will automatically save it if we're a new record, but we have to do it ourselves in an after_save if not
  derived
end
valid_image_type() click to toggle source
# File lib/processors/image.rb, line 18
def valid_image_type
  valid_image_types = self.class.attachment_options[:valid_image_types] || DEFAULT_VALID_IMAGE_TYPES

  uploaded_file.rewind
  magic = MimeMagic.by_magic(uploaded_file)

  if magic.nil?
    # if it doesn't look like an image, make sure it's not labelled as an image
    self.content_type = 'application/octet-stream' if image? || content_type.nil?
  elsif !valid_image_types.include?(magic.type)
    # overwrite the content type given by the untrusted client with the real content type; it may get refined later by the image processor
    self.content_type = magic.type
  else
    # seems legit
    return true
  end

  errors.add(:content_type, "is invalid") if respond_to?(:errors)
  false
end
want_format?(derived_name, derived_width, derived_height) click to toggle source

determines if a particular configured derived image should be created. this implementation, which always returns true, may be overridden by applications to make certain formats conditional (for example, only creating certain larger sizes if the original image was at least that large).

# File lib/processors/image.rb, line 68
def want_format?(derived_name, derived_width, derived_height)
  true
end