Module: AttachmentSaver::Processors::ImageSize

Includes:
Image
Defined in:
lib/processors/image_size.rb

Constant Summary

Constants included from Image

AttachmentSaver::Processors::Image::DEFAULT_VALID_IMAGE_TYPES

Instance Method Summary collapse

Methods included from Image

#before_validate_attachment, #build_derived, #derived_image?, from_geometry_string, #image?, #process_attachment, #process_attachment?, #update_derived, #valid_image_type, #want_format?

Instance Method Details

#examine_imageObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/processors/image_size.rb', line 12

def examine_image
  image_size = ::ImageSize.path(uploaded_file_path)
  raise ImageSizeProcessorError, "Not an image" if image_size.format.nil?

  self.width = image_size.width if respond_to?(:width)
  self.height = image_size.height if respond_to?(:height)
  self.file_extension = extension_for_image_format(image_size.format) unless self.class.attachment_options[:keep_file_extension]
  self.content_type = mime_type_for_image_format(image_size.format) unless self.class.attachment_options[:keep_content_type]
rescue AttachmentSaverError
  raise
rescue Exception => ex
  raise ImageSizeProcessorError, "#{ex.class}: #{ex.message}", ex.backtrace
end

#extension_for_image_format(format) ⇒ Object



26
27
28
29
30
31
# File 'lib/processors/image_size.rb', line 26

def extension_for_image_format(format)
  case format
  when :jpeg then 'jpg'
  else format.to_s
  end
end

#mime_type_for_image_format(format) ⇒ Object



33
34
35
# File 'lib/processors/image_size.rb', line 33

def mime_type_for_image_format(format)
  "image/#{format}"
end

#with_image(*args) ⇒ Object

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/processors/image_size.rb', line 37

def with_image(*args)
  raise NotImplementedError, "the image_size processor can only be used to check image types and dimensions, not produce resized images"
end