Register now and start sharing your code snippets.
-->

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

HTML (Rails) posted 6 months ago by christian

   1  <input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden" />

or

   1  <%= token_tag %> 

Tagged authenticity, token, ruby, rails

How to add a watermark to images using MiniMagick, attachment_fu and Ruby

Ruby posted 6 months ago by christian

Use this snippet to add a watermark to an image, after the image is uploaded:

   1  class Image
   2    .
   3    has_attachment ...
   4    .
   5    .
   6    after_attachment_saved do |record|    
   7      # Don't add watermarks to thumbnails
   8      if record.thumbnail.nil?
   9        full_path = File.join(RAILS_ROOT, 'public/', record.public_filename)
  10        
  11        img = MiniMagick::Image.from_file(full_path)
  12        
  13        width = img[:width]
  14        height = img[:height]
  15        
  16        if width > 150 && height > 150
  17          img.combine_options do |c|
  18            c.gravity 'SouthWest'
  19            # This is RAILS_ROOT/images/watermark.gif
  20            c.draw "image Over 0,0 0,0 \"images/watermark.gif\""
  21          end
  22  
  23          img.write(full_path)
  24          
  25        end
  26      end
  27      
  28    end

Note that after_attachment_saved is a callback added by attachment_fu, use after_save if you’re not using attachment_fu.

Tagged attachment_fu, mini_magick, ruby, watermark

How to add a text caption to an image with MiniMagick and Ruby

Ruby posted 6 months ago by christian

   1  require 'rubygems'
   2  require 'mini_magick'
   3  
   4  img = MiniMagick::Image.from_file("jpeg.jpg")
   5  
   6  img.combine_options do |c|
   7    c.gravity 'Southwest'
   8    c.draw 'text 10,10 "whatever"'
   9    c.font '-*-helvetica-*-r-*-*-18-*-*-*-*-*-*-2'
  10    c.fill("#FFFFFF")
  11  end
  12  
  13  img.write("new.jpg")

Tagged mini_magick, ruby, caption, text

How to add a watermark to pictures with MiniMagick and Ruby

Ruby posted 6 months ago by christian

This code can be used to add a watermark to pictures:

   1  require 'rubygems'
   2  require 'mini_magick'
   3  
   4  # Read the image
   5  img = MiniMagick::Image.from_file("the_picture.jpg")
   6  
   7  # 0,0 0,0 = add the watermark at coordinates: x, y, set watermark size to auto with 0,0
   8  img.draw 'image Over 0,0 0,0 "the_watermark.gif"'
   9  
  10  img.write("watermarked_image.jpg")

How to add a watermark to the bottom-left corner?

To add a watermark to the bottom-left corner, you need to use the combine_options method to pass more than one command plus parameters to MiniMagick (gravity and draw commands):

   1  require 'rubygems'
   2  require 'mini_magick'
   3  
   4  img = MiniMagick::Image.from_file("the_image.jpg")
   5  
   6  img.combine_options do |c|
   7    c.gravity 'SouthWest'
   8    c.draw 'image Over 0,0 0,0 "the_watermark.gif"'
   9  end
  10  
  11  img.write("new.jpg")

Troubleshooting

See mogrify’s documentation for details on how to use the draw command.

If you get this error it might mean that mogrify can’t find the watermark image, or that the syntax is incorrect:

   1  mogrify: Non-conforming drawing primitive definition `image'.

Tagged watermark, overlay, caption, mini_magick, ruby, gravity, draw

A Ruby script that deletes images that are not used anywhere

Ruby posted 7 months ago by christian

   1  Dir.glob("**/*.{jpg,gif,jpeg,png}").sort.each do |image|
   2  
   3    name   = File.basename(image)
   4    in_use = false
   5    
   6    Dir.glob("**/*.{erb,css,js,html,htm}").each do |file|
   7      contents = File.read(file)
   8  
   9      in_use = contents.include?(name)
  10      
  11      break if in_use
  12    end
  13  
  14    if !in_use
  15      puts "Deleting '#{image}'"
  16      File.delete(image)
  17    end
  18  end

Tagged ruby, script, image