Skip to content

Instantly share code, notes, and snippets.

@Godoy
Created July 25, 2016 20:20
Show Gist options
  • Save Godoy/d0585a4fec44cb940d957c4d9650b01e to your computer and use it in GitHub Desktop.
Save Godoy/d0585a4fec44cb940d957c4d9650b01e to your computer and use it in GitHub Desktop.
Example with image magick - load font, background, caption
def image_generate
# composite compartilhar.png ab.png abc.png
# convert a.png b.png -append ab.png
# composite -font Raleway-Regular.ttf -size 380x200 -gravity east label:Joaquim abc.png abcd.png
file = Tempfile.new(['image1', '.png'])
file.binmode
fileAux = Tempfile.new(['image2', '.png'])
fileAux.binmode
begin
refs = []
image_top = Rails.root + self.clothing_top.image.path
refs << self.clothing_top.name
image_bottom = Rails.root + self.clothing_bottom.image.path if self.clothing_bottom.present?
refs << self.clothing_bottom.name if self.clothing_bottom.present?
image_extra = Rails.root + self.clothing_extra.image.path if self.clothing_extra.present?
refs << self.clothing_extra.name if self.clothing_extra.present?
#top e bottom
Paperclip.run("convert", " -gravity Center #{image_top} #{image_bottom} -append :dest",
:dest => File.expand_path(file.path) )
#floating extra image
if image_extra.present?
Paperclip.run("composite", " -gravity North #{image_extra} :dest :dest",
:dest => File.expand_path(file.path) )
end
#background image
Paperclip.run("composite", " -gravity Center -geometry +115+0 :dest #{Rails.root}/public/bg-img-share.jpg :dest",
:dest => File.expand_path(file.path) )
#references
Paperclip.run("convert", " -background '#e7e7e7' -fill black -border 20x10 -bordercolor '#e7e7e7' -font #{Rails.root}/public/fonts/Raleway-Regular.ttf -size 380x30 -gravity west caption:'ref: #{refs.join(" / ref: ")}' :dest",
:dest => File.expand_path(fileAux.path) )
Paperclip.run("composite", " -geometry +50+40 -gravity southwest :dest_aux :dest :dest",
:dest => File.expand_path(file.path), :dest_aux => File.expand_path(fileAux.path) )
if self.name.present?
Paperclip.run("convert", " -font #{Rails.root}/public/fonts/DancingScript-Regular.ttf -size 380x50 -gravity west caption:'por #{self.name}' :dest",
:dest => File.expand_path(fileAux.path) )
Paperclip.run("composite", " -geometry +55+55 -gravity West :dest_aux :dest :dest",
:dest => File.expand_path(file.path), :dest_aux => File.expand_path(fileAux.path) )
end
Paperclip.run("composite", " -gravity West #{Rails.root}/public/result.png :dest :dest",
:dest => File.expand_path(file.path) )
self.image = file
ensure
file.close
file.unlink # deletes the temp file
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment