paperclip 3.0.3 で完全に動作するプロジェクトで paperclip を使用していますが、paperclip 3.2.0 では完全にバグです。3.0.3 では、画像をトリミングして透かしを追加します。3.2.0 では、各画像の最初のコマンドを処理しながらループし、クラッシュするまで何度も繰り返します。
以下にあるコードは、主に、ペーパークリップに関する Railcast と、stackoverflow で見つけた他のコードからのものです。私は、これらすべてが統合された (paperclip 3.0.3 を使用した) 動作バージョンを見て興味を持つ人がいるかもしれません。
ここにモデルのコードがあります
require 'paperclip_processors/watermark'
require 'paperclip_processors/cropper'
class Imageblock < ActiveRecord::Base
belongs_to :block
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h, :old_size_width, :old_size_height
validates_presence_of :size_width, :size_height, :photo
validates_format_of :description, :with => /\A[^"]+\z/, :message => "No quotes allowed"
has_attached_file :photo, :styles => lambda { |attachment|
image = attachment.instance
dimensions = "#{image.size_width}x#{image.size_height}#"
{
:small => {:geometry => "100x100#"},
:custom => {:geometry => dimensions, :processors => [:cropper] },
:large => {:geometry => "500x500>"},
:display =>{:geometry => "1000x1000>", :watermark_path => "#{Rails.root}/app/assets/images/monogram.png", :processors => [:watermark]},
:retina =>{:geometry => "2000x2000>", :watermark_path => "#{Rails.root}/app/assets/images/monogram.png", :processors => [:watermark]}
}
}, :dependant => :destroy, :url => "/art/photo/:id/:style/:basename.:extension", :path => ":rails_root/public/art/photo/:id/:style/:basename.:extension"
after_update :reprocess_photo, :if => :cropping?
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
def photo_geometry(style = :original)
@geometry ||= {}
@geometry[style] ||= Paperclip::Geometry.from_file(photo.path(style))
end
private
def reprocess_photo
photo.reprocess!
end
end
ここにクロッパープロセッサのコードがあります
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ') # super returns an array like this: ["-resize", "100x", "-crop", "100x100+0+0", "+repage"]
else
super
end
end
def crop_command
target = @attachment.instance
if target.cropping?
["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"]
end
end
end
end
透かしのコードは非常に長く、stackoverflow で見つかりました
module Paperclip
class Watermark < Processor
# Handles watermarking of images that are uploaded.
attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options, :watermark_path, :overlay, :position
def initialize file, options = {}, attachment = nil
super
geometry = options[:geometry]
@file = file
@crop = geometry[-1,1] == '#'
@target_geometry = Geometry.parse geometry
@current_geometry = Geometry.from_file @file
@convert_options = options[:convert_options]
@whiny = options[:whiny].nil? ? true : options[:whiny]
@format = options[:format]
@watermark_path = options[:watermark_path]
@position = options[:position].nil? ? "SouthEast" : options[:position]
@overlay = options[:overlay].nil? ? true : false
@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
end
# TODO: extend watermark
# Returns true if the +target_geometry+ is meant to crop.
def crop?
@crop
end
# Returns true if the image is meant to make use of additional convert options.
def convert_options?
not @convert_options.blank?
end
# Performs the conversion of the +file+ into a watermark. Returns the Tempfile
# that contains the new image.
def make
dst = Tempfile.new([@basename, @format].compact.join("."))
dst.binmode
command = "convert"
params = [fromfile]
params += transformation_command
params << tofile(dst)
begin
success = Paperclip.run(command, params.flatten.compact.collect{|e| "'#{e}'"}.join(" "))
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error resizing and cropping #{@basename}" if @whiny
end
if watermark_path
command = "composite"
params = %W[-gravity #{@position} #{watermark_path} #{tofile(dst)}]
params << tofile(dst)
begin
success = Paperclip.run(command, params.flatten.compact.collect{|e| "'#{e}'"}.join(" "))
rescue
raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny
end
end
dst
end
def fromfile
File.expand_path(@file.path)
end
def tofile(destination)
File.expand_path(destination.path)
end
def transformation_command
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
trans = %W[-resize #{scale}]
trans += %W[-crop #{crop} +repage] if crop
trans << convert_options if convert_options?
trans
end
end
end
paperclip 3.2.0 で動作しない理由を誰かが知っている場合、このバグに関する Web 上のヘルプを見つけることができません。
乾杯