Rails paperclip styles not working - ruby-on-rails

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%"

Related

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 generate thumbnail from other style

I'm following the jcrop rails tutorial, but I've hit a snag. What it comes down to is the fact that paperclip is generating the thumbnail from the original file, but I need it to be generated from another style. The original file doesn't have any space between the product shot and the edge of the document. Therefore I can't crop further out. To combat that, I made another style which has white pixel padding. That's what I want to generate the thumbnail from.
# croppable is the one with the padding...it's what shows up in the crop view.
# I want :thumb to be generated from THAT style, not :original.
# When generating from :original, the crop offset/size is screwed because the dimensions of :original don't match :cropped
# and I can't crop beyond the pixel dimensions of :original.
has_attached_file :photo, :styles => {
:thumb => { :geometry => "300x300#", :format => :jpg, :processors => [:cropper] },
:general => ["150x375", :jpg],
:show => ["x425", :jpg],
:croppable => ["1200x1200>", :jpg]
},
:url => "/assets/wines/:style/:wine_name",
:path => ":rails_root/public:url",
:default_url => ":wine_default",
:default_path => ":rails_root/public:wine_default",
:default_style => :show,
:convert_options => {
:thumb => '-gravity center -rotate -30',
:croppable => '-gravity center -extent 1200x1200',
:general => '-gravity center -extent 150x375 -quality 95',
:all => '-quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0'
},
:processors => [:thumbnail, :compression]
I came across a processor that someone else had made online and used it for myself. Details can be found at http://pjkh.com/articles/speeding-up-thumbnail-generation-with-paperclip/ for advanced usage information.
recursive_thumbnail.rb (include in lib/paperclip_processors)
module Paperclip
class RecursiveThumbnail < Thumbnail
def initialize file, options = {}, attachment = nil
super Paperclip.io_adapters.for(attachment.styles[options[:thumbnail] || :original]), options, attachment
end
end
end
and for your model:
:styles => {
:croppable => ["1200x1200>", :jpg],
:show => ["x425", :jpg],
:general => ["150x375", :jpg],
:thumb => {
:geometry => "150x150^",
:format => :jpg,
:processors => [:recursive_thumbnail] },
:thumbnail => :croppable
}
Maybe this would help you or someone else like me who Google led to this question.
https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation
Generating/regenerating your thumbnails
Note: regenerating only one of several defined styles as described in
some of the examples below will lead to broken paths/urls for all
other styles if you have :hash in your paperclip_defaults[:path] and
:updated_at in your paperclip_defaults[:hash_data] (and you have by
default). Don’t do this unless you really know what you’re doing.
You can (re)generate your thumbnails en masse with Paperclip’s rake
tasks. Using our example class above:
bundle exec rake paperclip:refresh:thumbnails CLASS=User

Paperclip is not saving the original file when a style is defined

I'm working to make Paperclip store a thumbnail in addition to the original image. The original image had always saved file and now I'm trying to add a thumbnail.
The problem is, once I defined the style thumbnail, the original is no longer being saved. How can I get the original to save along with the thumb?
has_mongoid_attached_file :attachment,
:storage => :s3,
:s3_credentials => "s3.yml",
:s3_protocol => 'https',
:s3_permissions => :private,
:use_timestamp => true,
:default_style => :original,
:default_url => '/images/:attachment/default_:style.png',
:path => "/:rails_env/private/:basename.:extension",
:styles => {
:thumb => "100x100#" },
:convert_options => {
:thumb => "-quality 75 -strip" }
Install Imagemagic (https://github.com/thoughtbot/paperclip) and then you can rezize your original sized photo into any size by using like this
<img src="#", size: '150x150'>

Paperclip and Amazon S3. Styled images can't be displayed, only downloaded. Original image is blank

Having some odd issues with paperclip, s3 and imagemagick.
The normal sized image will force an image download.
http://ads_honours_development.s3.amazonaws.com/assets/adverts/10/normal.jpg?1333899985
Meanwhile the original image is blank.
http://ads_honours_development.s3.amazonaws.com/assets/adverts/10/original.jpg?1333899985
Here is the model.
class Advert < ActiveRecord::Base
belongs_to :group
#paperclip
has_attached_file :photo, {
:styles => { :large => "512x512", :normal => "360x360", :small => "200x200", :tiny => "64x64", :thumbnail => "32x32" },
:convert_options => { :large => "-quality 86" },
:default_url => "/images/thumbnail/blank-recipe.png",
:url => ':s3_domain_url',
:path => 'assets/:class/:id/:style.:extension',
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:s3_permissions => 'public-read',
:s3_protocol => 'http'}
validates_attachment_presence :photo
end
Gem file is like:
gem 'aws-sdk'
gem 'paperclip', '~> 2.0'
I can't work out what I'm doing wrong here. Any help/suggestions?
UPDATE I've got this running on heroku as well and the exact same issue occurs, so it's not down to my development machines settings. I'm guessing the problem lies within Paperclip itself, my bucket setup or the amazon aws-sdk gem.
SECOND UPDATE Ok so all of these images load in IE, but not Chrome. What gives?
Maybe you have an extension running on chrome which is blocking the images? The link shows for me in Chrome only with adblock disabled.

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