问题
I have an application where a user chooses a template. On this template the user can upload an image.
I use paperclip to upload the images.
Each template has different image sizes.
Is it possible to set an image style => 'widthxheight' dynamically in paperclip?
I want this functionality because if the user decides to change template then they don't have to upload the photo again they just crop the 'original'.
Thanks for any help.
I will try to clear this up.
A user uploads an image for the header of a page. The style could be called "header" and the dimensions should be the dimensions for that header space, say "400x600"
Now the user views the images they have uploaded in a gallery. They want to select one of the images for their page but this time its for the "sidebar" which has dimensions "300x100". I don't want to make them upload the same image again. I want to create a new style called "sidebar" with dimensions "300x100". I also don't want to delete the "header" style or resize it.
How can I do this with paperclip?
回答1:
If I understand you have in mind something like that: Paperclip change size
Additionally:
attr_accessor :size
...
self.dimensions = self.size.split("x")
Controller:
def create
...
@file.size = params[:size] # OR Simply include such field in form
...
end
Example:
Model:
class File
has_attached_file :upload
attr_accessor :size
before_save :extract_dimensions
serialize :dimensions
...
def extract_dimensions
...
self.dimensions = self.size.split("x")
end
end
Form:
<form action="some link">
...
<label>Size: </label><select name="file_size">
<option value="100x200">Large</option>
...
</select>
</form>
来源:https://stackoverflow.com/questions/10280748/define-paperclip-style-sizes-dynamicaly