Paperclip initializes styles before form parameters are processed, thereby ignoring my attempt to specify a custom resize.
Model:
attr_accessor :new_width, :new_height
has_attached_file :attachment,
styles: lambda { |attachment| attachment.instance.set_styles }
...
def set_styles
# Default thumb:
styles = { thumb: '100x100>' }
# Resize original if new sizes have been specified
if new_width and new_height
styles[:original] = "#{new_width}x#{new_height}>"
end
styles
end
I can see through the log files that styles are defined and the convert command triggers before new_width and new_height are assigned the values passed by the controller.
Rails server log file:
{:thumb=>"100x100>"} # logger.info in the model
...
# Command :: convert ... etc
# [paperclip] Saving attachments.
...
{:thumb=>"100x100>", :original=>"300x300>"} # logger.info in the model
By the time the custom dimensions are assigned to the instance, the ImageMagick command has already been triggered by Paperclip.
How can I pass custom dimensions to Paperclip to define the styles before the image is processed?
Have you tried using a callback in your model to calculate the width and height before saving the model?
From the paperclip documentation on attachments, the paperclip attachment saves when the model saves and process the file upon assignment.