Created
June 2, 2009 15:52
-
-
Save henrypoydar/122318 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AR model callback that uses paperclip and tempfile | |
def generate_previews | |
dimension_multiplier = case units | |
when 'inches'; 30 | |
else; 30 | |
end | |
png_width = (self.width * dimension_multiplier).to_i | |
png_height = (self.height * dimension_multiplier).to_i | |
tmp_svg = Tempfile.new('rsvg_svg', "#{RAILS_ROOT}/tmp") | |
tmp_png = Tempfile.new('rsvg_png', "#{RAILS_ROOT}/tmp") | |
tmp_svg.write self.svg | |
tmp_svg.flush | |
# rsvg svg to png conversion | |
system "#{RSVG_PATH} -d 72 -w #{png_width} -h #{png_height} #{tmp_svg.path} #{tmp_png.path}" | |
# Paperclip call | |
File.open(tmp_png.path) {|img| self.image = img} | |
tmp_svg.close! | |
tmp_png.close! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment