Ich verwende Paperclip für ein Projekt, das mit Paperclip 3.0.3 perfekt funktioniert, aber mit Paperclip 3.2.0 total fehlerhaft ist. Mit 3.0.3 beschneidet es das Bild und fügt Wasserzeichen hinzu, mit 3.2.0 macht es eine Schleife während der Verarbeitung des ersten Befehls jedes Bildes und tut es wieder und wieder, bis es abstürzt.
Der unten stehende Code stammt größtenteils aus dem Railcast über Paperclip und anderen Code, den ich hier auf Stackoverflow gefunden habe. Ich dachte, es könnte die Leute interessieren, um eine funktionierende (mit paperclip 3.0.3) Version mit all dies integriert zu sehen.
Hier ist der Code des Modells
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
Hier ist der Code des Cropper-Prozessors
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
der Code des Wasserzeichens ist sehr lang und wurde in Stackoverflow gefunden
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
Wenn jemand eine Idee hat, warum es mit Paperclip 3.2.0 nicht funktioniert, kann ich im Internet keine Hilfe zu diesem Fehler finden.
Prost