2017-03-06 40 views
0

添加一個反斜槓符號我用這GraphicsMagick工具包裝從https://github.com/dignoe/graphicsmagick的Rails上的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

我不認爲反斜槓是這裏的問題('^'通常需要在shell命令中轉義)。我正在查看[GraphicsMagick文檔](http://www.graphicsmagick.org/GraphicsMagick.html),它看起來不像'^'對'-thumbnail'選項有效。這是根據文檔的有效格式:'-thumbnail x {%} {@} {!} {<}{>}'。 –

+0

我從這裏生成裁剪縮略圖解決方案中得到的那部分:http://superuser.com/questions/275476/square-thumbnails-with-imagemagick-convert –

+0

這個問題是關於ImageMagick,而不是GraphicsMagick。 –

回答

1

我也許應該在,你的問題是Windows中的一開始就猜到了。 Windows總是很有趣。

Ruby的Shellwords模塊,the graphicsmagick gem uses,不是設計與Windows的工作(per the docs,它-there是一個long-standing issue打開它「根據UNIX伯恩的字解析規則殼操縱字符串」)。

假設我無法說服你切換到一個更適合Ruby開發的操作系統,我能給你提供的最好的方法就是黑客攻擊。使用Module#prepend改變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.您應該在您的graphicsmagic issue中提到您使用的是Windows。

+0

謝謝。我讚賞你爲這個問題付出的時間和努力。我已經更新了這個問題。 –