I want to provide my styles parameter with some lambda that checks if the file is an SVG file, scale it properly or not at all, I would like to communicate with the model as I do with all my other images, as when i render them (#image.image(:thumb). Is this possible?
Right now I attach my file as:
has_mongoid_attached_file :image,
:path => 'app/assets/images/library/:id/:style.:extension',
:styles => {:thumb => "216x162#", :medium => "400x300#", :scenario => "700x525#"},
:url => '/assets/library/:id/:style.:extension'
I've read about dynamic styles and did some trial and error with not success. My thought was that someone perhaps already have done this.
correct me if I misunderstood your question.
Please check https://github.com/thoughtbot/paperclip#dynamic-styles which says you can provide an lambda with attachment as argument of this lambda.
Inside the block you can use attachment.instance.#{any instance method of model}.
Related
I use paperclip to upload a users avatar. The image is stored correctly in the /public directory. However I cant figure out how I can get the image displayed. I played with the :url and :path settings for about an hour and cant match them in a way the image will be displayed in the browser.
There is always a 'images/localhost' in the GET-requests path that I can not get rid of.
Here is my code:
user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "missing.png",
:url => ':class/:id/:style.:extension',
:path => ':url'
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
The path in the html-tag looked up by the request looks like this:
<img alt="Original" src="/images/localhost/users/1/original.png?1405249849" />
The correct request which returns the image would be
http://localhost:3000/users/1/original.png?1405248482.
How can I get the request match the correct file-system-path. Or: what is wrong with my settings?
I didnt change the application.rb or the development.rb
Best regards,
Kev
you should take url or path helpers of paperclip wherever possible. So to show the image use:
<%= image_tag #user.avatar.url(:medium) %>
Then:
the image url isn't a file system path. Depending on the storage you use in paperclip, images can reside in different places, see understanding storage of the paperclip gem.
If you use file storage, the files are store somewhere like
public/system/users/avatar/000/000/013/small/my_pic.png
Timestamp
What you're seeing is Paperclip's timestamp - the last time the file / object was updated.
Although I can't find any official reference to this number, it basically allows you to determine which files you're dealing with. According to the question referenced above, it's apparently there for if you want to ensure your visitors see the latest version of the file (IE never gets stored in the cache)
I'm not sure why there is a disparity between your stored image & your path. However, I would say the path is correct; you just need to be able to
--
Bottom line - if your image shows on the page, I don't think there's any systemic issue with your path; if it doesn't show on the page, can you provide logs / reference to the error?
This post, https://stackoverflow.com/a/26222093/1949363, which pointed to http://www.bwigg.com/2009/10/paperclip-customizing-paths-and-urls/ was the answer for me. I was having issues only in my test environment but I believe the fix should work in other environments as well.
Try the following settings:
:path => "public/system/:class/:id/:filename",
:url => "/system/:class/:id/:basename.:extension"
Im referring to this app link as a tutorial. After looking at it. Im curious to understand how the program (client or server)can accesse images. Since the program doesn't seem to be explicitely saving image urls in the server's database once they are uploaded to Amazon S3?
After implementing this app, I dont see a column for image_urls or path in the database. But the program magically loads images in the client!!
Something is going on under the hood, what is that something?
Here is the Photo model method:
Paperclip.interpolates :prefix do |attachment, style|
"#{attachment.instance.takenby}/#{Date.today.to_s }/#
{attachment.instance.image_file_name}"
end
has_attached_file :image,
:path => ":prefix/:style/:basename.:extension",
:styles => { :thumbnail => "57x57", :original => "300x300" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS
validates :image,:presence => true
validates :lat, :lng,:presence => true,:numericality => true
It's right there in the options for has_attached_file. You've told it how to construct the path for the image (which corresponds to its filename on s3) in the :path argument. So all paperclip needs to know to find the image again is:
The host name for your s3 bucket
The relevant information to re-construct the path - in this case, that looks like :prefix, :basename, :style, and :extension.
Most of that information is in your database. Presumably :basename comes from the original filename somehow, which ought to be saved in the image_file_name field of the model to which the image is attached. :style depends on which image size you're looking up at runtime. :extension can be determined from the image_content_type attribute.
:prefix is a little bit trickier. In fact, I worry that your interpolation rule will break your image lookup, because of the Date.today it uses to construct the prefix. Do you have any images more than one day old? If so, do they still work? I worry that when the image is uploaded, it will have a filename containing that day's date, and then when you go to find them in the future you'll reconstruct the url using the new current date, and wind up with a 403 error.
TL;DR paperclip constructs the image url when it uploads the images to s3, and reconstructs them later from the same parameters.
I am using paperclip and would like to process a set of files offline and ideally move them to a content server. But I am not seeing where paperclip is storing this information to be able to get at it. The hash used isn't meant to obfuscate so I'm not that worried about it but would like to handle different images with same name as we've had problems with this from current digital cameras.
I have the following interpolation:
Paperclip.interpolates :val do |attachment, style|
Digest::MD5.hexdigest("me-" + attachment.size.to_s + "-other")
end
has_attached_file :asset,
:url => "/images/:global_path/:val-:style.:extension",
:path => UPLOAD_PATH + "/:global_path/:val-:style.:extension",
:styles => { :medium => "200x200>", :thumb => "75x75>", :bigthumb => "125x125"
}
Producing a url like: /images/170/1c8ce44646e4cbe816ebe901482783e9-thumb.jpg?1337831837
I'd like to be able to rsync the images directory but can't find how to get at the hash value. Where would I find this?
thx
There are a lot of good resources out there that show how to create a Rails application with multiple image uploads. Additionally, there are a lot of good resources showing how to use paperclip to upload different file types (PDF, image, .Doc).
I'd like to create an application where a User has an image asset for their profile picture, and also has the ability to upload PDFs or .Doc files to their account to be tied to the User. Has anyone had experience with this to confirm it's possible through the Paperclip gem? Any tutorials or resources to point me in the right direction would be appreciated.
I was also having exactly same problem like you. No need to do extra coding for this, just add following
has_attached_file :attachment,
:styles => lambda{ |a|
["image/jpeg", "image/png", "image/jpg", "image/gif"].include?( a.content_type ) ? {
:thumb=> "100x100#",
:small => "150x150>",
:medium => "300x300>",
:large => "500x500>" }: {}
}
Let me know if you need further explanations.
Just in case anyone is looking at this recently, we have just tried the accepted answer, and the content types did not work properly. We got it working like this:
has_attached_file :attachment, styles: lambda { |a| a.instance.attachment_content_type =~ %r(image) ? {:small => "x200>", :medium => "x300>", :large => "x400>"} : {} }
The difference is in the "column name" for the object -- typically "___content_type", instead of just "content_type"
You sure can upload images and other filled with paperclip, may be worth going to www.railscasts.com as there are quite a few related posts to your question.
I set in an initializer the save-path of paperclip to a new one and everything is fine.
But some attachment-names (like file) are very abstract:
has_attached_file :file, :styles => {
:thumb => "100x100"
}
I want this one within the new save-path, but in a different folder than 'file'. Is that possible, without changing the attachment_name?
For example: Now I'm have something like /save-path/file/thumb, but I want /save-path/my-new-file-name/thumb.
You can manipulate both path and url of the attachment(s) with the Paperclip interpolations. See https://github.com/thoughtbot/paperclip/wiki/interpolations for further reference.