Define paperclip style sizes dynamicaly

泄露秘密 提交于 2019-12-11 07:14:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!