Screencast: Bildbearbeitung mit RMagick

In diesem Screencast zeigt Ryan wie Bilder mit RMagick verändert und/oder verarbeitet werden. Teil des Screencastes widmet er auch CarrierWave das in Kombination mit RMagick für die Verarbeitung von hochgeladenen Bildern eingesetzt werden kann.

Downloads in verschiedenen Formaten:

mp4
m4v
webm
ogg

 

Resourcen:

terminal

[bash]
brew install imagemagick
convert -version
convert octocat.png -resize ’70×70^‘ -gravity center -crop ’70×70+0+0′ -quantize GRAY -colors 256 -contrast source.png
composite stamp_overlay.png source.png source.png
convert -size 70×70 canvas:red ( octocat.png -resize ’70×70^‘ -gravity center -crop ’70×70+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
[/bash]

stamp.rb

[ruby]

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 70×70 canvas:red \( octocat.png
# -resize ’70×70^‘ -gravity center -crop ’70×70+0+0′
# -quantize GRAY -colors 256 -contrast stamp_overlay.png
# -composite -negate
# \) -compose copy-opacity -composite stamp.png
# COMMAND
[/ruby]

Gemfile

[ruby]
gem ‚rmagick‘
gem ‚carrierwave‘
[/ruby]

models/stamp.rb

[ruby]

mount_uploader :image, StampUploader
[/ruby]

app/uploaders/stamp_uploader.rb

[ruby]

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
[/ruby]

stamps/index.html

[html]
<%= image_tag stamp.image_url(:blue) %>
[/html]