Class: Magick::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/magick/image.rb

Overview

Output example: file.ext JPEG 1920x1200 1920x1200+0+0 8-bit DirectClass 1.955MB 0.000u 0:00.000 Error example: identify: no decode delegate for this image format ‘file.ext’ @ error/constitute.c/ReadImage/532.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Image

Returns a new instance of Image.

Raises:

  • (Errno::ENOENT)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/magick/image.rb', line 9

def initialize(path)
  raise Errno::ENOENT, "the file '#{path}' does not exist" unless File.exists?(path)
  
  @path = escape(path)
  @size = File.size(path)
  
  stdin, stdout, stderr = Open3.popen3("identify '#{path}'") # Output will land in stderr

  out = stdout.read
  err = stderr.read
  
  @valid = out.length > 0
  
  if valid?
    filename, @codec, resolution, etc = out.split
    
    @width = resolution.split("x").first.to_i
    @height = resolution.split("x").last.to_i
  end
end

Instance Attribute Details

#codecObject (readonly)

Returns the value of attribute codec.



7
8
9
# File 'lib/magick/image.rb', line 7

def codec
  @codec
end

#heightObject (readonly)

Returns the value of attribute height.



7
8
9
# File 'lib/magick/image.rb', line 7

def height
  @height
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/magick/image.rb', line 7

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



7
8
9
# File 'lib/magick/image.rb', line 7

def size
  @size
end

#widthObject (readonly)

Returns the value of attribute width.



7
8
9
# File 'lib/magick/image.rb', line 7

def width
  @width
end

Instance Method Details

#transcode(output_file, parameters = "") ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/magick/image.rb', line 34

def transcode(output_file, parameters = "")
  stdin, stdout, stderr = Open3.popen3("convert #{path} #{escape(parameters)} #{output_file}")
  
  out = stdout.read
  err = stderr.read
  
  raise err if err.length > 0
  
  self.class.new(output_file)
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/magick/image.rb', line 30

def valid?
  @valid
end