我正在使用https://github.com/dignoe/graphicsmagick
中的这个GraphicsMagick包装器它可以工作,但是当我添加任何符号时,ruby会添加一个反斜杠,这会在运行命令时导致错误。我该如何防止这种情况?
代码:
img.crop('360x504+432+72').resize('125x177!').write("public/#{path}/xs-" + filename)
生成错误消息:
GraphicsMagick::UnknownOptionError (gm mogrify -crop 360x504\+432\+72 -resize 125x177\! public//media/xs-cccc.JPG failed: gm mogrify: Option '-crop' requires an argument or argument is malformed.
):
答案 0 :(得分:1)
我可能应该在一开始就猜到你的问题是Windows。 Windows总是很有趣。
Ruby的Shellwords模块,the graphicsmagick gem uses,不适用于Windows(per the docs,它“根据UNIX Bourne shell的单词解析规则操纵字符串” - 这是一个{{ 3}}打开它。)
假设我无法说服您切换到更适合Ruby开发的操作系统,我能为您提供的最佳操作就是黑客攻击。使用long-standing issue更改Shellwords.escape
的行为以从某些字符中删除反斜杠:
require "shellwords"
module UglyShellwordsHack
def escape(*args)
super.gsub(/\\([+^])/, '\1')
end
end
Shellwords.singleton_class.prepend(UglyShellwordsHack)
puts Shellwords.escape("360x504+432+72")
# => 360x504+432+72
当然,就像所有的黑客一样,这一切都有可能在将来的某些时候打破。
P.S。您应该在Module#prepend
中提及您使用的是Windows。