Module: AttachmentSaver::Processors::ImageScience
- Includes:
- Image
- Defined in:
- lib/processors/image_science.rb
Defined Under Namespace
Modules: Operations
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, #want_format?
Instance Method Details
#examine_attachment ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/processors/image_science.rb', line 22
def examine_attachment
with_image_attributes(uploaded_file_path) do |original_image|
self.width = original_image.width if respond_to?(:width)
self.height = original_image.height if respond_to?(:height)
self.content_type = original_image.mime_type unless self.class.attachment_options[:keep_content_type] || original_image.mime_type.nil?
self.file_extension = original_image.file_type_extension unless self.class.attachment_options[:keep_file_extension] || original_image.file_type_extension.nil?
end
rescue AttachmentSaverError
raise
rescue Exception => ex
raise ImageScienceProcessorError, "#{ex.class}: #{ex.message}", ex.backtrace
end
|
#process_image(original_image, derived_format_name, resize_format) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/processors/image_science.rb', line 35
def process_image(original_image, derived_format_name, resize_format)
resize_format = Image.from_geometry_string(resize_format) if resize_format.is_a?(String)
original_image.send(*resize_format) do |derived_image|
return nil unless want_format?(derived_format_name, derived_image.width, derived_image.height)
if derived_image.file_type == 'GIF' derived_content_type = 'image/png'
derived_extension = 'png'
else
derived_content_type = derived_image.mime_type || original_image.mime_type || content_type derived_extension = (derived_image.file_type || file_extension).downcase end
temp = ExtendedTempfile.new("asitemp", tempfile_directory, derived_extension)
temp.binmode
temp.close
derived_image.save(temp.path)
temp.open
{ :format_name => derived_format_name.to_s,
:width => derived_image.width,
:height => derived_image.height,
:content_type => derived_content_type,
:file_extension => derived_extension,
:uploaded_data => temp }
end
end
|
#with_image(filename, &block) ⇒ Object
13
14
15
|
# File 'lib/processors/image_science.rb', line 13
def with_image(filename, &block)
::ImageScience.with_image(filename) {|image| block.call(image.extend(Operations))}
end
|
#with_image_attributes(filename, &block) ⇒ Object
17
18
19
20
|
# File 'lib/processors/image_science.rb', line 17
def with_image_attributes(filename, &block)
return with_image(filename, &block) unless ::ImageScience.respond_to?(:with_image_attributes)
::ImageScience.with_image_attributes(filename) {|image| block.call(image)}
end
|