Module: Imagery::GM

Defined in:
lib/imagery.rb

Constant Summary collapse

CONVERT =

-size tells GM to only read from a given dimension. -resize is the target dimension, and understands geometry strings. -quality we force it to 80, which is very reasonable and practical.

"gm convert -size '%s' '%s' -resize '%s' %s -quality 80 '%s'"
IDENTIFY =

2 is the file descriptor for stderr, which ‘gm identify` happily chucks out information to, regardless if the image was identified or not.

We utilize the fact that gm identify exits with a status of 1 if it fails to identify the image.

See Also:

  • an explanation of file descriptions and redirection. http://stackoverflow.com/questions/818255/in-the-bash-shell-what-is-21
"gm identify '%s' 2> /dev/null"

Class Method Summary collapse

Class Method Details

.convert(src, dst, resize, extent = nil) ⇒ Object



143
144
145
# File 'lib/imagery.rb', line 143

def self.convert(src, dst, resize, extent = nil)
  system(sprintf(CONVERT, dim(resize), src, resize, extent(extent), dst))
end

.dim(dim) ⇒ Object

Return the cleaned dimension representation minus the geometry directives.



165
166
167
# File 'lib/imagery.rb', line 165

def self.dim(dim)
  dim.gsub(/\^><!/, "")
end

.extent(dim) ⇒ Object

Cropping and all that nice presentation kung-fu.



172
173
174
175
176
# File 'lib/imagery.rb', line 172

def self.extent(dim)
  if dim
    "-background black -compose Copy -gravity center -extent '#{dim}'"
  end
end

.identify(io) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/imagery.rb', line 147

def self.identify(io)
  file = Tempfile.new("imagery")
  file.write(io.read)
  file.close

  `gm identify #{io.path} 2> /dev/null`

  return $?.success?
ensure
  # Very important, else `io.read` will return "".
  io.rewind

  # Tempfile quickly runs out of names, so best to avoid that.
  file.unlink
end