我的應用程序具有允許上傳圖片和輸入標題的窗體。使用Rails中的回形針上傳圖片合成文本
我想將提交的標題文字動態合成到圖像上。
目前我可以上傳圖像並使用自定義Paperclip流程進行處理。這會將預定義文本複合到圖像上,如下所示:
class Caption < ActiveRecord::Base
has_attached_file :captioned_photo,
processors: [:captioner],
styles: {
captioned_photo: {
format: '.png',
caption_text: 'text to overlay on image'
}
}
belongs_to :submission
end
如何將用戶提交的文本傳遞到處理器?例如:
class Caption < ActiveRecord::Base
has_attached_file :captioned_photo,
processors: [:captioner],
styles: {
captioned_photo: {
format: '.png',
caption_text: user_uploaded_text
}
}
belongs_to :submission
end
謝謝。
UPDATE:
喇嘛確實是要走的路:
has_attached_file :captioned_photo,
processors: [:captionbitch],
styles: -> (attachment) {
{
polarized: {
format: '.png',
is_polarized: true,
the_text: attachment.instance.text
}
}
}
這使得has_attached_file
方法調用中的屬性的動態分配。 attachment.instance.text
返回名爲text的數據字段。
你使用什麼處理器?認爲開放源代碼? –
@KazimZaidi [Paperclip](https://github.com/thoughtbot/paperclip)是處理器,它已經是開源的。 – dwkns
我的意思是captionbitch處理器,如上面的代碼所示。我搜索了它,但找不到它。 –