【问题标题】:Paperclip don't generate styles and original image appears as broken回形针不生成样式,原始图像显示为损坏
【发布时间】:2014-01-11 07:15:04
【问题描述】:

我在这里遇到了一个问题。我真的试过了,但是我自己不能解决这个问题,所以我希望人们可以在这里帮助我。

在谈论我的问题之前,我必须说我在我的项目中使用了PaperclipIMGKit,但我认为问题在于回形针。

我创建了一个Rails Task 来从某些网站的主页上拍摄快照。前段时间一切正常,但现在一切都失败了。我将我的真实数据库从 Heroku 导入到 localhost(没有任何图像和回形针的迁移),运行迁移,从“公共/系统”中删除所有旧文件并再次运行我的任务(拍摄所有网站的快照)。

所以,现在我有:

  • 生成了路径和原始图像,但是当我尝试在 View 中加载它们时,这只是显示为损坏的图像。
  • Paperclip 不会生成:styles 的路径和转换后的图像。
  • 没有图片的网站,我可以正确看到我的默认图片。
  • ImageMagick 似乎可以正常工作,我尝试转换一些图像并像魅力一样工作。

让我们看一下代码。假设我正在运行一个任务,它将对我数据库中的所有站点执行此任务。这些网站在我的架构中被称为“项目”。

开发.rb

#config/environments/development.rb

# "which convert" give me this path
Paperclip.options[:command_path] = "/usr/local/bin/" 

Item.rb(模型)

我的任务只是调用数据库中每个站点的“object.save”,所以我的代码从 before_save 开始。

has_attached_file :image,
    :styles => { :small => "200x147#" }, 
    :convert_options => { :small => "-quality 75 -strip" },
    :default_url => '/images/:style/bitcoin-earth.jpg'

before_save :generate_data
def generate_data
    self.image = get_image(self.id, self.url_original)
end

# Take snapshot of the website
def get_image(filename, link)       
   kit = IMGKit.new(link.to_s, :quality => 100, :width => 1024, :height => 768)

   file  = Tempfile.new(["template_#{filename}", 'png'], 'tmp',
                     :encoding => 'ascii-8bit')
   
   file.write(kit.to_img(:png))
   file.flush
   return file
 end

查看

<%= image_tag store.image.url %>

宝石文件

gem "paperclip"

如果我尝试运行rake paperclip:refresh:missing_styles,任务会很快完成,没有任何错误。但如果我尝试运行rake paperclip:refresh CLASS=Item,我得到了:

Image Paperclip::Errors::NotIdentifiedByImageMagickError

是的,我已经搜索过了,但没有找到适合我的案例的解决方案。


小费?

当我在项目中“检查元素”并尝试查看项目图像的来源时,我看到了:

http://localhost:3000/public/system/items/images/000/000/216/original/template_21620140109-14507-1c0yszzpng?1389305824

但是,如果我转到我的项目文件夹,我只会看到一个名为 template_21620140109-21209-1yls03opng 的图像。请注意,那里不存在任何“?1389305824”。见上图。

嗯,我想就是这样。可能是什么问题?我真的需要解决这个问题,请帮助我:/


[2013 年 1 月 10 日编辑]

Item.rb(模型)

before_save :generate_data
def generate_data
    file = File.open(get_image(self.id, self.url_original))
    self.image = file
    file.close
end

def get_image(filename, link)
    kit = IMGKit.new(link.to_s, :quality => 100,
                                :width => 1024, :height => 768)

    file  = Tempfile.new(["template_#{filename}", '.png'], 'tmp',
                         :encoding => 'ascii-8bit')
    file.write(kit.to_img(:png))
    file.flush
    return file
end

现在我在获取图像并保存到数据库时在控制台上没有任何错误,但 Paperclip 仍然没有生成我的 :styles。当我去log/development.log时,我可以看到这个错误,但我不知道我能做些什么来解决:

Command :: file -b --mime 'tmp/template_24320140110-17577-80zj1c.png'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/template_24320140110-17577-80zj1c20140110-17577-mqa2q3.png[0]'
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>

我想我们越来越近了,请继续帮助我:)

【问题讨论】:

    标签: ruby-on-rails imagemagick paperclip imagekit


    【解决方案1】:

    我认为你的问题在这里:

    template_21620140109-14507-1c0yszzpng?1389305824 #-> should have .png (not a valid image)
    

    图片

    这可能不是问题,但也许您可以简化排除临时文件的方法:

    # Take snapshot of the website
    def get_image(filename, link)       
       kit = IMGKit.new(link.to_s, :quality => 100, :width => 1024, :height => 768)
       file = kit.to_file("system/temp/template_#{filename}")
       return file
     end
    

    我认为问题在于 ImageMagick 没有被传递“真实”文件,因此您遇到了无法识别的图像问题

    【讨论】:

    • 嗨,瑞克,非常感谢您的回复!我的代码有错误,你能帮我最终解决这个问题吗? :) 行self.image = get_image(self.id, self.url_original) 给我这个错误:closed stream。我认为 self.image 无法存储 get_image 返回的内容或类似的内容。您想要完整的错误代码吗?
    • 在 log/development.rb 我发现只有一个错误:Command :: file -b --mime '/home/paladini/Documents/Projetos/bittle/btc-stores/tmp/template_22.png'
    • 有些事情发生了变化,我会更新我的代码并告诉你新的问题。
    • 嘿费尔南多 - 请提供最新消息 - 很想听听你的进展:)
    • 嗨 Rich,我已经更新了我的代码,您可以在“编辑于...”部分下方看到它。并感谢您的关注! (:
    猜你喜欢
    • 2012-09-08
    • 2017-09-25
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2019-07-15
    • 2013-08-29
    • 1970-01-01
    • 2018-02-12
    相关资源
    最近更新 更多