Class: Magick::Image

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Image

Returns a new instance of Image.

Raises:

  • (Errno::ENOENT)


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

def initialize(path)
  raise Errno::ENOENT, "the file '#{path}' does not exist" unless File.exists?(path)
  
  @path = path
  @size = File.size(path)
  
  out = Open3.popen3("identify #{Shellwords.escape(path)}") { |stdin, stdout, stderr| stdout.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.



9
10
11
# File 'lib/magick/image.rb', line 9

def codec
  @codec
end

#heightObject (readonly)

Returns the value of attribute height.



9
10
11
# File 'lib/magick/image.rb', line 9

def height
  @height
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/magick/image.rb', line 9

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'lib/magick/image.rb', line 9

def size
  @size
end

#widthObject (readonly)

Returns the value of attribute width.



9
10
11
# File 'lib/magick/image.rb', line 9

def width
  @width
end

Instance Method Details

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

Raises:



32
33
34
35
36
37
38
# File 'lib/magick/image.rb', line 32

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

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/magick/image.rb', line 28

def valid?
  @valid
end