Class: MiniMagick::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_magick/image.rb,
lib/mini_magick/image/info.rb

Defined Under Namespace

Classes: Info

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_path, tempfile = nil) {|MiniMagick::Tool| ... } ⇒ 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.

Parameters:

  • input_path (String, Pathname)

    The location of an image file

Yields:



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.to_s
  @tempfile = tempfile
  @info = MiniMagick::Image::Info.new(@path)

  combine_options(&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.



462
463
464
465
466
# File 'lib/mini_magick/image.rb', line 462

def method_missing(name, *args)
  mogrify do |builder|
    builder.send(name, *args)
  end
end

Instance Attribute Details

#colorspaceString (readonly)

Returns:

  • (String)


235
# File 'lib/mini_magick/image.rb', line 235

attribute :colorspace

#dataHash (readonly)

Returns the result of converting the image to JSON format.

Returns:

  • (Hash)


266
# File 'lib/mini_magick/image.rb', line 266

attribute :data

#dimensionsArray<Integer> (readonly)

Returns:

  • (Array<Integer>)


219
# File 'lib/mini_magick/image.rb', line 219

attribute :dimensions

#exifHash (readonly)

Returns:

  • (Hash)


239
# File 'lib/mini_magick/image.rb', line 239

attribute :exif

#heightInteger (readonly)

Returns:

  • (Integer)


215
# File 'lib/mini_magick/image.rb', line 215

attribute :height

#human_sizeString (readonly)

Returns the file size in a human readable format.

Returns:

  • (String)


231
# File 'lib/mini_magick/image.rb', line 231

attribute :human_size

#pathString (readonly)

Returns The location of the current working file.

Returns:

  • (String)

    The location of the current working file



131
132
133
# File 'lib/mini_magick/image.rb', line 131

def path
  @path
end

#resolutionArray<Integer> (readonly)

Returns the resolution of the photo. You can optionally specify the units measurement.

Examples:

image.resolution("PixelsPerInch") #=> [250, 250]

Returns:

  • (Array<Integer>)

See Also:



249
# File 'lib/mini_magick/image.rb', line 249

attribute :resolution

#signatureString (readonly)

Returns the message digest of this image as a SHA-256, hexadecimal 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.

Examples:

image.signature #=> "60a7848c4ca6e36b8e2c5dea632ecdc29e9637791d2c59ebf7a54c0c6a74ef7e"

Returns:

  • (String)

See Also:



261
# File 'lib/mini_magick/image.rb', line 261

attribute :signature

#sizeInteger (readonly)

Returns the file size of the image (in bytes).

Returns:

  • (Integer)


225
# File 'lib/mini_magick/image.rb', line 225

attribute :size

#tempfileTempfile (readonly)

Returns The underlying temporary file.

Returns:

  • (Tempfile)

    The underlying temporary file



135
136
137
# File 'lib/mini_magick/image.rb', line 135

def tempfile
  @tempfile
end

#typeString (readonly)

Returns the image format (e.g. “JPEG”, “GIF”).

Returns:

  • (String)


207
# File 'lib/mini_magick/image.rb', line 207

attribute :type, "format"

#widthInteger (readonly)

Returns:

  • (Integer)


211
# File 'lib/mini_magick/image.rb', line 211

attribute :width

Class Method Details

.attribute(name, key = name.to_s) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/mini_magick/image.rb', line 118

def self.attribute(name, key = name.to_s)
  define_method(name) do |*args|
    if args.any? && name != :resolution
      mogrify { |b| b.send(name, *args) }
    else
      @info[key, *args]
    end
  end
end

.create(ext = nil) {|Tempfile| ... } ⇒ MiniMagick::Image

Used to create a new Image object data-copy. Not used to “paint” or that kind of thing.

Takes an extension in a block and can be used to build a new Image object. Used by both open and read to create a new object. Ensures we have a good tempfile.

Parameters:

  • ext (String) (defaults to: nil)

    Specify the extension you want to read it as

Yields:

  • (Tempfile)

    You can #write bits to this object to create the new Image

Returns:



107
108
109
110
111
# File 'lib/mini_magick/image.rb', line 107

def self.create(ext = nil, &block)
  tempfile = MiniMagick::Utilities.tempfile(ext.to_s.downcase, &block)

  new(tempfile.path, tempfile)
end

.get_image_from_pixels(pixels, dimension, map, depth, format) ⇒ Object

This is used to create image from pixels. This might be required if you create pixels for some image processing reasons and you want to form image from those pixels.

DANGER: This operation can be very expensive. Please try to use with caution.

Examples:

# It is given in readme.md file


367
368
369
370
371
# File 'lib/mini_magick/image.rb', line 367

def self.get_image_from_pixels(pixels, dimension, map, depth, format)
  pixels = pixels.flatten
  blob = pixels.pack('C*')
  import_pixels(blob, *dimension, depth, map, format)
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’.

Parameters:

  • blob (String)

    Binary string blob containing raw pixel data.

  • columns (Integer)

    Number of columns.

  • rows (Integer)

    Number of rows.

  • depth (Integer)

    Bit depth of the encoded pixel data.

  • map (String)

    A code for the mapping of the pixel data. Example: ‘gray’ or ‘rgb’.

  • format (String) (defaults to: 'png')

    The file extension of the image format to be used when creating the image object.

Returns:



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:
  read(blob, ".dat").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.convert 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, **options) ⇒ 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.

Parameters:

  • path_or_url (String)

    Either a local file path or a URL that open-uri can read

  • ext (String) (defaults to: nil)

    Specify the extension you want to read it as

  • options (Hash)

    Specify options for the open method

Returns:



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mini_magick/image.rb', line 82

def self.open(path_or_url, ext = nil, **options)
  if path_or_url.to_s =~ %r{\A(https?|ftp)://}
    uri = URI(path_or_url)
    ext ||= File.extname(uri.path).sub(/:.*/, '') # handle URL including a colon
    uri.open(options) { |file| read(file, ext) }
  else
    pathname = Pathname(path_or_url)
    ext ||= File.extname(pathname.to_s)
    pathname.open(binmode: true, **options) { |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 (BLOB)

Probably easier to use the open method if you want to open a file or a URL.

Parameters:

  • stream (#read, String)

    Some kind of stream object that needs to be read or is a binary String blob

  • ext (String) (defaults to: nil)

    A manual extension to use for reading the file. Not required, but if you are having issues, give this a try.

Returns:



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

#==(other) ⇒ Object Also known as: eql?



156
157
158
# File 'lib/mini_magick/image.rb', line 156

def ==(other)
  self.class == other.class && signature == other.signature
end

#[](value) ⇒ String Also known as: info

Use this method if you want to access raw Identify’s format API.

Examples:

image["%w %h"]       #=> "250 450"
image["%r"]          #=> "DirectClass sRGB"

Parameters:

  • value (String)

Returns:

  • (String)

See Also:



279
280
281
# File 'lib/mini_magick/image.rb', line 279

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.

Parameters:

  • frame (Integer) (defaults to: 0)

    The frame to which to collapse to, defaults to ‘0`.

Returns:

  • (self)


532
533
534
# File 'lib/mini_magick/image.rb', line 532

def collapse!(frame = 0)
  mogrify(frame) { |builder| builder.quality(100) }
end

#combine_options {|MiniMagick::Command| ... } ⇒ self

You can use multiple commands together using this method. Very easy to use!

Examples:

image.combine_options do |c|
  c.draw "image Over 0,0 10,10 '#{MINUS_IMAGE_PATH}'"
  c.thumbnail "300x500>"
  c.background "blue"
end

Yields:

  • (MiniMagick::Command)

Returns:

  • (self)

See Also:



451
452
453
# File 'lib/mini_magick/image.rb', line 451

def combine_options(&block)
  mogrify(&block)
end

#composite(other_image, output_extension = type.downcase, mask = nil) ⇒ Object

Examples:

first_image = MiniMagick::Image.open "first.jpg"
second_image = MiniMagick::Image.open "second.jpg"
result = first_image.composite(second_image) do |c|
  c.compose "Over" # OverCompositeOp
  c.geometry "+20+20" # copy second_image onto first_image from (20, 20)
end
result.write "output.jpg"

See Also:



511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/mini_magick/image.rb', line 511

def composite(other_image, output_extension = type.downcase, mask = nil)
  output_tempfile = MiniMagick::Utilities.tempfile(".#{output_extension}")

  MiniMagick.composite 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.



539
540
541
542
543
544
# File 'lib/mini_magick/image.rb', line 539

def destroy!
  if @tempfile
    FileUtils.rm_f @tempfile.path.sub(/mpc$/, "cache") if @tempfile.path.end_with?(".mpc")
    @tempfile.unlink
  end
end

#format(format, page = 0, read_opts = {}) {|MiniMagick::Tool| ... } ⇒ 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.

Parameters:

  • format (String)

    The target format… Like ‘jpg’, ‘gif’, ‘tiff’ etc.

  • page (Integer) (defaults to: 0)

    If this is an animated gif, say which ‘page’ you want with an integer. Default 0 will convert only the first page; ‘nil’ will convert all pages.

  • read_opts (Hash) (defaults to: {})

    Any read options to be passed to ImageMagick for example: image.format(‘jpg’, page, ‘300’)

Yields:

  • (MiniMagick::Tool)

    It optionally yields the command, if you want to add something.

Returns:

  • (self)


400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/mini_magick/image.rb', line 400

def format(format, page = 0, read_opts={})
  if @tempfile
    new_tempfile = MiniMagick::Utilities.tempfile(".#{format}")
    new_path = new_tempfile.path
  else
    new_path = Pathname(path).sub_ext(".#{format}").to_s
  end

  input_path = path.dup
  input_path << "[#{page}]" if page && !layer?

  MiniMagick.convert do |convert|
    read_opts.each do |opt, val|
      convert.send(opt.to_s, val)
    end
    convert << input_path
    yield convert if block_given?
    convert << new_path
  end

  if @tempfile
    destroy!
    @tempfile = new_tempfile
  else
    File.delete(path) unless path == new_path || layer?
  end

  path.replace new_path
  @info.clear

  self
rescue MiniMagick::Invalid, MiniMagick::Error => e
  new_tempfile.unlink if new_tempfile && @tempfile != new_tempfile
  raise e
end

#get_pixels(map = "RGB") ⇒ Array

Returns a matrix of pixels from the image. The matrix is constructed as an array (1) of arrays (2) of arrays (3) of unsigned integers:

1) one for each row of pixels 2) one for each column of pixels 3) three or four elements in the range 0-255, one for each of the RGB(A) color channels

It can also be called after applying transformations:

In this example, all pixels in pix should now have equal R, G, and B values.

Examples:

img = MiniMagick::Image.open 'image.jpg'
pixels = img.get_pixels
pixels[3][2][1] # the green channel value from the 4th-row, 3rd-column pixel
img = MiniMagick::Image.open 'image.jpg'
pixels = img.get_pixels("RGBA")
pixels[3][2][3] # the alpha channel value from the 4th-row, 3rd-column pixel
img = MiniMagick::Image.open 'image.jpg'
img.crop '20x30+10+5'
img.colorspace 'Gray'
pixels = img.get_pixels

Parameters:

  • map (String) (defaults to: "RGB")

    A code for the mapping of the pixel data. Must be either ‘RGB’ or ‘RGBA’. Default to ‘RGB’

Returns:

  • (Array)

    Matrix of each color of each pixel

Raises:

  • (ArgumentError)


335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/mini_magick/image.rb', line 335

def get_pixels(map="RGB")
  raise ArgumentError, "Invalid map value" unless ["RGB", "RGBA"].include?(map)
  convert = MiniMagick.convert
  convert << path
  convert.depth(8)
  convert << "#{map}:-"

  # Do not use `convert.call` here. We need the whole binary (unstripped) output here.
  shell = MiniMagick::Shell.new
  output, * = shell.run(convert.command)

  pixels_array = output.unpack("C*")
  pixels = pixels_array.each_slice(map.length).each_slice(width).to_a

  # deallocate large intermediary objects
  output.clear
  pixels_array.clear

  pixels
end

#hashObject



161
162
163
# File 'lib/mini_magick/image.rb', line 161

def hash
  signature.hash
end

#identify {|MiniMagick::Tool| ... } ⇒ String

Runs ‘identify` on itself. Accepts an optional block for adding more options to `identify`.

Examples:

image = MiniMagick::Image.open("image.jpg")
image.identify do |b|
  b.verbose
end # runs `identify -verbose image.jpg`

Yields:

Returns:

  • (String)

    Output from ‘identify`



558
559
560
561
562
563
# File 'lib/mini_magick/image.rb', line 558

def identify
  MiniMagick.identify do |builder|
    yield builder if block_given?
    builder << path
  end
end

#landscape?Boolean

Compares if image width is greater than height

| | | |

Returns:

  • (Boolean)


591
592
593
# File 'lib/mini_magick/image.rb', line 591

def landscape?
  width > height
end

#layer?Boolean

Returns:

  • (Boolean)


579
580
581
# File 'lib/mini_magick/image.rb', line 579

def layer?
  path =~ /\[\d+\]$/
end

#layersArray<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.

Examples:

image = MiniMagick::Image.new("document.pdf")
image.pages.each_with_index do |page, idx|
  page.write("page#{idx}.pdf")
end

Returns:



295
296
297
298
299
300
# File 'lib/mini_magick/image.rb', line 295

def layers
  layers_count = identify.lines.count
  layers_count.times.map do |idx|
    MiniMagick::Image.new("#{path}[#{idx}]")
  end
end

#mogrify(page = nil) ⇒ Object



565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/mini_magick/image.rb', line 565

def mogrify(page = nil)
  MiniMagick.mogrify do |builder|
    yield builder if block_given?
    if builder.args.include?("-format")
      fail MiniMagick::Error, "you must call #format on a MiniMagick::Image directly"
    end
    builder << (page ? "#{path}[#{page}]" : path)
  end

  @info.clear

  self
end

#portrait?Boolean

Compares if image height is greater than width

| | | | | | | |

Returns:

  • (Boolean)


605
606
607
# File 'lib/mini_magick/image.rb', line 605

def portrait?
  height > width
end

#respond_to_missing?(name, include_all) ⇒ Boolean

Prevents ruby from calling ‘#to_ary` on the image when checking if it’s a splattable data structure in certain cases.

Returns:

  • (Boolean)


471
472
473
# File 'lib/mini_magick/image.rb', line 471

def respond_to_missing?(name, include_all)
  false
end

#to_blobString

Returns raw image data.

Returns:

  • (String)

    Binary string



170
171
172
# File 'lib/mini_magick/image.rb', line 170

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.

Returns:

  • (Boolean)


183
184
185
186
187
188
# File 'lib/mini_magick/image.rb', line 183

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.



196
197
198
199
200
# File 'lib/mini_magick/image.rb', line 196

def validate!
  identify
rescue MiniMagick::Error => error
  raise MiniMagick::Invalid, error.message
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

Parameters:

  • output_to (String, Pathname, #read)

    Some kind of stream object that needs to be read or a file path as a String



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/mini_magick/image.rb', line 483

def write(output_to)
  case output_to
  when String, Pathname
    if layer?
      MiniMagick.convert 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