Archiwum

Posty oznaczone ‘Ruby’

Easy way to test your regular expressions

Kwiecień 25th, 2011 Brak komentarzy

Just visit Rubular a Ruby regular expression editor.

Tagi:,

Ruby and RMagick – Quote with Polaroid-like effect

Grudzień 3rd, 2010 Brak komentarzy

To create quote like on picture below you just need RMagick gem and two small functions.

Function that will return random number in range <-max, max> so image can be rotated in both ways.

# Custom random function, range <-max..max>
def crand max
  if rand(2) == 0 then
    mod = -1
  else
    mod = 1
  end
  (rand(max)+1)*mod
end

Function that will add border, wave, shadow and randomly rotate everything.
It expects Image object and can be used to add effect to any image.

# Slightly modified Polaroid effect
# Source: http://rmagick.rubyforge.org/Polaroid/polaroid.html
def create_card_effect image
  image.border!(18, 18, "#fff")
  image.border!(1,1,"#ddd")
 
  image.background_color = "none"
 
  amplitude = image.columns * 0.01 
  wavelength = image.rows  * (rand(5)+5)
 
  image.rotate!(90)
  image = image.wave(amplitude, wavelength)
  image.rotate!(-90)
 
  shadow = image.flop
  shadow = shadow.colorize(1, 1, 1, "gray75") 
  shadow.background_color = "white" 
  shadow.border!(10, 10, "white")
  shadow = shadow.blur_image(0, 3)
 
  image = shadow.composite(image, -amplitude/2, 5, Magick::OverCompositeOp)
 
  image.rotate!(crand(10))
  image.trim!
 
  image
end

You can see how this is exactly done in quotes model by checking app/models/quote.rb file in Quotes app.