Imagehack and paperclip how to create a thumb and define path and filename? - ruby-on-rails

I am trying to create a thumb with and uploaded image. I also want to resize the uploaded image to 672x378 and the thumb is should be 219x123.
The path for the thumb should be photographer/image/:id/thumb:Filename
I have installed imageshack (gem 'Rmagick') and intstalled the program on my pc.
My model:
has_attached_file :image,
:storage => :s3,
:bucket => 'mybucket',
:path => '/photographer/image/:id/:filename',
:s3_credentials => {
:access_key_id => 'mykey',
:secret_access_key => 'mykey'
}

It looks like you want to attach images to photographers. So in that case I would include this code on the photographer model.
has_attached_file :image,
:styles => { :original => "672>x378>", :thumb => "219>x123>" }, # width x height
:storage => :s3,
:bucket => "mybucket",
:path => "photographers/:id/images/:style/:basename.:extension",
:s3_credentials => {
:access_key_id => 'mykey',
:secret_access_key => 'mykey'
}
Not that the > after the size of the images will restrict the width from exceeding the value, but still keep the image in proportion. So, the image can't go larger than the height or width. You can remove those if you want.

Related

Paperclip missing.png not showing

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
}

Paperclip + AWS S3, Prefixing remote path with my local path

I'm using Paperclip with a Rails 4 app and Amazon S3 storage. On my development machine, the site is running at
/Users/Jeff/Sites/example.com/web
When I upload a file with Paperclip to S3, the remote path in S3 inherits my local folder structure.
http://s3.amazonaws.com/example_com_bucket/Users/Jeff/Sites/example.com/web/public/assets/uploads/my_class/8/medium/some_image.png?1383060287
Why is this happening? How do I strip that part out? I tried changing the :path property but that only seemed to affect the "application" part of the path (e.g. after /assets/uploads) My site is still in development, so I don't care about having to preserve links.
My config is...
config.paperclip_defaults = {
:storage => :s3,
:path => '/:class/:attachment/:id_partition/:style/:filename',
:s3_credentials => {
:bucket => 'example_com_bucket',
:access_key_id => '...',
:secret_access_key => '...'
}
}
I had this exact same issue when I was using the :url parameter where I should have been using the :path parameter:
has_attached_file :primary_photo,
:styles => ...,
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => 'config/s3.yml',
:url => '/product/:attachment/:id/:style/:filename'
I fixed it by changing my config to this:
has_attached_file :primary_photo,
:styles => ...,
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => 'config/s3.yml',
:path => '/product/:attachment/:id/:style/:filename'

Paperclip is not saving the original file when a style is defined

I'm working to make Paperclip store a thumbnail in addition to the original image. The original image had always saved file and now I'm trying to add a thumbnail.
The problem is, once I defined the style thumbnail, the original is no longer being saved. How can I get the original to save along with the thumb?
has_mongoid_attached_file :attachment,
:storage => :s3,
:s3_credentials => "s3.yml",
:s3_protocol => 'https',
:s3_permissions => :private,
:use_timestamp => true,
:default_style => :original,
:default_url => '/images/:attachment/default_:style.png',
:path => "/:rails_env/private/:basename.:extension",
:styles => {
:thumb => "100x100#" },
:convert_options => {
:thumb => "-quality 75 -strip" }
Install Imagemagic (https://github.com/thoughtbot/paperclip) and then you can rezize your original sized photo into any size by using like this
<img src="#", size: '150x150'>

How to automate a paperclip upload to amazon s3 in rails app

I have some images that I want to move from myBucket/... to myBucket/someFolder/.... I have paperclip and aws-sdk setup to put new images in that location when uploaded on a form, but I can't figure out how to move the images to the location defined in production.rb:
config.paperclip_defaults = {
:storage => :s3,
:url => ":s3_eu_url",
:s3_protocol => "https",
:path => ":class/images/000/000/:id/:style/:basename.:extension",
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
How do you call paperclip on an image in a remote location in a controller method? I want something in my illustrationsController like the following:
def convert
#illustration = Illustration.find(params[:id])
Paperclip.someMagicMethod(#illustration.image_url)
end
Thanks.

Rails paperclip amazon aws s3 gem, how to change image url?

In my model I have:
has_attached_file :image,
:storage => :s3,
:styles => { :original => ["300x250>", :png], :small => ["165x138>", :png], :mini => ["120x120>", :png] },
:path => 'images/vind/:style/:id/:basename.:extension',
:url => 'images/vind/:style/:id/:basename.png',
:bucket => 'konkurrencerher',
:s3_credentials => {
:access_key_id => 'x',
:secret_access_key => 'x'
}
The problem is just that there is added the amazon s3 hostname to the url in view.
I have a solution to this, but is a bit ugly:
<%= image_tag(kon.photo.image.url(:small).gsub("http://s3.amazonaws.com/konkurrencerher", ""), :class => 'koni') %>
But, how is it possible to define the image url in the model, without the Amazon S3 hostname?
Take a look at Paperclip::Storage::S3, especially on the :s3_host_alias.
You can try configuring your has_attached_file with the following additional options
:url => ':s3_alias_url',
:s3_host_alias => "example.domain.net"
Hope this helps.
My solution created a file in the initializers map with this:
Paperclip.interpolates(:s3_path_url) { |attachment, style|
"#{(attachment.path).gsub("images/", "")}"
}
And then the url should be:
:url => ':s3_path_url'
This is a much better solution.

Resources