Class: Amazon::Hacks::Image
- Inherits:
-
Object
- Object
- Amazon::Hacks::Image
- Includes:
- Countries
- Defined in:
- lib/amazon-hacks.rb
Overview
Class for manipulating Amazon’s image URLs. These images are generated on demand at Amazon, so please don’t abuse this feature against their site (save the image locally or link to them so they can get some sales out of it). I also do not have transformations that apply Amazon-specific decorations like “20% off” badges or “Look Inside” to hinder spoofing an Amazon site somewhat.
Amazon images require an ASIN and country. These can either be specified manually or imported from a URL. In addition, each image can be modified with zero or more transformations (all specified in the URL), although the using more than two transformations is not necessarily guaranteed to be honored by Amazon’s rendering engine (only the first will generally be applied, but [size + transform] seems to work usually).
Abusing Amazon Images, a site to whom I am most indebted here, notes the following about transform chaining:
“The cool thing (if you want to generate unlikely Amazon images) is that you’re not limited to one use of any of these commands. You can have multiple discounts, multiple shadows, multiple bullets, generating images that Amazon would never have on its site. However, every additional command you add generates another 10% to the image dimensions, adding white space around the image. And that 10% compounds; add a lot of bullets, and you’ll find that you have a small image in a large blank space. (You can use the #crop! command to cut away the excess, however.) Note also that the commands are interpreted in order, which can have an impact on what overlaps what.”
Constant Summary collapse
- SIZES =
{ :small => "SCTZZZZZZZ", :medium => "SCMZZZZZZZ", :large => "SCLZZZZZZZ" }
- SHADOW_POSITIONS =
[ :left, :right, :custom ]
- SCALE_TYPES =
{ :proportion => "AA", :square => "SS", :width => "SX", :height => "SY" }
- TEXT_FONTS =
[ :times, :arial, :arialbi, :verdanab, :advc128d ]
- TEXT_ALIGNS =
[ :left, :center ]
Instance Attribute Summary collapse
-
#asin ⇒ Object
readonly
Returns the value of attribute asin.
Class Method Summary collapse
-
.build_from_url(url) ⇒ Object
Creates an Image object from a
url
.
Instance Method Summary collapse
-
#add_border!(width, html_color = "\#000000") ⇒ Object
Add a border around the image.
-
#add_custom_shadow!(size, xoff, yoff, fuzz) ⇒ Object
Add a custom shadow to the image.
-
#add_shadow!(side) ⇒ Object
Add a simple shadow to the left or right; takes arguments :left or :right.
-
#add_text!(text, xoff, yoff, width, height, font, size, html_color, align = :left) ⇒ Object
Overlay text on the image.
-
#blur!(radius) ⇒ Object
Blur the image.
-
#crop!(xoff, yoff, width, height) ⇒ Object
Crops the image.
-
#highlight!(xoff, yoff, width, height, intensity) ⇒ Object
Highlight the image.
-
#initialize(asin, country) ⇒ Image
constructor
Initializes an Image object, requires both a valid asin as well as a country symbol (eg,
:us
or:uk
). -
#reset_transforms! ⇒ Object
Transformations like #crop! and #add_shadow!, etc.
-
#scale!(type, size) ⇒ Object
Scale the image to exact dimensions.
-
#set_size!(size) ⇒ Object
Add a size transform to the image.
-
#sharpen!(percent) ⇒ Object
Sharpen the image.
-
#tilt!(degrees) ⇒ Object
Rotate the image.
-
#url ⇒ Object
(also: #to_s)
Returns a URL for the image from Amazon’s servers.
Methods included from Countries
#country_host, #country_imgcode, #country_imghost
Constructor Details
#initialize(asin, country) ⇒ Image
Initializes an Image object, requires both a valid asin as well as a country symbol (eg, :us
or :uk
)
244 245 246 247 248 |
# File 'lib/amazon-hacks.rb', line 244 def initialize(asin, country) @asin = asin @country = country reset_transforms! end |
Instance Attribute Details
#asin ⇒ Object (readonly)
Returns the value of attribute asin.
218 219 220 |
# File 'lib/amazon-hacks.rb', line 218 def asin @asin end |
Class Method Details
Instance Method Details
#add_border!(width, html_color = "\#000000") ⇒ Object
Add a border around the image. Takes the following arguments
-
width
- size of the border (pixels) -
color
- color of the border in HTML format (eg, “#CCCCCC”) [defaults black if not specified]
296 297 298 299 300 301 302 |
# File 'lib/amazon-hacks.rb', line 296 def add_border! (width, html_color="\#000000") color = Color::RGB.from_html(html_color) cmd = "BO#{width}," cmd << [color.r, color.g, color.b].map {|c| (c*255).to_i }.join(",") @transforms << cmd self end |
#add_custom_shadow!(size, xoff, yoff, fuzz) ⇒ Object
Add a custom shadow to the image. Takes 4 parameters:
-
size
- padding around the image (pixels) -
xoff
- distance from the shadow’s edge to the item’s edge horizontally. Positive values go to the right, negative to the left (pixels) -
yoff
- like xoff but vertically. -
fuzz
- fuzziness of shadow; 0 is perfectly square while higher values blur the sharpness of the shadow’s edge
285 286 287 288 |
# File 'lib/amazon-hacks.rb', line 285 def add_custom_shadow! (size, xoff, yoff, fuzz) @transforms << "PA#{size},#{xoff},#{yoff},#{fuzz}" self end |
#add_shadow!(side) ⇒ Object
Add a simple shadow to the left or right; takes arguments :left or :right.
268 269 270 271 272 273 274 |
# File 'lib/amazon-hacks.rb', line 268 def add_shadow! (side) case when side == :left then @transforms << "PB" when side == :right then @transforms << "PC" end self end |
#add_text!(text, xoff, yoff, width, height, font, size, html_color, align = :left) ⇒ Object
Overlay text on the image. This is easily the most complicated transformation you can apply in terms of sheer argument heft. The text is placed in a square area you must set the dimensions of first. Takes the following options:
-
text
- the text you want to overlay -
xoff
- x offset from the top left corner to tlc of box (pixels) -
yoff
- y offset from the top left corner to tlc of box (pixels) -
width
- width of the text box -
height
- height of the text box in pixels -
font
- four options are allowed:-
:times - Times Roman
-
:arial - Arial
-
:arialbi - Arial Bold Italic
-
:verdanab - Verdana Bold
-
:advc128d - barcode font
-
-
size
- font size (points?) -
color
- a color specified in HTML style (eg, “#CCEECC”) -
align
- may be :left or :center only, defaults to :left
384 385 386 387 388 389 390 391 392 |
# File 'lib/amazon-hacks.rb', line 384 def add_text! (text, xoff, yoff, width, height, font, size, html_color, align=:left) color = Color::RGB.from_html(html_color) cmd = (align == :center) ? "ZC" : "ZA" parts = [URI.escape(text), xoff, yoff, width, height, font, size].map {|x| x.to_s} + [color.r, color.g, color.b].map {|c| (c*255).to_i } cmd << parts.join(",") @transforms << cmd self end |
#blur!(radius) ⇒ Object
Blur the image. The input argument seems to represent a radius, but the essential thing to know is the higher the value, the blurrier it gets.
322 323 324 325 |
# File 'lib/amazon-hacks.rb', line 322 def blur! (radius) @transforms << "BL#{radius}" self end |
#crop!(xoff, yoff, width, height) ⇒ Object
Crops the image. This can be used to generate Flickr-like square windows into the image (although there is no method to figure out programmatically the dimensions of a particular size, so this might be hard [maybe scale then crop?]). Takes four arguments to set the crop square:
-
xoff
- x offset from top left corner of image to tlc of box (pixels) -
yoff
- y offset from top left corner of image to tlc of (pixels) -
width
- width of crop square -
height
- height of crop square
360 361 362 363 |
# File 'lib/amazon-hacks.rb', line 360 def crop! (xoff, yoff, width, height) @transforms << "CR#{xoff},#{yoff},#{width},#{height}" self end |
#highlight!(xoff, yoff, width, height, intensity) ⇒ Object
Highlight the image. I’m still not sure what this means, but here it is. You specify an square highlight area of the image with the following parameters:
-
xoff
- x offset from the top left corner to tlc of box (pixels) -
yoff
- y offset from the top left corner to tlc of box (pixels) -
width
- width of the text box -
height
- height of the text box in pixels -
intensity
- I really don’t know what this means, but more is bigger
404 405 406 407 |
# File 'lib/amazon-hacks.rb', line 404 def highlight!(xoff, yoff, width, height, intensity) @transforms << "HL#{xoff},#{yoff},#{width},#{height},#{intensity}" self end |
#reset_transforms! ⇒ Object
Transformations like #crop! and #add_shadow!, etc. can be chained and are potentially applied in the order specified. Should you need to clear the transforms and revert to the original image, this command does that.
413 414 415 416 |
# File 'lib/amazon-hacks.rb', line 413 def reset_transforms! @transforms = [] self end |
#scale!(type, size) ⇒ Object
Scale the image to exact dimensions. Takes a scaling type and dimension in pixels. There are four types of scaling possible:
-
:proportion
- preserve the image’s proportions -
:square
- make the image square -
:width
- the size represents the width of image. Height is proportionally scaled. -
:height
- the size represents the height of the image. Width is proportionally scaled.
345 346 347 348 |
# File 'lib/amazon-hacks.rb', line 345 def scale!(type, size) @transforms << "#{SCALE_TYPES[type]}#{size}" self end |
#set_size!(size) ⇒ Object
Add a size transform to the image. The following sizes are allowed:
-
small
-
medium
-
large
These sizes are not guaranteed to particular dimensions. If you need exact sizing, use the #scale! transformation. These sizes are a lot friendlier to Amazon’s image servers though, so I would recommend them if you can handle some variation.
260 261 262 263 |
# File 'lib/amazon-hacks.rb', line 260 def set_size!(size) @transforms << SIZES[size] self end |
#sharpen!(percent) ⇒ Object
Sharpen the image. The value can range from 0 - 99. Larger values may affect color and increase noise significantly.
330 331 332 333 |
# File 'lib/amazon-hacks.rb', line 330 def sharpen! (percent) @transforms << "SH#{percent}" self end |
#tilt!(degrees) ⇒ Object
Rotate the image. Takes a single argument of degrees to rotate. Positive values rotate to the right, negative to the left.
According to auugh: “The values seem to range as high as 135. Positive values rotate to the right, negative numbers rotate to the left. Negative numbers go… well, I stopped testing in the hundreds of millions, but there’s not much need to go beyond -360, which takes it full circle. Positive rotations in some values seem to cause severe image shrinkage, so you should consider using a size adjustment after rotation.”
314 315 316 317 |
# File 'lib/amazon-hacks.rb', line 314 def tilt! (degrees) @transforms << "PT#{degrees}" self end |
#url ⇒ Object Also known as: to_s
Returns a URL for the image from Amazon’s servers.
420 421 422 423 424 425 |
# File 'lib/amazon-hacks.rb', line 420 def url url = "http://#{country_imghost}/images/P/#{asin}" url << "." << country_imgcode << "." url << "_" << @transforms.join("_") << "_" << ".jpg" end |