【问题标题】:rails 3 paperclip processor watermark error in filename文件名中的rails 3回形针处理器水印错误
【发布时间】:2012-11-26 01:37:10
【问题描述】:

rails 3 中的回形针有问题。当我上传文件时,我的处理器抛出错误,因为 imagemagick get 命令:

“复合-重力南 /home/xxx/xxx/public/images/watermark.png /tmp/a s20121207-5819-1dq7y81.jpg /tmp/a s20121207-5819-1dq7y8120121207-5819-1juqw7a”

复合:无法打开图像`/tmp/a':

处理器:

def make
  dst = Tempfile.new([@basename, @format].compact.join("."))
  dst.binmode

  if @watermark_path
    command = "composite"
    params = "-gravity #{@position} #{@watermark_path} #{fromfile} "
    params += tofile(dst)
    begin
      p " >>>>>>>>>>>>>>>>> #{command} #{params}"
      success = Paperclip.run(command, params)
    rescue PaperclipCommandLineError
      success = false
    end
    unless success
      raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny
    end
    return dst
  else
    return @file
  end
end

def fromfile
  File.expand_path(@file.path)
end

def tofile(destination)
  File.expand_path(destination.path)
end

它仅在文件名包含空格或其他非 alfanum 字符时发生。 /tmp/a 应该是 /tmp/a b.jpg。

我尝试了http://www.davesouth.org/stories/make-url-friendly-filenames-in-paperclip-attachments 等等,但处理器中的文件名仍然错误

有什么想法吗?或适用于这个问题的处理器? :(

【问题讨论】:

  • 你应该从文件名中删除空格...
  • 我已经这样做了,但它不起作用我认为回形针会在删除空格之前创建 tmp 文件
  • 但是你自己创建临时文件?? dst = Tempfile.new([@basename, @format].compact.join("."))
  • 不,这个文件传入构造函数:def 初始化文件,选项 = {},附件 = nil super
  • 这个问题好运吗?我有完全相同的问题。我正在查看源代码,但我只是找不到如何从@file.path 中删除非法字符。我只是在 photo_file_name 上有一个 validate_format_of 来安定下来,这并不是这个问题的直接解决方案。

标签: ruby ruby-on-rails-3 paperclip


【解决方案1】:

不妨试试:

dst = Tempfile.new([@basename, @format].compact.join(".").gsub(" ","")

【讨论】:

  • thx 但是错误的文件名也在 @file 中,它是在处理器初始化程序中传递的,我不知道在创建水印实例之前我可以在哪里更改这个“文件”
【解决方案2】:

我今天回过头来发现你只需要像 ImageMagic 文档 (http://www.imagemagick.org/script/command-line-processing.php) 中那样用引号将路径括起来:

If the image path includes one or more spaces, enclose the path in quotes:

'my title.jpg'

例如,在我的处理器中,我有:

command = "convert \"#{File.expand_path(@file.path)}\" -crop #{crop_area} -resize 150x150 \"#{File.expand_path(destination.path)}\""

Paperclip.run(command)

.

我目前在 Windows 上,单引号似乎不起作用。 和你一样,我尝试更改 @file 的属性但没有成功。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多