My model looks like:
class Photo < ApplicationRecord
has_attached_file :image,
:styles => { :small => "50x50#" },
:default_style => :small
validates_attachment :image,
content_type: { content_type: ["image/jpeg",
"image/gif", "image/png"] }
end
RAILS stores the image twice: as original size, and as resized defined in :small. I would like to store only resized image.
I believe that you can simply define a style for :original to have paperclip replace the original with that size.
:styles => { :original => '300x168>', :cropped_thumb => {:geometry => "50x50#", :jcrop => true}, ...}
Thank you, puneet18.
This model do the job:
class Photo < ApplicationRecord
has_attached_file :image,
:styles => { :original => "50x50#" },
:default_style => :original
validates_attachment :image,
content_type: { content_type: ["image/jpeg", "image/gif",
"image/png"] }
end
Related
Getting error on expecting key word
class Listing < ActiveRecord::Base
if Rails.env.development?
has_attached_file :image, :styles => { :medium => "200", :thumb => "100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
else
has_attached_file :image, :styles => { :medium => "200", :thumb => "100x100>" }, :default_url => "default.jpg",
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
:storage => :dropbox,
:dropbox_credentials => Rails.root.join("config/dropbox.yml"),
:path => ":style/:id_filename"
end
validates :name, :description, :price, :address, :phone, presence: true
validates :price, numericality: { greater_than: 0}
validates :phone, length: { maximum: 14 }
validates_attachment_presence :image
belongs_to :user
end
Extra comma at the end of line
has_attached_file :image, :styles => { :medium => "200", :thumb => "100x100>" }, :default_url => "default.jpg",
without a comma at the end of line
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
Hope this might help you .....
class Listing < ActiveRecord::Base
if Rails.env.development?
has_attached_file :image, :styles => {:medium => "200", :thumb => "100x100>"}, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
else
has_attached_file :image, :styles => {:medium => "200", :thumb => "100x100>"}, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/,
:storage => :dropbox, :dropbox_credentials => Rails.root.join("config/dropbox.yml"), :path => ":style/:id_filename"
end
validates :name, :description, :price, :address, :phone, presence: true
validates :price, numericality: {greater_than: 0}
validates :phone, length: {maximum: 14}
validates_attachment_presence :image
belongs_to :user
end
When I try to upload a video using Paperclip, I get an error message can't dump File.
Model Video :
class Video < ActiveRecord::Base
has_attached_file :avatar,
:storage => :s3,
:styles => {
:mp4 => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "300x300>", :format => 'jpg', :time => 5 }
}, :processors => [:ffmpeg]
validates_attachment_presence :avatar
validates_attachment_content_type :avatar,
:content_type => /video/,
:message => "Video not supported"
end
I am running Ruby on Rails 3 and I would like to reduce the quality of an uploading image using the Paperclip plugin/gem. How can I do that?
At this time in my model file I have:
has_attached_file :avatar,
:styles => {
:thumb => ["50x50#", :jpg],
:medium => ["250x250#", :jpg],
:original => ["600x600#", :jpg] }
that will convert images in to the .jpg format and will set dimensions.
Try using convert_options.
has_attached_file :avatar,
:styles => { :thumb => '50x50#' },
:convert_options => { :thumb => '-quality 80' }
From the paperclip wiki, there's an option for quality:
class User < ActiveRecord::Base
has_attached_file :photo,
:styles => {
:small => {
:geometry => '38x38#',
:quality => 40,
:format => 'JPG'
},
:medium => {
:geometry => '92x92#',
:quality => 50
}
end
As James says, once you figure out the correct arguments to pass to ImageMagick's convert by experimenting on the command line, you can pass these in to Paperclip through the convert_options option as in James' example.
If you have multiple arguments, pass them in as an array. Here's an example which I laboured over for a while:
:convert_options => {:medium => ["-shave", "2x2", "-background", "white",
"-gravity", "center", "-extent",
"530x322", "+repage"],
:small => ["-shave", "1x1"] }
Except -quality, the -strip option of ImageMagick can remove all profile and other fluff from the image which may reduce more size
has_attached_file :photo,
:styles => {
:thumb => "100x100#" },
:convert_options => {
:thumb => "-quality 75 -strip" }
How can I do something inside the model depending on the whether it's creating or updating a record? I'm sure it's really simple, but I can't seem to figure it out.
These are different styles for updating or creating attachments with paperclip.
class Photo < ActiveRecord::Base
belongs_to :product
#after_upate :flag_somthin
Paperclip.interpolates :product_id do |attachment, style|
attachment.instance.product_id
end
has_attached_file :data,
:storage => 's3',
:s3_credentials => "#{RAILS_ROOT}/config/s3_credentials.yml",
:bucket => 'leatherarts.com',
:s3_host_alias => 'leatherarts.com.s3.amazonaws.com',
:url => ':s3_alias_url',
:path => "images/products/:product_id/:style/:basename.:extension",
:styles => lambda { |style| style.instance.choose_styles },
:default_style => :medium,
:default_url => 'http://leatherarts.com.s3.amazonaws.com/images/records/m1.png',
:s3_headers => { 'Expires' => 2.months.from_now.httpdate }
validates_attachment_presence :data
validates_attachment_size :data, :less_than => 10.megabytes
validates_attachment_content_type :data, :content_type => ['image/jpeg','image/gif','image/png']
def choose_styles
{ :thumb => "60x60#", :small => "200x200>", :medium => "400x400>", :large => "1000x1000>", :backup => "2000x2000>" }, :on => :create
{ :thumb => "60x60#", :small => "200x200>", :medium => "400x400>", :large => "1000x1000>" }, :on => :update
end
end
Use the new_record? method to return different hashes:
def choose_styles
defaults = { :thumb => "60x60#", :small => "200x200>", :medium => "400x400>", :large => "1000x1000>" }
defaults.merge! :backup => "2000x2000>" if new_record?
defaults
end
I'm using paperclip to upload all sorts of files (text documents, binaries, images).
I'd like to put this in my model:
has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }
but it has to perform the styles only if it's an image. I tried adding
if :attachment_content_type =~ /^image/
but it didn't work.
You can use before_<attachment>_post_process callback to halt thumbnail generation for non-images. If you return false in callback, there will be no attempts to use styles.
See "Events" section in docs
before_attachment_post_process :allow_only_images
def allow_only_images
if !(attachment.content_type =~ %r{^(image|(x-)?application)/(x-png|pjpeg|jpeg|jpg|png|gif)$})
return false
end
end
May be you need something like this:
:styles => lambda { |attachment|
!attachment.instance.image? ? {} : {:thumb => "80x24", :preview => "800x600>"}
}
And define method in your model:
def image?
attachment.content_type.index("image/") == 0
end
You can use on your model
`has_attached_file :avatar,
:styles => lambda { |a| if a.content_type =~ %r{^(image|(x-)?application)/(x-png|pjpeg|jpeg|jpg|png|gif)$}
{
:thumb => "100x100#",
:medium => "300x300>",
}
else
Hash.new
end
},:default_url => "/missing.png"`