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]

Screencast: Datei-Upload mit CarrierWave

CarrierWave ist ein Gem, dass für Dateiübertragungen in Ruby eingesetzt werden kann. Es vereinfacht nicht nur die Uploads, sondern unterstützt dabei auch eine verschiedenste ORMs, Frameworks und Bildverarbeitungstools wie z.B. image-magick.

 

Download:

Download(20.7 MB, 9:05)
Alternativer Download für iPod & Apple TV(18.5 MB, 9:05)

 

Resourcen:

 

Quellcode:

[bash]
bundle
rails g uploader image
rails g migration add_image_to_paintings image:string
rake db:migrate
[/bash]

[ruby]
# Gemfile
gem "rmagick"
gem "carrierwave"

# models/painting.rb
class Painting < ActiveRecord::Base
attr_accessible :gallery_id, :name, :image, :remote_image_url
belongs_to :gallery
mount_uploader :image, ImageUploader
end

# app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick

storage :file

def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

version :thumb do
process :resize_to_limit => [200, 200]
end
end
[/ruby]

[html]
<!– galleries/show.html.erb –>
<%= image_tag painting.image_url(:thumb) if painting.image? %>

<!– paintings/_form.html.erb –>
<%= form_for @painting, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<%= f.hidden_field :gallery_id %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.file_field :image %>
</p>
<p>
<%= f.label :remote_image_url, "or image URL" %><br />
<%= f.text_field :remote_image_url %>
</p>
<p><%= f.submit %></p>
<% end %>
[/html]