Skip to content

Instantly share code, notes, and snippets.

@takuya-i
Created September 7, 2012 01:54
Show Gist options
  • Save takuya-i/3662521 to your computer and use it in GitHub Desktop.
Save takuya-i/3662521 to your computer and use it in GitHub Desktop.
rails with Rmagick createing thumbnail image
#Create squre thumbnail from bytedata by RMagick
def thumbnail(size=128)
image = Magick::Image.from_blob(self.binary_contents).shift
image_width = image.columns
image_height = image.rows
if image_width > image_height
crap_size = image_width -image_height # 正方形にするため、カットするpixcel数
thumbnail = image.crop(crap_size /2, 0, image_width - crap_size , image_height).resize(size, size)
elsif(image_height > image_width)
crap_size = image_height - image_width
thumbnail = image.crop(0, crap_size /2, image_width , image_height - crap_size).resize(size, size)
else
thumbnail = image.resize(size, size)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment