nul*_*tek 0 ruby ruby-on-rails imagemagick minimagick
我正在开发一个小型 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
Run Code Online (Sandbox Code Playgroud)
当我从控制台运行它并执行以下操作时:
m = Meme.new
m.process_image(Img.last.id, "Good", "Stuff")
Run Code Online (Sandbox Code Playgroud)
它生成带有正确覆盖的文本的图像。
现在,当我做同样的事情并在这样的标题中包含空格时:
m = Meme.new
m.process_image(Img.last.id, "This is", "Totally Weird")
Run Code Online (Sandbox Code Playgroud)
我在控制台中收到一个异常,如下所示:
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.
Run Code Online (Sandbox Code Playgroud)
我查看了 mini_magick 的 API 文档,但没有看到任何与空格相关的内容。我看到很多关于如何让 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}"
Run Code Online (Sandbox Code Playgroud)
引用字符串mini_magick:
# ? ? HERE
c.draw "text 200,200 '#{top_text}'"
c.fill 'white'
c.draw "text 100,100 '#{bottom_text}'"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
696 次 |
| 最近记录: |