#374 Image Manipulation
Aug 16, 2012 | 11 minutes | Tools
Learn how to do extensive image manipulation with the ImageMagick commands. Also learn how RMagick can be used in combination with CarrierWave to process uploaded images.
- Download:
- source codeProject Files in Zip (54.2 KB)
- mp4Full Size H.264 Video (30.4 MB)
- m4vSmaller H.264 Video (14.5 MB)
- webmFull Size VP8 Video (15.3 MB)
- ogvFull Size Theora Video (30.3 MB)
Resources
- ImageMagick
- ImageMagick Usage Guide
- RMagick
- Image Sorcery
- Mini Magick
- Episode 253: CarrierWave File Uploads
terminal
brew install imagemagick convert -version convert octocat.png -resize '70x70^' -gravity center -crop '70x70+0+0' -quantize GRAY -colors 256 -contrast source.png composite stamp_overlay.png source.png source.png convert -size 70x70 canvas:red \( octocat.png -resize '70x70^' -gravity center -crop '70x70+0+0' -quantize GRAY -colors 256 -contrast stamp_overlay.png -composite -negate \) -compose copy-opacity -composite stamp.png gem install rmagick mate stamp.rb ruby stamp.rb
stamp.rb
require "rmagick" source = Magick::Image.read("octocat.png").first source = source.resize_to_fill(70, 70).quantize(256, Magick::GRAYColorspace).contrast(true) overlay = Magick::Image.read("stamp_overlay.png").first source.composite!(overlay, 0, 0, Magick::OverCompositeOp) colored = Magick::Image.new(70, 70) { self.background_color = "red" } colored.composite!(source.negate, 0, 0, Magick::CopyOpacityCompositeOp) colored.write("stamp.png") # Or through the command line: # system <<-COMMAND # convert -size 70x70 canvas:red \\( octocat.png \ # -resize '70x70^' -gravity center -crop '70x70+0+0' \ # -quantize GRAY -colors 256 -contrast stamp_overlay.png \ # -composite -negate \ # \\) -compose copy-opacity -composite stamp.png # COMMAND
Rails App
Gemfile
gem 'rmagick' gem 'carrierwave'
models/stamp.rb
mount_uploader :image, StampUploader
app/uploaders/stamp_uploader.rb
class StampUploader < CarrierWave::Uploader::Base include CarrierWave::RMagick # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: include Sprockets::Helpers::RailsHelper include Sprockets::Helpers::IsolatedHelper # Choose what kind of storage to use for this uploader: storage :file # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: def extension_white_list %w(jpg jpeg gif png) end %w[red green blue purple black].each do |color| version(color) { process stamp: color } end def stamp(color) manipulate! format: "png" do |source| overlay_path = Rails.root.join("app/assets/images/stamp_overlay.png") overlay = Magick::Image.read(overlay_path).first source = source.resize_to_fill(70, 70).quantize(256, Magick::GRAYColorspace).contrast(true) source.composite!(overlay, 0, 0, Magick::OverCompositeOp) colored = Magick::Image.new(70, 70) { self.background_color = color } colored.composite(source.negate, 0, 0, Magick::CopyOpacityCompositeOp) end end end
stamps/index.html
<%= image_tag stamp.image_url(:blue) %>