Error resizing images with Paperclip after images have been added - ruby-on-rails

This is the code in my model:
has_attached_file :photo,
:styles => {
:tiny => "25x25#",
:thumbnail => "50x50#",
:small => "150x150>",
:medium => "300x300>" },
:default_url => "/images/default_:style.jpg"
I run this command:
rake paperclip:refresh:thumbnails CLASS=Photo
and I get this error:
rake aborted!
uninitialized constant Photo
What going on? How can I fix this?

Obviously it doesn't know your model Photo. Try a command "require", e.g.
require "lib/photo"
or whatever your model path is.

Related

Rails and Paperclip storing images in a specific path sets wrong URL

I want to store my images using the normal file storage adapter.
This is my PAPERCLIP_STORAGE_OPTS:
PAPERCLIP_STORAGE_OPTS = {
:styles => { :thumb => '170x170!#', :medium => '450x300!>', :large => '600x400!>',:desktop => '750x300!>'},
:convert_options => { :all => '-quality 100' },
:processor => [ :papercrop ],
:path => "/opt/www/myapp/images/:class/:attachment/:id_partition/:style/:filename"
}
This is my model :
class User < ActiveRecord::Base
attr_accessor :PAPERCLIP_STORAGE_OPTS
has_attached_file :user_photo, PAPERCLIP_STORAGE_OPTS_THUMB
When a user uploads a photo - it actually does store the image in the correct location on my system:
/opt/www/myapp/images/users/user_photos/000/000/050/original/picture
However when I go to show the image, like this :
<%=image_tag current_user.user_photo.url(:thumb), :height=> "30", :width=> "30" %>
The image is not found, and in my logs I see the image request at this URL:
ActionController::RoutingError (No route matches [GET] "/system/users/user_photos/000/000/050/thumb/picture"):
And the full URL created is :
https://www.myapp.com/system/users/user_photos/000/000/050/thumb/picture?1460285803 - which doesnt resolve.
How can I configure paperclip to allow my images to be stored in this particular url /opt/www/myapp/images/ and still be accessed and linked to correctly through Paperclip in my rails app?
You will have to set URL option:
for me it was:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>", :small=>"60x60>" },
:path => ':rails_root/public/system/:class/:id/:style/:filename',
:url => '/system/:class/:id/:style/:filename'
Not sure for your case as you store images in the app folder directly so you may try(test it from console and modify it):
:path => "/opt/www/myapp/images/:class/:attachment/:id_partition/:style/:filename",
:url => '/images/:class/:attachment/:id_partition/:style/:filename'

Paperclip - Using custom watermark with dynamic path

I'm trying to create a custom path using this:
has_attached_file :avatar,
:processors => [:watermark],
:styles => lambda { |attachment| {
:medium => {
:geometry => "300x300>",
:watermark_path => Customer.find(attachment.instance.customer_id).signature.path
},
:thumb => "100x100>",
}
}
This is working fine with Customer.find(attachment.instance.customer_id).signature.path with a static path to image, but in this case it is throwing an exception
Paperclip::Errors::InfiniteInterpolationError in Api::Ipad::V1::ImagesController#create
Can anyone tell how to ignore this exception ?
Usually that error comes from not explicity defining the :url and :path options for the attachment, might try setting those options and see if the interpolation error goes away.

Paperclip change default path and hash image names

I'm new to Rails and using Paperclip, I have it set up on my model already without issue. I just don't like the path it's generating for my images right now (:root_path/system/users/avatars/000/000/001) I really don't even understand it. How can I modify this default path for my images to something more friendly? And how can I hash the image names?
In your model, you can set the default path, styles and url as so:
has_attached_file :avatar,
:styles => { :large => "500x500>", :medium => "300x300>", :thumb => "100x100>" },
:path => ":rails_root/public/images/:id/:style/:filename",
:url => "/images/:id/:style/:filename"
you can setup the hash on the paperclip initializer file (config/initializers/paperclip_defaults.rb)
Quoting from paperclip wiki:
Paperclip::Attachment.default_options.update({
:path => ":class/:attachment/:hash/:style.:extension",
:hash_secret => "SOME_RANDOM_SECRET"
})
The :hash part is generated from :hash_secret and the pattern given by the :hash_data option, which by default is ":class/:attachment/:id/:style/:updated_at".

Rails paperclip styles not working

I'm trying to apply styles to an image uploaded by paperclip, to save the original and a thumbnail.
has_attached_file :image, styles => { :thumb => "100x100", :original => "100%"},
:path => "images/:id/:style_:basename.:extension"
Is not working, it only saves the original, even I delete the :original => "100%" part.
And I've ImageMagick installed and the gem rmagick.
:thumb => "100x100" s/b :thumb => "100x100>"
you forgot the >
see doc:
https://github.com/thoughtbot/paperclip
remove this :original => "100%"

How to pass additional convert options to paperclip on Heroku?

class User < ActiveRecord::Base
has_attached_file :photo, :styles => { :square => "100%", :large => "100%" },
:convert_options => {
:square => "-auto-orient -geometry 70X70#",
:large => "-auto-orient -geometry X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
validates_attachment_size :photo,
:less_than => 5.megabyte
end
Works great on local machine, but gives me an error on Heroku: There was an error processing the thumbnail for stream.20143
The thing is I want to auto-orient photos before resizing, so they resized properly.
The only working variant now(thanks to jonnii) is resizing without auto-orient:
...
as_attached_file :photo, :styles => { :square => "70X70#", :large => "X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
...
How to pass additional convert options to paperclip on Heroku?
UPD
I discover, the trouble in "-auto-orient" option. It seems like this option is broken in version of ImageMagick used by Heroku. I created custom paperclip image processor inherited from paperclip's standard thumbnail:
module Paperclip
class Ao < Thumbnail
def transformation_command
super + " -auto-orient"
end
end
end
It works perfect on local machine, but fails on Heroku.
These are the sizes I use. They all work fine on heroku:
SIZES = {
:original => "640x480>",
:thumb => "150x150#",
:mini => "60x60#",
:micro => "30x30#"
}
Make sure your gem version of paperclip is the same as heroku's. You can specify the specific gem version in your .gems file and in your environment.rb to make sure they line up.
I'm not sure exactly why your convert_options are causing problems, but if I remember correctly paperclip uses ImageScience directly and your chosen options might be incompatible with the read only heroku file system.
If this is critical and you need an answer right now I'd raise a support ticket on heroku. If you get a response make sure you post it back here!

Resources