ruby-on-rails - 如何使用 rmagick 将动态文本附加到图像?

标签 ruby-on-rails image imagemagick rmagick

我正在构建一个 Rails 应用程序,用户可以在其中输入关于自己的六个词并上传图像。我想将这六个词附加到图像中。

我知道如何使用“静态”文本使文本附加工作,但我不知道如何将文本字段链接到用户在输入六个单词时填写的表单。

现在,“文本”是静态的,无论我放什么。

我想知道为什么以下不起作用?

process :textify => <%= @user.sixwords %> 

这是我所拥有的:
process :textify => 'Text'

  def textify(phrase)
    manipulate! do |img|
    img = img.sepiatone

  title = Magick::Draw.new

  title.annotate(img, 0,0,0,40, phrase) {
    self.font_family = 'Helvetica'
    self.fill = 'white'
    self.pointsize = 32  
  }

  img = img.write('newimg.gif')
 end
end

感谢您帮助我成为一个少一点的 n00b! :)

最佳答案

在 friend 的帮助下解决了这个问题。

这就是我需要改变的全部:

title.annotate(img, 0,0,0,40, model.name) {
    self.font_family = 'Helvetica'
    self.fill = 'white'
    self.pointsize = 32  
  }

这是有效的,因为 Carrierwave 使用 model作为指向上传器附加到的任何实例对象的变量。在我的模型中,name是接受用户文本输入的表单域的名称。

对于其他用例,您只需使用 model.field_name你把你给的任何名字都放在哪里 field_name .

我的 friend 这样写给我解释的。我把它贴在这里以防它可以帮助其他人。

The 'process' method in ImageUploader is a static class method which only gets called once when the file is loaded into memory and interpreted by ruby. It's basically just internally keeping track of which processing methods should be called later on for every instance of ImageUploader. The textify method is the instance method that gets called on each ImageUploader object instance that gets created. The 'mount_uploader' class method that you call in your Athlete model is doing some ruby magic to instantiate the ImageUploader instance and pass your athlete instance object to it, making it available to the ImageUploader object instance as an instance method called 'model'.

关于ruby-on-rails - 如何使用 rmagick 将动态文本附加到图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12681642/

相关文章:

ruby-on-rails - 向 Ruby JSON 对象添加值

mysql - mysql 中的 ActiveRecord DATETIME 列值

javascript - 远程表格 - XLSX 下载

ruby - Mac Rmagick 无法与 Xcode 4.2 一起安装

c++ - 如何将多页 Magick++ 图像转换为 QImage?

ruby-on-rails - Bundler 找不到 gem "actionpack": 的兼容版本

python - 如何将圆弧延伸成完整的圆?

image - 更改单个像素的颜色 - Golang 图像

PHP 和 MySQL。想要显示目录的第一张图像。数据库的目录路径

Imagemagick:如何减小具有大静态区域的动画 GIF 的大小?