Rails paperclip watermark "uninitialized constant Paperclip" - ruby-on-rails

I try apply watermark to my paperclip, it keep show error message and unable update/upload image. It keep show **
uninitialized constant Paperclip::Watermark::PaperclipCommandLineError
**
gem file
gem "paperclip", '4.2' gem 'rails', '4.2.6' gem
'paperclip-compression'
paperclip_processors/watermark.rb
module Paperclip
class Watermark < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
#watermark_path = options[:watermark_path]
#position = options[:position].nil? ? "SouthEast" : options[:position]
end
def make
src = #file
dst = Tempfile.new([#basename].compact.join("."))
dst.binmode
return super unless #watermark_path
params = "-gravity #{#position} #{transformation_command.join(" ")} #{#watermark_path} :source :dest"
begin
success = Paperclip.run("composite", params, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the watermark for #{#basename}" if #whiny
end
dst
end
end
end
lisitng.rb
class Listing < ActiveRecord::Base
require 'paperclip_processors/watermark'
has_attached_file :image,
:processors => [:watermark],
:styles => {
:medium => {
:geometry => "300x300>",
:watermark_path => "#{Rails.root}/public/images/watermark.png"
},
:thumb => "100x100>",
}

I think you're subclassing wrong. Don't subclass Thumbnail, subclass Paperclip::Processor
module Paperclip
class Watermark < Processor
...
https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/processor.rb

Related

How to fix 'Missing an image filename' error while try to add watermark using s3 image in paperclip

When I'm trying to upload watermark on s3 image through paperclip then it will give error like not authorised and missing an image filename because I'm using dynamic watermark URL. It is also stored in s3
photo.rb
has_attached_file :image,
:processors => lambda {|attachment|
if attachment.class.apply_watermark
[:thumbnail,:watermark]
else
[:thumbnail]
end
},
:styles => lambda { |attachment|
{
:medium => {
:geometry => "259x259#",
:watermark_path => attachment.instance.class.watermark_thumb_url,
:position => "SouthEast",
:s3_protocol => :https
},
:thumb => {
:geometry => Proc.new { |instance| instance.resize },
},
:original => {
:geometry => '1200>',
},
}
}, default_url: "https://s3_bucket_name.s3.ap-south-1.amazonaws.com/shared_photos/missing.png",
:s3_protocol => :https
lib/paperclip_processors/watermark.rb
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? ? "center" : options[:position]
#overlay = options[:overlay].nil? ? true : false
#current_format = File.extname(#file.path)
#basename = File.basename(#file.path, #current_format)
end
# 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 PaperclipCommandLineError
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
watermarks_controller.rb
def upload_watermark_on_dummy_image
Photo.apply_watermark = true
Photo.watermark_thumb_url = #watermark.photo.image.url(:thumb)
current_resource_owner.photos.create(image: File.new("public/shared_photos/dummy-image.jpg"))
end
It is giving error when try to run below command in watermark processors
command = "composite"
params = ["-gravity",
"SouthEast",
"https://s3.ap-south-1.amazonaws.com/s3_bucket_name/photos/images/000/000/025/thumb/download.png?1560325300",
"/tmp/3151d071493084e42ac1e51947ef71ce20190612-25378-1usu3ji20190612-25378-135qwqy",
"/tmp/3151d071493084e42ac1e51947ef71ce20190612-25378-1usu3ji20190612-25378-135qwqy"]
success = Paperclip.run(command, params.flatten.compact.collect{|e| "'#{e}'"}.join(" "))
It is giving below error:
Command :: composite '-gravity' 'SouthEast' 'https://s3.ap-south-1.amazonaws.com/s3_bucket_name/photos/images/000/000/026/thumb/download.png?1560328681' '/tmp/703fb0e145df584135fc841239e8aa3020190612-27615-14xprt020190612-27615-10g8hgo' '/tmp/703fb0e145df584135fc841239e8aa3020190612-27615-14xprt020190612-27615-10g8hgo'
Cocaine::ExitStatusError: Command 'composite '-gravity' 'SouthEast' 'https://s3.ap-south-1.amazonaws.com/s3_bucket_name/photos/images/000/000/026/thumb/download.png?1560328681' '/tmp/703fb0e145df584135fc841239e8aa3020190612-27615-14xprt020190612-27615-10g8hgo' '/tmp/703fb0e145df584135fc841239e8aa3020190612-27615-14xprt020190612-27615-10g8hgo'' returned 1. Expected 0
Here is the command output: STDOUT:

uninitialized constant Paperclip::Cropper

I have a custom processor for my Paperclip styles: cropper.rb. Though it is not called and return NameError (uninitialized constant Paperclip::Cropper) error.
It has been discussed here : Rails3 and Paperclip but a while ago. It was concerning Rails 3 back then.
I am under Rails 5 (update from Rails 4.x)
Profilepic.rb
class Profilepic < ApplicationRecord
belongs_to :professionnels
has_attached_file :image, styles: { big: "1200x1200", medium: "400x400", small: "250x250"}
validates_attachment :image, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: {less_than: 10.megabytes}
has_attached_file :finalimage, styles: { medium: "500x500", small: "200x200"}, whiny: false, use_timestamp: false, processors: [:cropper]
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
end
lib/paperclip_processors/cropper.rb
module Paperclip
class CustomCropper < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
if target.crop_w && target.crop_x
#current_geometry.width = target.crop_w
#current_geometry.height = target.crop_h
end
end
def target
#attachment.instance
end
def transformation_command
# call crop processing only if user inputs are there
if target.crop_w && target.crop_x
crop_command = [
"-crop",
"#{target.crop_w}x" \
"#{target.crop_h}+" \
"#{target.crop_x}+" \
"#{target.crop_y}",
"+repage"
]
crop_command + super
else
super
end
end
end
end
OK spent a day to realize the correct lib subfolder is actually paperclip and not paperclip_processors although Paperclip Git does mention both as valid and automatically loaded.

Paperclip::Error, There was an error processing the thumbnail

I'm using paperclip, imagemagick, rmagick to upload the images to s3. Previously the image upload functionality is working well on ruby 1.9.3,'rails', '3.2.11'. After upgrading ruby 2.0.0 and 'rails', '4.0.3' it throughing error.
In store.rb
#paperclip
has_attached_file :logo,
styles: {
thumb: ["40x40#", :png],
small: ["400x400>", :png],
masked_with_overlay: ["288x64", :png],
masked_with_nearby_overlay: ["238x47#", :png],
masked_with_rewards_overlay: ["288x64", :png],
masked_with_overlay_for_old: ["288x64", :png],
masked_with_nearby_overlay_for_old: ["238x47#", :png],
masked_with_rewards_overlay_for_old: ["288x64", :png],
masked_full_card_image: ["576x128", :png],
masked_logo_only: ["576x128", :png]
},
convert_options: { masked_with_overlay: Proc.new{self.apply_mask_and_overlay},
masked_with_nearby_overlay: Proc.new{self.apply_mask_and_nearby_overlay},
masked_with_rewards_overlay: Proc.new{self.apply_mask_and_rewards_overlay},
masked_with_overlay_for_old: Proc.new{self.apply_mask_and_overlay_for_old},
masked_with_rewards_overlay_for_old: Proc.new{self.apply_mask_and_rewards_overlay_for_old},
masked_full_card_image: Proc.new{self.apply_mask_full_card_image},
masked_logo_only: Proc.new{self.apply_mask_logo_only}
},
storage: :s3,
s3_credentials: "#{Rails.root}/config/s3.yml",
path: "/store_logos/:style/:id/:filename"
validates_attachment :logo, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/JPG", "image/JPEG", "image/PNG"] }
Here the Gemfile
ruby '2.0.0'
gem 'rails', '4.0.3'
gem 'aws-s3'
gem 'aws-sdk', '~> 1.5.7'
gem 'paperclip', "~> 4.2"
gem 'rmagick', '2.13.2'
stores_controller.rb
class StoresController < ApplicationController
def update
if !params[:store].nil? && !params[:store][:store_owner].nil?
params[:store][:store_owner][:email] = params[:store][:store_owner][:email].downcase
end
if params[:demo_store_changes]
params[:store][:active] = false
else
params[:store][:store_hours] = params[:store][:store_hours].to_json
params[:store][:minimum_seconds_per_scan] = (params[:store][:minimum_seconds_per_scan]).to_i * 3600
end
#store.store_owner.store_owner!
if params[:store][:scanning_distance].blank?
params[:store].delete :ms
else
if params[:store][:location_check]
if params[:store][:ms] == "mi"
params[:store][:scanning_distance] = ( params[:store][:scanning_distance].to_f * 5280 )
params[:store].delete :ms
else
params[:store][:scanning_distance] = params[:store][:scanning_distance].to_f
params[:store].delete :ms
end
end
end
if #store.update_attributes(store_params) and #store.build_categories(params[:category_ids].to_a)
if params[:demo_store_changes]
demo_message = #store.demo ? " is now demo store" : " is not a demo store anymore"
flash[:notice] = #store.name + demo_message
redirect_to stores_path('store[search]'=> params[:store_search], search_value: params[:search_value])
else
#password_chk=params[:store][:store_owner_attributes][:password]
redirect_to "#{store_path()}?myid=#{session[:bread_crum]}", notice: (I18n.t :update_store_success)
end
else
#store.store_hours = ActiveSupport::JSON.decode(#store.store_hours)
if #store.minimum_seconds_per_scan?
#store.minimum_seconds_per_scan = (#store.minimum_seconds_per_scan / 3600).to_i
end
#categories = Category.all.sort
#states = StoreConstants::STATES.sort
#main_location_stores = Store.with_no_branch(#store.id)
flash.now[:error] = (I18n.t :update_store_failed)
render action: "edit"
end
private
def store_params
params.require(:store).permit!
end
end
Here is log
Any help?, Thanks in advance
Look into the log
(:bucket_name=>"punchme-co-dev",:key=>"store_logos/masked_with_rewards_overlay/96/Casablanca.png") AWS::S3::Errors::NoSuchKey No Such Key)
AWS access key is taking your image file path.
I finally found the solution for this.
convert: no decode delegate for this image format `/home/likewise-open/ANNETSITE/1820/RailsWorkspace/Punchme_web_Test' # error/constitute.c/ReadImage/532.
convert: unable to open image `Builds/app/assets/images/card_overLay.png': # error/blob.c/OpenBlob/2587.
By renaming project folder Project_web_Test Builds to Project_web_Test_Builds, When you process image space is not allowed.

How to Re-size Images that are Too Large on-the-fly with Paperclip and Rails 3

I am trying to implement the steps to check and resize images with paperclip based on this blog post: http://www.techdarkside.com/how-to-re-size-images-that-are-too-large-on-the-fly-with-paperclip-and-rails
Here is what I have in place...
class Question < ActiveRecord::Base
# subclasses
class Question::Image < Asset
has_attached_file :attachment,
:url => "/uploads/:class/:attachment/:id_partition/:basename_:style.:extension",
:styles => Proc.new { |attachment| attachment.instance.styles },
:styles => Proc.new { |attachment| attachment.instance.resize }
attr_accessible :attachment
# http://www.ryanalynporter.com/2012/06/07/resizing-thumbnails-on-demand-with-paperclip-and-rails/
def dynamic_style_format_symbol
URI.escape(#dynamic_style_format).to_sym
end
def styles
unless #dynamic_style_format.blank?
{ dynamic_style_format_symbol => #dynamic_style_format }
else
{ :medium => "300x300>", :thumb => "100x100>" }
end
end
def dynamic_attachment_url(format)
#dynamic_style_format = format
attachment.reprocess!(dynamic_style_format_symbol) unless attachment.exists?(dynamic_style_format_symbol)
attachment.url(dynamic_style_format_symbol)
end
def resize
if self.attachment_file_size > 2000000
"300x300>"
else
" "
end
end
end
I'm thinking the issue is with the reuse of the :styles symbol, however I'm not sure how to work both the styles method AND the resize method into a single Proc statement.
Here is what I ended up with thanks to #janfoeh suggestion. I did need to add :originalto the options in style to get this to work. I also bumped the max file size up to 5mb.
class Question < ActiveRecord::Base
# subclasses
class Question::Image < Asset
has_attached_file :attachment,
:url => "/uploads/:class/:attachment/:id_partition/:basename_:style.:extension",
:styles => Proc.new { |attachment| attachment.instance.styles }
attr_accessible :attachment
# http://www.ryanalynporter.com/2012/06/07/resizing-thumbnails-on-demand-with-paperclip-and-rails/
def dynamic_style_format_symbol
URI.escape(#dynamic_style_format).to_sym
end
def styles
unless #dynamic_style_format.blank?
{ dynamic_style_format_symbol => #dynamic_style_format }
else
{ :original => resize, :medium => "300x300>", :thumb => "100x100>" }
end
end
def dynamic_attachment_url(format)
#dynamic_style_format = format
attachment.reprocess!(dynamic_style_format_symbol) unless attachment.exists?(dynamic_style_format_symbol)
attachment.url(dynamic_style_format_symbol)
end
def resize
if self.attachment_file_size > 5000000
"1000x1000>"
else
" "
end
end
end

watermark with paperclip

according to this example (http://dimaspriyanto.com/2010/06/08/image-watermarking-with-paperclip/), I try to put a watermark on every picture I upload (for now, I restrain myself to the large one).
And guess what? It doesn't work!
So in my picture model, I have
require 'paperclip_processors/watermark'
has_attached_file :image,
:styles => {:medium => "300x300^", :thumb => "150x105^",
:large => {
:geometry => "460",
:watermark_path => ":rails_root/public/images/watermark.png"
}
},
:url => "/images/:style/:id_:style.:extension",
:path => ":rails_root/public/images/:style/:id_:style.:extension"
and in /lib/paperclip_processors/watermark.rb, I have:
module Paperclip
class Watermark < Processor
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
def crop?
#crop
end
def convert_options?
not #convert_options.blank?
end
def make
dst = Tempfile.new([#basename, #format].compact.join("."))
dst.binmode
if watermark_path
command = "composite"
params = "-gravity #{#position} #{watermark_path} #{fromfile} #{transformation_command} #{tofile(dst)}"
else
command = "convert"
params = "#{fromfile} #{transformation_command} #{tofile(dst)}"
end
begin
success = Paperclip.run(command, params)
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the watermark for #{#basename}" if #whiny
end
dst
end
def fromfile
"\"#{ File.expand_path(#file.path) }[0]\""
end
def tofile(destination)
"\"#{ File.expand_path(destination.path) }[0]\""
end
def transformation_command
scale, crop = #current_geometry.transformation_to(#target_geometry, crop?)
trans = "-resize \"#{scale}\""
trans << " -crop \"#{crop}\" +repage" if crop
trans << " #{convert_options}" if convert_options?
trans
end
end
end
The watermark is in /public/images/ and it doesn't crash in the process, I mean the pictures are uploaded, in every size but the large one is nude, without the watermark.
Any idea?
Here's the preprocessor that works (I use it)
https://gist.github.com/2499137
Here's sample code for you model:
has_attached_file :data,
:processors => [:watermark],
:url => "/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:path => ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:styles => {
:thumb => '118x100#',
:content => {
:geometry => '700>',
:watermark_path => "#{Rails.root}/public/images/watermark.png",
:position => 'SouthWest'
},
}

Resources