动态定义回形针样式大小 [英] Define paperclip style sizes dynamicaly
问题描述
我有一个应用程序,用户可以在其中选择模板.在此模板上,用户可以上传图像.
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.
是否可以在回形针中动态设置图像样式 => 'widthxheight'?
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'.
感谢您的帮助.
我会努力解决这个问题.
I will try to clear this up.
用户上传了页面标题的图片.样式可以称为标题",尺寸应该是该标题空间的尺寸,例如400x600"
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"
现在用户查看他们上传到图库中的图像.他们想为他们的页面选择一张图片,但这次是为尺寸为300x100"的侧边栏"选择一张图片.我不想让他们再次上传相同的图片.我想创建一个名为侧边栏"的新样式,尺寸为300x100".我也不想删除标题"样式或调整它的大小.
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?
推荐答案
如果我知道你有这样的想法:回形针改变大小
If I understand you have in mind something like that: Paperclip change size
另外:
attr_accessor :size
...
self.dimensions = self.size.split("x")
控制器:
def create
...
@file.size = params[:size] # OR Simply include such field in form
...
end
示例:
型号:
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 action="some link">
...
<label>Size: </label><select name="file_size">
<option value="100x200">Large</option>
...
</select>
</form>
这篇关于动态定义回形针样式大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!