使用ImageMagick在Rails中的图像上放置文本

时间:2017-11-30 23:08:19

标签: ruby-on-rails ruby imagemagick paperclip

我已经在网络应用上使用Paperclip和ImageMagick实现了图片上传,并且我尝试添加一项功能,以便用户可以添加一个简短的标题,并将其放在图像上。我试图找出一个Paperclip处理器,但这太复杂了,所以退后一步,现在尝试使用ImageMagick。这是我的模特:

class Photo < ApplicationRecord
    belongs_to :user

    has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
    validates :image, presence: true
    validates :description, presence: true
    before_save :textOnImage

    def textOnImage
        require 'rubygems'
        require 'RMagick'
        include Magick
        require "open-uri"
        img = ImageList.new(image)
        txt = Draw.new
        img.annotate(txt, 0,0,0,0, "Testing"){
            txt.gravity = Magick::SouthGravity
            txt.pointsize = 25
            txt.stroke = ‘#000000’
            txt.fill = ‘#ffffff’
            txt.font_weight = Magick::BoldWeight
        }

        img.format = ‘jpeg’
        send_data img.to_blob, :stream => ‘false’, :filename => ‘test.jpg’, :type => ‘image/jpeg’, :disposition => ‘inline’

    end
end

我已经在stackoverflow上尝试了所有相关的答案,但是我最接近解决方案的是获得此错误:未定义的方法`include&#39;对于#Photo:0x007fda7eaa87b8

我认为这是因为这是在模型中,我不能在模型中使用Include标签?有什么建议吗?

0 个答案:

没有答案