Class: Lizard::Image
- Inherits:
-
Object
- Object
- Lizard::Image
- Defined in:
- lib/lizard/image.rb
Constant Summary collapse
- TYPES =
['jpeg', 'png', 'gif']
Class Method Summary collapse
Instance Method Summary collapse
- #color_model ⇒ Object
- #color_space ⇒ Object
- #crop(width, height, type = "jpeg") ⇒ Object
- #data ⇒ Object
- #height ⇒ Object
- #histogram ⇒ Object
-
#initialize(data) ⇒ Image
constructor
A new instance of Image.
- #inspect ⇒ Object
- #resize(width, height, mode = :resize_down_only, type = "jpeg") ⇒ Object
- #resolution ⇒ Object
- #type ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(data) ⇒ Image
Returns a new instance of Image.
18 19 20 21 |
# File 'lib/lizard/image.rb', line 18 def initialize(data) @data = data @properties = identify end |
Class Method Details
.is_image?(data) ⇒ Boolean
11 12 13 14 15 16 |
# File 'lib/lizard/image.rb', line 11 def self.is_image?(data) image = Image.new(data) true rescue NotAnImage false end |
Instance Method Details
#color_model ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/lizard/image.rb', line 51 def color_model case color_space when/CMYK/i then "CMYK" when /RGB/i then "RBG" else nil end end |
#color_space ⇒ Object
47 48 49 |
# File 'lib/lizard/image.rb', line 47 def color_space @properties[:color_space].strip end |
#crop(width, height, type = "jpeg") ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/lizard/image.rb', line 95 def crop(width, height, type = "jpeg") unless TYPES.include?(type) raise InvalidFileType, "#{type} is not valid. Choose from #{TYPES.join(', ')}" end command = ['convert', '-', '-gravity', 'center', '-extent', "#{width.to_i}x#{height.to_i}", "#{type}:-"] stdout, stderr, exit_code = Lizard.run_command(command, @data) if exit_code == 0 Image.new(stdout) else raise CropFailed, "Image could not be cropped (#{stderr})" end end |
#data ⇒ Object
27 28 29 |
# File 'lib/lizard/image.rb', line 27 def data @data end |
#height ⇒ Object
43 44 45 |
# File 'lib/lizard/image.rb', line 43 def height @properties[:height].to_i end |
#histogram ⇒ Object
109 110 111 |
# File 'lib/lizard/image.rb', line 109 def histogram @histogram ||= Histogram.new(@data) end |
#inspect ⇒ Object
23 24 25 |
# File 'lib/lizard/image.rb', line 23 def inspect "#<Lizard::Image type=#{type}, size=#{width}x#{height}, bytes=#{@data.bytesize}>" end |
#resize(width, height, mode = :resize_down_only, type = "jpeg") ⇒ Object
59 60 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 86 87 88 89 90 91 92 93 |
# File 'lib/lizard/image.rb', line 59 def resize(width, height, mode = :resize_down_only, type = "jpeg") case mode when :default operator = "" when :resize_down_only operator = ">" when :fill operator = "^" when :ignore_aspect operator = "!" else raise InvalidResizeMode, "#{mode} is not a valid" end unless TYPES.include?(type) raise InvalidFileType, "#{type} is not valid. Choose from #{TYPES.join(', ')}" end command = [ 'convert', '-', '-profile', Lizard::COLOR_PROFILES["RGB"], '-flatten', '-resize', "#{width.to_i}x#{height.to_i}#{operator}", "#{type}:-" ] if self.color_model == "CMYK" command.insert(4, '-profile') command.insert(5, COLOR_PROFILES[self.color_model].to_s) end stdout, stderr, exit_code = Lizard.run_command(command, @data) if exit_code == 0 Image.new(stdout) else raise ResizeFailed, "Image could not be resized (#{stderr})" end end |
#resolution ⇒ Object
35 36 37 |
# File 'lib/lizard/image.rb', line 35 def resolution @properties[:resolution] end |
#type ⇒ Object
31 32 33 |
# File 'lib/lizard/image.rb', line 31 def type @properties[:type] end |
#width ⇒ Object
39 40 41 |
# File 'lib/lizard/image.rb', line 39 def width @properties[:width].to_i end |