Rails paperclip generate thumbnail from other style - ruby-on-rails

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

Related

URL issue: paperclip-av-transcoder

I am trying to implement paperclip-av-transcoder gem. I have checked everything but not able to find what I am doing wrong here. I'm writing steps which I have followed.
Added into gemfile
--> gem 'paperclip-av-transcoder'
Added into my model
--> has_attached_file :video_file, :styles => {
:medium => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 }
}, :processors => [:transcoder]
--> validates_attachment_content_type :video_file, :content_type => /\Avideo\/.*\Z/
created schema to add column name
"video_file_meta"
In my view file
video_tag(video.video_file.url, controls: true, autobuffer: true, size: "320x240")
I have checked video in public/system folder it is properly saved I am able to see that video there but I am not able to see that in my view file.
Video Url -> /system/videos/video_files/000/000/003/original/tingtong_464.mp4?1497851104
I am sharing screens to show how it looks in the browser.
Everything from my point is correct, except Paperclip requires at least one field in database - *_file_name (for you it's video_file_file_name), but you didn't added it and Paperclip can't construct url properly. Read more https://github.com/thoughtbot/paperclip#usage
Currently working model file code:
has_attached_file :video_file, :styles => {
:medium => { :geometry => "500x500", :format => 'jpg' },
:thumb => { :geometry => "100x100", :format => 'jpg' }
}, :processors => [:transcoder]
validates_attachment_content_type :video_file,
:content_type => [
"video/mp4",
"video/quicktime",
"video/3gpp",
"video/x-ms-wmv",
"video/mov",
"video/flv",
],
:message => "Sorry! We do not accept the attached file type"
I guess I am not resizing my video file in any other video format so it is working properly.

Paperclip Resize and Crop

I am using Paperclip with Rails4. I have the following image
Now i want to resize and Crop the image but its getting cropped and orientation of the image is getting displaced like below
:photo,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename",
:styles => {
:small => { :geometry => "100x100!" },
:medium => { :geometry => "500x500!"}
}
Check out ImageMagic resize docs. You probably need to use 100x100# instead of 100x100!

rmagick rotate and resize to fit specified dimensions in paperclip

All I essentially want to do, is to crop an image using jcrop, then with those dimensions, rotate the image -30deg and save the thumbnail. However rmagick creates additional pixels due to the fact that there's no scaling involved after the rotation. I'd like to know how to achieve that.
# 150 x 150 is the final cropped image I want for my thumb(nails)
# :croppable is a file I use that takes the original and adds white padding in a 1200x1200
# file size so I can actually crop with white space available (you can't crop outside the
# original dimensions of the file)
has_attached_file :photo, :styles => {
:thumb => { :geometry => "150x150#", :format => :jpg, :processors => [:cropper] },
:general => ["150x375", :jpg],
:show => ["x425", :jpg],
:croppable => ["1200x1200>", :jpg]
},
:url => "/assets/w/:style/:w",
:path => ":rails_root/public:url",
:default_url => ":w_default",
:default_path => ":rails_root/public:w",
: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 -interlace Plane -unsharp 0.3x0.3+5+0'
},
:processors => [:thumbnail, :compression]
What I eventually want to do is to rotate the preview image via css transform, so that the preview actually shows what the thumbnail will look like after cropping. At this point I'm not sure how to get what I want when it comes to paperclip saving the thumbnail and rotating it.
No answer found. I ended up switching to Carrierwave and encountering another set of problems which I've finally arrived at a solution for. Too bad Carrierwave doesn't log the processing of images, but that's a small price to pay.
Carrierwave RMagick not removing transparency in convert to jpg
Carrierwave +repage option not working

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