Class: MiniMagick::Image
- Inherits:
-
Object
- Object
- MiniMagick::Image
- Defined in:
- lib/mini_magick/image.rb,
lib/mini_magick/image/info.rb
Defined Under Namespace
Classes: Info
Instance Attribute Summary collapse
- #colorspace ⇒ String readonly
- #dimensions ⇒ Array<Integer> readonly
- #exif ⇒ Hash readonly
- #height ⇒ Integer readonly
- #mime_type ⇒ String readonly
-
#path ⇒ String
readonly
The location of the current working file.
-
#resolution ⇒ Array<Integer>
readonly
Returns the resolution of the photo.
-
#signature ⇒ String
readonly
Returns the message digest of this image as a SHA-256, hexidecimal encoded string.
-
#size ⇒ Integer
readonly
Returns the file size of the image.
-
#type ⇒ String
readonly
Returns the image format (e.g. “JPEG”, “GIF”).
- #width ⇒ Integer readonly
Class Method Summary collapse
- .attribute(name, key = name.to_s) ⇒ Object
-
.create(ext = nil, validate = MiniMagick.validate_on_create) {|Tempfile| ... } ⇒ MiniMagick::Image
Used to create a new Image object data-copy.
-
.import_pixels(blob, columns, rows, depth, map, format = 'png') ⇒ MiniMagick::Image
Creates an image object from a binary string blob which contains raw pixel data (i.e. no header data).
-
.open(path_or_url, ext = nil) ⇒ MiniMagick::Image
Opens a specific image file either on the local file system or at a URI.
-
.read(stream, ext = nil) ⇒ MiniMagick::Image
This is the primary loading method used by all of the other class methods.
Instance Method Summary collapse
-
#[](value) ⇒ String
(also: #info)
Use this method if you want to access raw Identify’s format API.
-
#collapse!(frame = 0) ⇒ self
Collapse images with sequences to the first frame (i.e. animated gifs) and preserve quality.
-
#combine_options {|MiniMagick::Tool::Mogrify| ... } ⇒ self
You can use multiple commands together using this method.
- #composite(other_image, output_extension = 'jpg', mask = nil) ⇒ Object
-
#destroy! ⇒ Object
Destroys the tempfile (created by Image.open) if it exists.
- #eql?(other) ⇒ Boolean (also: #==)
-
#format(format, page = 0) {|MiniMagick::Tool::Convert| ... } ⇒ self
This is used to change the format of the image.
- #hash ⇒ Object
-
#identify {|MiniMagick::Tool::Identify| ... } ⇒ String
Runs ‘identify` on itself.
-
#initialize(input_path, tempfile = nil) {|MiniMagick::Tool::Mogrify| ... } ⇒ Image
constructor
Create a new Image object.
-
#layers ⇒ Array<MiniMagick::Image>
(also: #pages, #frames)
Returns layers of the image.
-
#method_missing(name, *args) ⇒ self
If an unknown method is called then it is sent through the mogrify program.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #run_command(tool_name, *args) ⇒ Object
-
#to_blob ⇒ String
Returns raw image data.
-
#valid? ⇒ Boolean
Checks to make sure that MiniMagick can read the file and understand it.
-
#validate! ⇒ Object
Runs ‘identify` on the current image, and raises an error if it doesn’t pass.
-
#write(output_to) ⇒ Object
Writes the temporary file out to either a file location (by passing in a String) or by passing in a Stream that you can #write(chunk) to repeatedly.
Constructor Details
#initialize(input_path, tempfile = nil) {|MiniMagick::Tool::Mogrify| ... } ⇒ Image
Create a new MiniMagick::Image object.
DANGER: The file location passed in here is the *working copy*. That is, it gets modified. You can either copy it yourself or use open which creates a temporary file for you and protects your original.
148 149 150 151 152 153 154 |
# File 'lib/mini_magick/image.rb', line 148 def initialize(input_path, tempfile = nil, &block) @path = input_path @tempfile = tempfile @info = MiniMagick::Image::Info.new(@path) (&block) if block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ self
If an unknown method is called then it is sent through the mogrify program.
377 378 379 380 381 382 383 384 385 |
# File 'lib/mini_magick/image.rb', line 377 def method_missing(name, *args) mogrify do |builder| if builder.respond_to?(name) builder.send(name, *args) else super end end end |
Instance Attribute Details
#colorspace ⇒ String (readonly)
234 |
# File 'lib/mini_magick/image.rb', line 234 attribute :colorspace |
#dimensions ⇒ Array<Integer> (readonly)
224 |
# File 'lib/mini_magick/image.rb', line 224 attribute :dimensions |
#exif ⇒ Hash (readonly)
238 |
# File 'lib/mini_magick/image.rb', line 238 attribute :exif |
#height ⇒ Integer (readonly)
220 |
# File 'lib/mini_magick/image.rb', line 220 attribute :height |
#mime_type ⇒ String (readonly)
212 |
# File 'lib/mini_magick/image.rb', line 212 attribute :mime_type |
#path ⇒ String (readonly)
Returns The location of the current working file.
135 136 137 |
# File 'lib/mini_magick/image.rb', line 135 def path @path end |
#resolution ⇒ Array<Integer> (readonly)
Returns the resolution of the photo. You can optionally specify the units measurement.
248 |
# File 'lib/mini_magick/image.rb', line 248 attribute :resolution |
#signature ⇒ String (readonly)
Returns the message digest of this image as a SHA-256, hexidecimal encoded string. This signature uniquely identifies the image and is convenient for determining if an image has been modified or whether two images are identical.
260 |
# File 'lib/mini_magick/image.rb', line 260 attribute :signature |
#size ⇒ Integer (readonly)
Returns the file size of the image.
230 |
# File 'lib/mini_magick/image.rb', line 230 attribute :size |
#type ⇒ String (readonly)
Returns the image format (e.g. “JPEG”, “GIF”).
208 |
# File 'lib/mini_magick/image.rb', line 208 attribute :type, "format" |
#width ⇒ Integer (readonly)
216 |
# File 'lib/mini_magick/image.rb', line 216 attribute :width |
Class Method Details
.attribute(name, key = name.to_s) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/mini_magick/image.rb', line 122 def self.attribute(name, key = name.to_s) define_method(name) do |*args| if args.any? && MiniMagick::Tool::Mogrify.instance_methods.include?(name) mogrify { |b| b.send(name, *args) } else @info[key, *args] end end end |
.create(ext = nil, validate = MiniMagick.validate_on_create) {|Tempfile| ... } ⇒ MiniMagick::Image
109 110 111 112 113 114 115 |
# File 'lib/mini_magick/image.rb', line 109 def self.create(ext = nil, validate = MiniMagick.validate_on_create, &block) tempfile = MiniMagick::Utilities.tempfile(ext.to_s.downcase, &block) new(tempfile.path, tempfile).tap do |image| image.validate! if validate end end |
.import_pixels(blob, columns, rows, depth, map, format = 'png') ⇒ MiniMagick::Image
Creates an image object from a binary string blob which contains raw pixel data (i.e. no header data).
Defaults to ‘png’.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mini_magick/image.rb', line 52 def self.import_pixels(blob, columns, rows, depth, map, format = 'png') # Create an image object with the raw pixel data string: create(".dat", false) { |f| f.write(blob) }.tap do |image| output_path = image.path.sub(/\.\w+$/, ".#{format}") # Use ImageMagick to convert the raw data file to an image file of the # desired format: MiniMagick::Tool::Convert.new do |convert| convert.size "#{columns}x#{rows}" convert.depth depth convert << "#{map}:#{image.path}" convert << output_path end image.path.replace output_path end end |
.open(path_or_url, ext = nil) ⇒ MiniMagick::Image
Opens a specific image file either on the local file system or at a URI. Use this if you don’t want to overwrite the image file.
Extension is either guessed from the path or you can specify it as a second parameter.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mini_magick/image.rb', line 81 def self.open(path_or_url, ext = nil) ext ||= if File.exist?(path_or_url) File.extname(path_or_url) else File.extname(URI(path_or_url).path) end Kernel.open(path_or_url, "rb") do |file| read(file, ext) end end |
.read(stream, ext = nil) ⇒ MiniMagick::Image
This is the primary loading method used by all of the other class methods.
Use this to pass in a stream object. Must respond to #read(size) or be a binary string object (BLOBBBB)
Probably easier to use the open method if you want to open a file or a URL.
29 30 31 32 33 34 35 |
# File 'lib/mini_magick/image.rb', line 29 def self.read(stream, ext = nil) if stream.is_a?(String) stream = StringIO.new(stream) end create(ext) { |file| IO.copy_stream(stream, file) } end |
Instance Method Details
#[](value) ⇒ String Also known as: info
Use this method if you want to access raw Identify’s format API.
273 274 275 |
# File 'lib/mini_magick/image.rb', line 273 def [](value) @info[value.to_s] end |
#collapse!(frame = 0) ⇒ self
Collapse images with sequences to the first frame (i.e. animated gifs) and preserve quality.
448 449 450 |
# File 'lib/mini_magick/image.rb', line 448 def collapse!(frame = 0) mogrify(frame) { |builder| builder.quality(100) } end |
#combine_options {|MiniMagick::Tool::Mogrify| ... } ⇒ self
You can use multiple commands together using this method. Very easy to use!
366 367 368 |
# File 'lib/mini_magick/image.rb', line 366 def (&block) mogrify(&block) end |
#composite(other_image, output_extension = 'jpg', mask = nil) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/mini_magick/image.rb', line 427 def composite(other_image, output_extension = 'jpg', mask = nil) output_tempfile = MiniMagick::Utilities.tempfile(".#{output_extension}") MiniMagick::Tool::Composite.new do |composite| yield composite if block_given? composite << other_image.path composite << path composite << mask.path if mask composite << output_tempfile.path end Image.new(output_tempfile.path, output_tempfile) end |
#destroy! ⇒ Object
Destroys the tempfile (created by open) if it exists.
455 456 457 |
# File 'lib/mini_magick/image.rb', line 455 def destroy! @tempfile.unlink if @tempfile end |
#eql?(other) ⇒ Boolean Also known as: ==
156 157 158 159 |
# File 'lib/mini_magick/image.rb', line 156 def eql?(other) self.class.equal?(other.class) && signature == other.signature end |
#format(format, page = 0) {|MiniMagick::Tool::Convert| ... } ⇒ self
This is used to change the format of the image. That is, from “tiff to jpg” or something like that. Once you run it, the instance is pointing to a new file with a new extension!
DANGER: This renames the file that the instance is pointing to. So, if you manually opened the file with Image.new(file_path)… Then that file is DELETED! If you used Image.open(file) then you are OK. The original file will still be there. But, any changes to it might not be…
Formatting an animation into a non-animated type will result in ImageMagick creating multiple pages (starting with 0). You can choose which page you want to manipulate. We default to the first page.
If you would like to convert between animated formats, pass nil as your page and ImageMagick will copy all of the pages.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/mini_magick/image.rb', line 323 def format(format, page = 0) @info.clear if @tempfile new_tempfile = MiniMagick::Utilities.tempfile(".#{format}") new_path = new_tempfile.path else new_path = path.sub(/\.\w+$/, ".#{format}") end MiniMagick::Tool::Convert.new do |convert| convert << (page ? "#{path}[#{page}]" : path) yield convert if block_given? convert << new_path end if @tempfile @tempfile.unlink @tempfile = new_tempfile else File.delete(path) unless path == new_path end path.replace new_path self end |
#hash ⇒ Object
162 163 164 |
# File 'lib/mini_magick/image.rb', line 162 def hash signature.hash end |
#identify {|MiniMagick::Tool::Identify| ... } ⇒ String
Runs ‘identify` on itself. Accepts an optional block for adding more options to `identify`.
471 472 473 474 475 476 |
# File 'lib/mini_magick/image.rb', line 471 def identify MiniMagick::Tool::Identify.new do |builder| yield builder if block_given? builder << path end end |
#layers ⇒ Array<MiniMagick::Image> Also known as: pages, frames
Returns layers of the image. For example, JPEGs are 1-layered, but formats like PSDs, GIFs and PDFs can have multiple layers/frames/pages.
289 290 291 292 293 294 |
# File 'lib/mini_magick/image.rb', line 289 def layers layers_count = identify.lines.count layers_count.times.map do |idx| MiniMagick::Image.new("#{path}[#{idx}]") end end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
387 388 389 |
# File 'lib/mini_magick/image.rb', line 387 def respond_to_missing?(method_name, include_private = false) MiniMagick::Tool::Mogrify.new.respond_to?(method_name, include_private) end |
#run_command(tool_name, *args) ⇒ Object
479 480 481 482 483 484 485 |
# File 'lib/mini_magick/image.rb', line 479 def run_command(tool_name, *args) MiniMagick::Tool.const_get(tool_name.capitalize).new do |builder| args.each do |arg| builder << arg end end end |
#to_blob ⇒ String
Returns raw image data.
171 172 173 |
# File 'lib/mini_magick/image.rb', line 171 def to_blob File.binread(path) end |
#valid? ⇒ Boolean
Checks to make sure that MiniMagick can read the file and understand it.
This uses the ‘identify’ command line utility to check the file. If you are having issues with this, then please work directly with the ‘identify’ command and see if you can figure out what the issue is.
184 185 186 187 188 189 |
# File 'lib/mini_magick/image.rb', line 184 def valid? validate! true rescue MiniMagick::Invalid false end |
#validate! ⇒ Object
Runs ‘identify` on the current image, and raises an error if it doesn’t pass.
197 198 199 200 201 |
# File 'lib/mini_magick/image.rb', line 197 def validate! identify rescue MiniMagick::Error => error raise MiniMagick::Invalid, error. end |
#write(output_to) ⇒ Object
Writes the temporary file out to either a file location (by passing in a String) or by passing in a Stream that you can #write(chunk) to repeatedly
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/mini_magick/image.rb', line 399 def write(output_to) case output_to when String, Pathname if layer? MiniMagick::Tool::Convert.new do |builder| builder << path builder << output_to end else FileUtils.copy_file path, output_to unless path == output_to.to_s end else IO.copy_stream File.open(path, "rb"), output_to end end |