Module: Gluttonberg::Library::QuickMagick

Defined in:
lib/gluttonberg/library/quick_magick.rb,
lib/gluttonberg/library/quick_magick.rb,
lib/gluttonberg/library/quick_magick/image.rb,
lib/gluttonberg/library/quick_magick/image_list.rb

Overview

Define quick magick error

Defined Under Namespace

Classes: Image, ImageList, QuickMagickError

Constant Summary collapse

PercentGeometry =

Normally the attributes are treated as pixels. Use this flag when the width and height attributes represent percentages. For example, 125x75 means 125% of the height and 75% of the width. The x and y attributes are not affected by this flag.

"%"
AspectGeometry =

Use this flag when you want to force the new image to have exactly the size specified by the the width and height attributes.

"!"
LessGeometry =

Use this flag when you want to change the size of the image only if both its width and height are smaller the values specified by those attributes. The image size is changed proportionally.

"<"
GreaterGeometry =

Use this flag when you want to change the size of the image if either its width and height exceed the values specified by those attributes. The image size is changed proportionally.

">"
AreaGeometry =

This flag is useful only with a single width attribute. When present, it means the width attribute represents the total area of the image in pixels.

"@"
MinimumGeometry =

Use ^ to set a minimum image size limit. The geometry 640x480^, for example, means the image width will not be less than 640 and the image height will not be less than 480 pixels after the resize. One of those dimensions will match the requested size, but the image will likely overflow the space requested to preserve its aspect ratio.

"^"
SolidColor =

Command for solid color

"xc"
LinearGradient =

Command for linear gradient

"gradient"
RadialGradient =

Command for radial gradient

"radial-gradient"
Patterns =

Different possible patterns

%w{bricks checkerboard circles crosshatch crosshatch30 crosshatch45 fishscales} +
(0..20).collect {|level| "gray#{level}" } +
%w{hexagons horizontal horizontalsaw hs_bdiagonal hs_cross hs_diagcross hs_fdiagonal hs_horizontal
hs_vertical left30 left45 leftshingle octagons right30 right45 rightshingle smallfishscales
vertical verticalbricks verticalleftshingle verticalrightshingle verticalsaw}

Class Method Summary collapse

Class Method Details

.escape_commandline(str) ⇒ Object Also known as: c

Escapes possible special chracters in command line by surrounding it with double quotes



157
158
159
# File 'lib/gluttonberg/library/quick_magick.rb', line 157

def escape_commandline(str)
  str =~ /^(\w|\s|\.)*$/ ? str : "\"#{str}\""
end

.exec3(command) ⇒ Object

Execute a command line and returns its results when it suceeds When the command fails, it throws QuickMagick::QuickMagickError



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/gluttonberg/library/quick_magick.rb', line 165

def exec3(command)
  error_file = Tempfile.new('error')
  error_filepath = error_file.path
  error_file.close
  result = `#{command} 2>"#{error_filepath}"`
  unless $?.success?
    error_message = <<-ERROR
      Error executing command: command
      Result is: #{result}
      Error is: #{File.read(error_filepath)}
    ERROR
    raise QuickMagick::QuickMagickError, error_message
  end
  result
end

.geometry(width, height = nil, x = nil, y = nil, flag = nil) ⇒ Object

Encodes a geometry string with the given options



74
75
76
77
78
79
80
81
82
# File 'lib/gluttonberg/library/quick_magick.rb', line 74

def geometry(width, height=nil, x=nil, y=nil, flag=nil)
  geometry_string = ""
  geometry_string << width.to_s if width
  geometry_string << 'x' << height.to_s if height
  geometry_string << '+' << x.to_s if x
  geometry_string << '+' << y.to_s if y
  geometry_string << flag if flag
  geometry_string
end

.graya_color(level, alpha = 255) ⇒ Object Also known as: gray_color

Returns a formatted string for a gray color with the given level and alpha. level and alpha could take one of the following values

  • an integer from 0 to 255

  • a float from 0.0 to 1.0

  • a string showing percentage from “0%” to “100%”



106
107
108
# File 'lib/gluttonberg/library/quick_magick.rb', line 106

def graya_color(level, alpha=255)
  rgba_color(level, level, level, alpha)
end

.hsla_color(hue, saturation, lightness, alpha = 1.0) ⇒ Object Also known as: hsl_color

HSL colors are encoding as a triple (hue, saturation, lightness). Hue is represented as an angle of the color circle (i.e. the rainbow represented in a circle). This angle is so typically measured in degrees that the unit is implicit in CSS; syntactically, only a number is given. By definition red=0=360, and the other colors are spread around the circle, so green=120, blue=240, etc. As an angle, it implicitly wraps around such that -120=240 and 480=120, for instance. (Students of trigonometry would say that “coterminal angles are equivalent” here; an angle (theta) can be standardized by computing the equivalent angle, (theta) mod 360.)

Saturation and lightness are represented as percentages. 100% is full saturation, and 0% is a shade of grey. 0% lightness is black, 100% lightness is white, and 50% lightness is ‘normal’.

Hue can take one of the following values:

  • an integer from 0…360 representing angle in degrees

  • a float value from 0…2*PI represeting angle in radians

saturation, lightness and alpha can take one of the following values:

  • an integer from 0 to 255

  • a float from 0.0 to 1.0

  • a string showing percentage from “0%” to “100%”



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gluttonberg/library/quick_magick.rb', line 133

def hsla_color(hue, saturation, lightness, alpha=1.0)
  components = [case hue
    when Integer then hue
    when Float then Integer(hue * 360 / 2 / Math::PI)
  end]
  components += [saturation, lightness].collect do |component|
    case component
      when Integer then (component * 100.0 / 255).round
      when Float then Integer(component*100)
      when String then Integer(component.sub('%', ''))
    end
  end
  components << case alpha
    when Integer then alpha * 100.0 / 255
    when Float then alpha
    when String then Float(alpha.sub('%', '')) / 100.0
  end
  "hsla(%d,%d%%,%d%%,%g)" % components
end

.random_string(length = 10) ⇒ Object

Generate a random string of specified length. Used to generate random names for temp files



68
69
70
71
# File 'lib/gluttonberg/library/quick_magick.rb', line 68

def random_string(length=10)
  @@CHARS ||= ("a".."z").to_a + ("1".."9").to_a 
  Array.new(length, '').collect{@@CHARS[rand(@@CHARS.size)]}.join
end

.rgba_color(red, green, blue, alpha = 255) ⇒ Object Also known as: rgb_color

Returns a formatted string for the color with the given components each component could take one of the following values

  • an integer from 0 to 255

  • a float from 0.0 to 1.0

  • a string showing percentage from “0%” to “100%”



89
90
91
92
93
94
95
96
97
# File 'lib/gluttonberg/library/quick_magick.rb', line 89

def rgba_color(red, green, blue, alpha=255)
  "#%02x%02x%02x%02x" % [red, green, blue, alpha].collect do |component|
    case component
      when Integer then component
      when Float then Integer(component*255)
      when String then Integer(component.sub('%', '')) * 255 / 100
    end
  end
end