Class: Media::GraphicsMagickTransmogrifier
- Inherits:
-
Transmogrifier
- Object
- Transmogrifier
- Media::GraphicsMagickTransmogrifier
- Defined in:
- lib/media/transmogrifiers/graphicsmagick.rb
Instance Attribute Summary
Attributes inherited from Transmogrifier
#command_output, #input, #input_file, #input_type, #options, #output, #output_file, #output_type
Class Method Summary collapse
- .available? ⇒ Boolean
-
.average_color(filename) ⇒ Object
returns the average color of an image, as represented by an array of red, green, blue values, integers in the range 0..255.
-
.dimensions(filename) ⇒ Object
try to detect the dimensions of the first page.
- .input_types ⇒ Object
- .output_types ⇒ Object
Instance Method Summary collapse
- #convert(input = input_file.to_s, &block) ⇒ Object
-
#run(&block) ⇒ Object
gm has an option -monitor that will spit out the progress.
Methods inherited from Transmogrifier
add, command_available?, converts_from?, converts_to?, find_class, inherited, #initialize, input_map, list, output_map, #run_command, run_command
Constructor Details
This class inherits a constructor from Media::Transmogrifier
Class Method Details
.available? ⇒ Boolean
33 34 35 |
# File 'lib/media/transmogrifiers/graphicsmagick.rb', line 33 def self.available? command_available?(GRAPHICSMAGICK_COMMAND) end |
.average_color(filename) ⇒ Object
returns the average color of an image, as represented by an array of red, green, blue values, integers in the range 0..255
note: it is important that the geometry is “1x1!” … without the ! this function might die a fiery death.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/media/transmogrifiers/graphicsmagick.rb', line 101 def self.average_color(filename) if available? args = [gm_command, 'convert', '-resize', '1x1!', filename, 'text:-'] color = nil status, color = run_command(*args) if status == :success match = color.match(/^0,0: \(\s*(?<red>\d+),\s*(?<green>\d+),\s*(?<blue>\d+)\)/) if match return [match['red'].to_i, match['green'].to_i, match['blue'].to_i] end end end #if something goes wrong, assume white: return [256,256,256] end |
.dimensions(filename) ⇒ Object
try to detect the dimensions of the first page. fallback to detecting dimensions of all pages.
89 90 91 92 93 |
# File 'lib/media/transmogrifiers/graphicsmagick.rb', line 89 def self.dimensions(filename) return unless available? run_dimensions(filename.to_s + '[0]') || run_dimensions(filename.to_s) end |
.input_types ⇒ Object
21 22 23 24 25 26 |
# File 'lib/media/transmogrifiers/graphicsmagick.rb', line 21 def self.input_types %w( image/jpeg image/pjpeg image/gif image/png image/x-png image/jpg image/tiff ) # application/pdf application/bzpdf application/gzpdf # application/postscript application/xpdf end |
.output_types ⇒ Object
28 29 30 31 |
# File 'lib/media/transmogrifiers/graphicsmagick.rb', line 28 def self.output_types %w( application/pdf image/jpeg image/pjpeg image/gif image/png image/jpg image/tiff ) end |
Instance Method Details
#convert(input = input_file.to_s, &block) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/media/transmogrifiers/graphicsmagick.rb', line 61 def convert(input = input_file.to_s, &block) # +profile '*' will remove all the image profiles, which will save # space (sometimes) and are not useful for thumbnails arguments = [self.class.gm_command, 'convert', '+profile', "*"] if [:size] # handle multiple size options, if it is an array. sizes = [:size].is_a?(Array) ? [:size] : [[:size]] sizes.each do |size| if version_less_than?(1,3,6) size = size.sub('^','!') end arguments << '-geometry' << size end end if [:background] # http://superuser.com/questions/213336/using-graphicsmagick-or-imagemagick-how-do-i-replace-transparency-with-a-fill-c arguments << '-background' << [:background] << '-extent' << '0x0' end if [:crop] # we add '+0+0' because we don't want tiles, just a single image arguments << '-crop' << [:crop]+'+0+0' end arguments << input << output_file run_command(*arguments, &block) end |
#run(&block) ⇒ Object
gm has an option -monitor that will spit out the progress. this could be interesting. we would need to use getc instead of gets on the pipe, since the progress is updated on a single line.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/media/transmogrifiers/graphicsmagick.rb', line 42 def run(&block) if !File.exist?(input_file.to_s) || ( File.size(input_file.to_s) == 0 ) debug "Could not find input file: #{input_file}" return :not_fould end # try converting first page only status = convert(input_file.to_s + '[0]', &block) # retry with full file if result was empty if File.exist?(output_file.to_s) && File.size(output_file.to_s) == 0 # reset filenames to the state before run set_temporary_outfile status = convert(&block) end FileUtils.chmod 0644, output_file.to_s if File.exist? output_file.to_s return status end |