Paperclip image upload in rails - ruby-on-rails

I am using paperclip for image uploading and in my model I have specified styles to store the images with various sizes, I have imagemagick installed, here are the codes
has_attached_file :image,
styles: { medium: '300x300>', thumb: '40x40>' },
path: 'images/:id/image/:basename.:extension',
default_url: 'default.png'
validates_attachment :image, content_type: { content_type: %w[image/jpeg image/png] }
It doesn't seems to store images with these specified styles, It only stores image with default style. Do I miss anything here?. Thanks in advance

I did a mistake by overriding the paperclip default path without :style specified. So it stores only the default size. The processed image (by ImageMagick) does not store as I didn't mention at override path.

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.

Paperclip ignores image sizing options

So I've got the Paperclip gem setup in my project to handle cover images for a model called Story:
class Story < ActiveRecord::Base
...
has_attached_file :cover_image,
styles: { large: "700x700>", thumb: "300x300>" },
default_url: "#{Rails.root}/app/assets/images/default_image.jpg",
path:"/cover_images/:filename",
processors: [:thumbnail, :paperclip_optimizer]
I'm trying to use the thumb style for when the story is displayed in a small format, which looks like this:
<div class="image">
<%= image_tag story.cover_image.url(:thumb) %>
</div>
This is the way the Paperclip documentation says you should specify which style you want to use.
In this context, the image has the correct dimensions of 300x111px.
However, when I move to my story page, I want the large/full size version of the image:
<div class="header_image">
<%= image_tag(#story.cover_image.url(:large)) %>
Except paperclip incorrectly still uses the thumbnail version of the image:
Why isn't Paperclip sizing the images correctly?
You must specify a :style option in the path:
has_attached_file :cover_image,
styles: { large: "700x700>", thumb: "300x300>" },
default_url: "#{Rails.root}/app/assets/images/default_image.jpg",
path:"/cover_images/:filename/:style",
processors: [:thumbnail, :paperclip_optimizer]
Now Paperclip will upload different versions of the image and access them as needed:

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"

Resources