使用 mini_magick 和 ruby 编写带空格的文本
Writing text with spaces using mini_magick and ruby
我正在开发一个小型 rails 5.0.0.1 应用程序,它可以在类似于模因的图像之上生成文本。我在模型中编写了基本代码。
class Meme < ApplicationRecord
require 'mini_magick'
mount_uploader :attachment, AttachmentUploader
def process_image(img_id, top_text, bottom_text)
image = MiniMagick::Image.open(Img.find(img_id).attachment.current_path)
image.combine_options do |c|
c.gravity 'Center'
c.pointsize '22'
c.draw "text 200,200 #{top_text}"
c.fill 'white'
c.draw "text 100,100 #{bottom_text}"
c.fill 'white'
end
self.attachment = image
self.save
end
end
当我从控制台 运行 执行以下操作时:
m = Meme.new
m.process_image(Img.last.id, "Good", "Stuff")
它会生成正确覆盖文本的图像。
现在,当我做同样的事情并在标题中包含空格时,如下所示:
m = Meme.new
m.process_image(Img.last.id, "This is", "Totally Weird")
我在控制台中收到异常,如下所示:
mogrify: non-conforming drawing primitive definition `is' @ error/draw.c/DrawImage/3259.
mogrify: non-conforming drawing primitive definition `Weird' @ error/draw.c/DrawImage/3259.
mogrify: non-conforming drawing primitive definition `is' @ error/draw.c/DrawImage/3259.
mogrify: non-conforming drawing primitive definition `Weird' @ error/draw.c/DrawImage/3259.
我查看了 API 的 mini_magick 文档,但没有看到任何与空格相关的内容。我看到大量链接讨论如何让 ImageMagick 核心正确注入空格但不使用 mini_magick 包装器。
我是不是遗漏了什么或者我应该对空格进行某种替换?
空格很重要:
# on "This is" input is becomes
# ⇓⇓ mini_magick does not expect that
# c.draw "text 200,200 This is"
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ HERE
c.draw "text 200,200 #{top_text}"
c.fill 'white'
c.draw "text 100,100 #{bottom_text}"
引用 mini_magick
的字符串:
# ⇓ ⇓ HERE
c.draw "text 200,200 '#{top_text}'"
c.fill 'white'
c.draw "text 100,100 '#{bottom_text}'"
我正在开发一个小型 rails 5.0.0.1 应用程序,它可以在类似于模因的图像之上生成文本。我在模型中编写了基本代码。
class Meme < ApplicationRecord
require 'mini_magick'
mount_uploader :attachment, AttachmentUploader
def process_image(img_id, top_text, bottom_text)
image = MiniMagick::Image.open(Img.find(img_id).attachment.current_path)
image.combine_options do |c|
c.gravity 'Center'
c.pointsize '22'
c.draw "text 200,200 #{top_text}"
c.fill 'white'
c.draw "text 100,100 #{bottom_text}"
c.fill 'white'
end
self.attachment = image
self.save
end
end
当我从控制台 运行 执行以下操作时:
m = Meme.new
m.process_image(Img.last.id, "Good", "Stuff")
它会生成正确覆盖文本的图像。
现在,当我做同样的事情并在标题中包含空格时,如下所示:
m = Meme.new
m.process_image(Img.last.id, "This is", "Totally Weird")
我在控制台中收到异常,如下所示:
mogrify: non-conforming drawing primitive definition `is' @ error/draw.c/DrawImage/3259.
mogrify: non-conforming drawing primitive definition `Weird' @ error/draw.c/DrawImage/3259.
mogrify: non-conforming drawing primitive definition `is' @ error/draw.c/DrawImage/3259.
mogrify: non-conforming drawing primitive definition `Weird' @ error/draw.c/DrawImage/3259.
我查看了 API 的 mini_magick 文档,但没有看到任何与空格相关的内容。我看到大量链接讨论如何让 ImageMagick 核心正确注入空格但不使用 mini_magick 包装器。
我是不是遗漏了什么或者我应该对空格进行某种替换?
空格很重要:
# on "This is" input is becomes
# ⇓⇓ mini_magick does not expect that
# c.draw "text 200,200 This is"
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ HERE
c.draw "text 200,200 #{top_text}"
c.fill 'white'
c.draw "text 100,100 #{bottom_text}"
引用 mini_magick
的字符串:
# ⇓ ⇓ HERE
c.draw "text 200,200 '#{top_text}'"
c.fill 'white'
c.draw "text 100,100 '#{bottom_text}'"