1

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?

chell
  • 7,646
  • 16
  • 74
  • 140
  • I think [this other thread](http://stackoverflow.com/questions/4029480/paperclip-attachments-with-dynamic-style-sizes-from-model) could be of your interest – Christian Apr 26 '12 at 16:57
  • Christian thanks very much that really helps me. – chell May 06 '12 at 13:01

1 Answers1

6

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>
Eraden
  • 2,818
  • 1
  • 19
  • 17
  • Thanks Eraden for your answer. I want to be able to save a different style name with different dimensions. The first time there might be a style called "header". So for the dimensions of the header this style is saved by paperclip. IF the user selects this image from an image gallery but wants the dimensions to be different then I want to save a different style with different name and dimensions, say "footer" with dimensions "50x50". That is what I want to do. – chell May 01 '12 at 09:11
  • Dimension save by Dimension model, image save using Paperclip. Using this schema U can set value for option in select field as dimension id||dimension name [by server side Dimension.find_by_name(...) ] or create new one. When U add new image save him as basic (U may add field type in database or create tree table type [parent_id]). When user transform image to diffrent type just create new entry with type dimension_id. This way you can check if there is a picture of that type (if so get its ID only) or create a new entry. – Eraden May 02 '12 at 09:58
  • Changes: remove `attr_accessor :size` add `belongs_to :dimension` add model Dimension replace `self.dimensions = self.size.split("x")` by `self.dimensions = self.dimension.size.split("x")` – Eraden May 02 '12 at 09:59
  • If this answer is not what U expect please send me more information on e-mail because I really don't understand what U want do... My e-mail => adrian.wozniak.1986@gmail.com – Eraden May 02 '12 at 10:08
  • Thank Eraden I think the information you have provided me is pointing me in the right direction. Thanks for your patience and details. – chell May 06 '12 at 13:01