I have something like:
class User < ApplicationRecord
has_one_attached :avatar do |attachable|
attachable.variant :large, resize_to_limit: [300, nil]
attachable.variant :medium, resize_to_limit: [100, nil]
attachable.variant :small, resize_to_limit: [ 50, nil]
end
end
How do I create...
[x, y, width, height]
Is it possible to "pass the coordinates to the model" somehow? If yes, how?
If not: Would the cropping need to happen separately in a controller action, creating a cropped version (of the original file), based upon which the "named variants" would be created? If so, how would that look?
I'll try to answer your question:
user.avatar.variant(:large)
. If done in the view append .processed
to retrieve the already generated variant if present.resize_to_limit: [300, 100], crop: '200x300+0+0'
If you want to get dimensions and coordinates from params then i would not use named variant, but something like
In model: has_one_attached :avatar
In controller:
def image_height
params[:height]
end
def generate_variant_small
@user.avatar.variant(resize_to_limit: "300x#{image_height}^", crop: '200x300+0+0').processed
end
This was added to Rails 7 ( https://github.com/rails/rails/pull/39135 ):
class User < ActiveRecord::Base
has_one_attached :avatar, variants: {
thumb: { resize: "100x100" },
medium: { resize: "300x300", monochrome: true }
}
end
class Gallery < ActiveRecord::Base
has_many_attached :photos, variants: {
thumb: { resize: "100x100" },
medium: { resize: "300x300", monochrome: true }
}
end
<%= image_tag user.avatar.variant(:thumb) %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.