Adding custom "styles" to Paperclip gem - ruby-on-rails

Right now I have this
has_attached_file :cover_image,
styles: { square: "200x200#", small:"400x400>",medium:"500x600" }
I want to have this:
has_attached_file :cover_image,
styles: { thumb: "50x50#", square: "200x200#", small: "400x400>", medium: "500x600" }
When I add the "thumb" key I get a 404 error. Is there a way to create a new "style"?

Seems like there is no issue in your code.
You might be getting error while fetching previous record's thumb, which is not generated yet. Try create previous record thumbnail also and your error will gone.
Run below command in console.
rake paperclip:refresh CLASS=User ## change User with model name.
More information you can find here.

Related

Rails 5 - download a paperclip attachment

In the app I am building to learn RoR, I want to automatically download an attached pdf. Attached with Paperclip. Then to get the pdf properties using pdfinfo.
In my annotation.rb model I attach using
has_attached_file :file,
styles: { large: "600x600>", medium: "500x500>", thumb: "150x150#" },
default_url: "/images/:style/missing.png"
In my AnnotationsController
require 'pdfinfo'
pdfinfo = Pdfinfo.new('#annotation.file.path')
page_count = pdfinfo.page_count
this throws an error
Pdfinfo::CommandFailed in AnnotationsController#pdf pdfinfo -f 0 -l -1
-enc UTF-8 #annotation.file.path
What does this error mean and how can I get the file to be read? I learned about downloading using send_file, but have not succeeded.
You are passing a literal string to the new method. You need to remove the quotes.
require 'pdfinfo'
pdfinfo = Pdfinfo.new(#annotation.file.path)
page_count = pdfinfo.page_count

Set restrictions for files in /public folder with rails and paperclip

I'm using paperclip for image storage, and i have a problem. When the file is uploaded, paperclip generates two files, a processed image with a watermark and the original file, in this case the image files are in public folder, now the question is, can i restric the url in case the user enter into it like:
localhost:3000/files/photos/image_processeds/57308cd52cb1be0846e4be9f/original/image.png
If the user enter into the link, it will be forbidden.
This is my paperclip config
has_mongoid_attached_file :image_original,
:url => "/files/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/:class/:attachment/:id/:style/:basename.:extension"
and
has_mongoid_attached_file :image_processed,
processors: [:watermark],
styles: {
thumb: ['150x150', :jpg],
small: ['350x300', :jpg],
medium: ['550x500', :jpg],
original: {geometry: '60%',watermark_path: "#{Rails.root}/public/images/logo.gif", position: "Center"}
},
:url => "/files/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/:class/:attachment/:id/:style/:basename.:extension"
Theres is a way to do it or maybe find a better approach for that?
The original/image.png should be watermarked I guess.
I mean the version without watermark should not exist after processing.
Did you check it?
Solved, a reliable solution is save a copy of the original image into the database and grant access to it only when they purchase the photo, otherwise they only can see the copies with watermark :)

Rails paperclip resizing not working

I am trying to resize my images to 256x256 ignoring aspect ratio, so I used:
has_attached_file :thumbnail, styles: { medium: "256x256!", thumb: "300x300"}
in my model, and in view:
<%= image_tag guide.thumbnail(:medium), class: "guide_thumbnail" %>
but It fails to work. After changing to:
has_attached_file :thumbnail, styles: { medium: "50x50!", thumb: "300x300"}
Nothing changes, so I guess It is using default styles? How can I change it and force to use my ones instead? Please help.
PS: After adding newly named style:
has_attached_file :thumbnail, styles: { medium: "256x256!", thumb: "300x300", hihi: "50x50!"}
It does not show any image - only its name. I checked the logs and that is the error message:
ActionController::RoutingError (No route matches [GET] "/system/guides/thumbnails/000/000/088/hihi/test300.jpg"):
What should I do?
And Additionally I am curious if setting the width of an image via css(for example width: 256px;) is a good practice?
You can change into this:
has_attached_file :thumbnail, styles: { medium: "50x50>", thumb: "300x300"}
In my opinion, you'd better use image version properly with the real version.

Ruby on rails gem paperclip How to generate missing image

I uploaded a missing.png to public/images/ folder. But the the missing.png not showing correctly.
The website requires images/small/missing.png
So I guest I should generate the missing.png for thumb small medium large size.
What should I do?
Update 1:
I manually create a folder public/images/small and put missing.png inside the small folder. The website shows the missing.png. But What's is the correct way to generate all size of missing.png?
Try to add this in your model:
has_attached_file :image, styles: { :small => "150x150>", medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"

Getting correct path/url from Paperclip Gem during after_post_process callback?

I have a fairly standard Paperclip setup, it's almost straight out of the readme. I have a simple method triggered via callback to get the primary colors out of an uploaded image, and save them to the corresponding instance.
class Image < ActiveRecord::Base
has_attached_file :file, :styles => { large: "800x>", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :file, :content_type => /\Aimage\/.*\Z/
after_post_process :get_colors
def get_colors
colors = Miro::DominantColors.new(self.file.url)
colors = colors.to_hex.join(',')
self.colors = colors
self.save
end
end
As you can see, I have an after_post_process callback, and it does get triggered. The trouble is that when I call self.file.url I get a path that looks like this:
"/system/images/files//original/Peterson-Products-Wireframe-v01.jpg?1398443345".
It's missing the :id_partion portion. It's real path should look more like:
"/system/images/files/000/000/033/original/Peterson-Products-Wireframe-v01.jpg?1398443345"
Should I be using some other callback? I only want this triggered once per upload... Never again if the image is updated. Is this a bug in paperclip that I should be filing on Github?
Rails Version 4.1
Paperclip Version 4.1
Ruby 2.1.0
Thanks so much!
try queued_for_write
colors = Miro::DominantColors.new(file.queued_for_write[:original].path)

Resources