So what I'm trying to do is have a centrally cropped image for the thumbnail and then also have the original image untouched so I can click on the thumbnail and bring the user to the original image.
So inside my model I have:
has_attached_file :image,
default_url: "/assets/missing.png",
url: "s3_domain_url",
storage: :s3,
s3_credentials: "#{Rails.root}/config/aws-#{Rails.env}.yml",
s3_permissions: 'authenticated-read',
path: "/users/:id/images/:basename.:extension",
s3_server_side_encryption: :aes256,
styles: { thumb: "200x200#" }
def s3_url(style = nil, expires_in = 30.seconds)
if image.exists?
image.s3_object(style).url_for(:read, :secure => true, :expires => expires_in).to_s
else
"/assets/missing.png"
end
end
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
And then inside my HTML I've got: <%= link_to image_tag(#challenge.s3_url(:thumb)), #challenge.s3_url(:original) %>
This should allow for the user to click the thumbnail and then it should take them to the original image. But currently it has the correct cropped thumbnail and then when the user clicks it, it will take them to just a larger version of the cropped thumbnail, not the original image.
Is there any way to do what I want?
The issue is the same as this SO question. Basically, needed to add a :style into the path. Now it works correctly.
So it now looks like:
has_attached_file :image,
default_url: "/assets/missing.png",
url: "s3_domain_url",
storage: :s3,
s3_credentials: "#{Rails.root}/config/aws-#{Rails.env}.yml",
s3_permissions: 'authenticated-read',
path: "/users/:id/images/:style/:basename.:extension",
s3_server_side_encryption: :aes256,
styles: { thumb: "200x200#" }
Related
I uploaded images to Amazon S3, but they're not displaying.
Image can upload but cannot view them.
I am using rails5.2 , ruby 2.4
where i get the solution for this ?
Model.rb
has_attached_file :avatar,
styles: { medium: '400x400#' },
storage: :s3,
s3_credentials: "#{Rails.root}/config/s3.yml",
url: ":s3_domain_url",
path: "/uploaded_data/:class/:id/:basename.:extension",
s3_protocol: 'https'
view
<%= image_tag #product.avatar.url %>
I had a model from which I used paperclip and stored to S3.
In paperclip.rb
bucket_name = (Rails.env != 'production') ? "mcds_staging_fulltext" : 'mcds_fulltext'
Paperclip::Attachment.default_options.merge!({
storage: :s3,
s3_credentials: {
bucket: bucket_name
},
url: "#{CUSTOMER}/static_cover_images/:style/:basename.:extension",
path: "#{CUSTOMER}/static_cover_images/:style/:basename.:extension"
})
model1.rb
has_attached_file :cover_image, styles: { :original => ["100%"], :thumbnail => ["100*100", :png] }
validates_attachment_content_type :cover_image, content_type: /\Aimage\/.*\Z/, message: 'Invalid Content Type. Please upload jpg/jpeg/png/gif'
validates_attachment_size :cover_image, :in => 0.megabytes..5.megabytes, :message => 'must be smaller than 5 MB'
this works poperly and store my images in S3 in correct location.
Now I have another model from where I need to upload a paperclip attachment to a different S3 Location.
In model2.rb
has_attached_file :xslt
Paperclip::Attachment.default_options.merge!({url: "#{CUSTOMER}/xslts/:style/:basename.:extension"})
validates_attachment_content_type :xslt, content_type: "application/xslt+xml", message: 'Invalid Content Type. Please upload jpg/jpeg/png/gif'
validates_attachment_size :xslt, :in => 0.megabytes..5.megabytes, :message => 'must be smaller than 5 MB'
but this attachment still stores to my model1 S3 and not in the url specified in model2.
What am I doing wrong?
I completely removed paper_clip.rb file and added paper_clip configurations in therespective models.
model1:
has_attached_file :file1,
storage: :s3,
s3_credentials: {
bucket: bucket_name
},
url: "#{CUSTOMER}/cover_images/:style/:basename.:extension",
path: "#{CUSTOMER}/cover_images/:style/:basename.:extension"
model2:
has_attached_file :file2,
storage: :s3,
s3_credentials: {
bucket: bucket_name
},
url: "#{CUSTOMER}/file2_folder/:style/:basename.:extension",
path: "#{CUSTOMER}/file2_folder/:style/:basename.:extension"
this stores theattachment correctly in differen S3 folders.
In my member model I have a default url when no image is uploaded:
default_url: "/images/:style/missing.png"
in the assets/images folder I created a 'medium' folder and stored an image 'missing.png'
However, when I created a new member object and not upload an image, the webpage doesn't show the missing.png
Am I missing something?
I also tried the solution here and changed the default setting to:
default_url: "/:style/missing.png"
but that didn't work.
Help is much appreciated
You can use the below working code. It's working fine in my case.
has_attached_file :content, styles: { thumb: "120x120>", medium: "160x226" }, default_url: "/assets/noimage/:style/missing.png",
:storage => :s3,
:s3_protocol => 'https',
:s3_host_name => Settings.aws.s3.host_name,
:s3_credentials => {
:bucket => Settings.aws.s3.bucket,
:access_key_id => Settings.aws.access_key_id,
:secret_access_key => Settings.aws.secret_access_key
}
I have a Rails application that uses Paperclip and saves images to S3. When the user uploads an asset without an image, it gets the default image set in the Paperclip setup.
My API serves those assets and has the links to the images in the JSON response (using jbuilder), however I can't seem to return the default image URL, it only returns "missing.png" and I wanted it to return the entire URL to the server with the missing image path attached to it.
I'm setting the default url in the model, and I've tried using ActionView::Helpers::AssetUrlHelper to get the image_url but it never works even though it is working inside the rails console. Any idea on what can I do to solve it?
The JBuilder file:
json.profile_picture_smallest asset.profile_picture.url(:smallest)
json.profile_picture_small asset.profile_picture.url(:small)
json.profile_picture_medium asset.profile_picture.url(:medium)
json.profile_picture_large asset.profile_picture.url(:large)
json.profile_picture_original asset.profile_picture.url(:original)
The part of paperclip that is included in the Model
module Picturable
extend ActiveSupport::Concern
included do
has_attached_file :profile_picture, path: '/images/' + name.downcase.pluralize + '/:style/:basename', default_url: "missing.png",
styles: {
smallest: '50x50>',
small: '100x100>',
medium: '200x200>',
large: '400x400>',
png: ['400x400>',:png]
}, :convert_options => {
smallest: '-trim',
small: '-trim',
medium: '-trim',
large: '-trim',
png: '-trim'
}
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/
end
def set_uuid_name
begin
self.profile_picture_file_name = SecureRandom.uuid
end while self.class.find_by(:profile_picture_file_name => self.profile_picture_file_name)
end
end
Paperclip config:
Paperclip::Attachment.default_options[:s3_host_name] = 's3hostname'
Development config:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => 'paperclipdev',
:access_key_id => 'accesskey',
:secret_access_key => 'secretaccesskey'
}
}
I think the way to do this is use the asset helpers in your jbuilder file:
json.profile_picture_smallest asset_url(asset.profile_picture.url(:smallest))
It's worth a mention here that you can also pass a symbol method name to paperclip for the default_url parameter if you want the default url to be dynamic based on the model.
I'm using paperclip gem for uploading and resizing images. This setup works fine. I'm able to display the uploaded images. The problem comes when I try to resize the uploaded image.
Here is snippet from the model file
has_attached_file :photo,
:size => {:small => "150x150>"}
When I try to upload the image I get this error.
Photo /var/folders/gm/gm-SegRMHuOkSlYtTMkO8U+++TI/-Tmp-/file.jpg is not recognized by the 'identify' command.
I'm sure that the file is jpg. Here is the output of the file command
file.jpg: JPEG image data, JFIF standard 1.01, comment: "CREATOR: gd-jpeg v1.0 (using IJ"
I'm not sure but in our application we do the same thing and it works. Our code looks like this:
has_attached_file :image,
:styles => {:small => "280x173#", :medium => "635x393#"},
:convert_options => {:all => "-quality 80"},#,
:default_style => :medium,
:default_url => "/images/study/nophoto.jpg"
validates_attachment_size :image, :less_than => 10.megabyte
validates_attachment_content_type :image, :content_type => ['image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg']
The difference I see, is that you might have to provide convert_options to be able to resize.
Have you tried any other jpg file, maybe with a simpler path also?