Ruby and RMagick - Quote with Polaroid-like effect

To create Polaroid-like effect you just need the RMagick gem and a couple of 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 mentioned effects 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
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu