简体   繁体   English

回形针不生成样式,原始图像显示为残破

[英]Paperclip don't generate styles and original image appears as broken

I having a big issue here. 我在这里有个问题。 I really tried, but I can't solve this problem by myself, so I hope people can help me here. 我确实尝试过,但我自己无法解决此问题,因此希望人们能在这里为我提供帮助。

Before talk about my problem, I must say I'm using Paperclip and IMGKit in my Project, but I think the problem is with Paperclip. 在谈论我的问题之前,我必须说我在项目中使用了PaperclipIMGKit ,但是我认为问题出在Paperclip。

I create a Rails Task to take snapshots from the home page of some sites. 我创建了一个Rails Task ,以从某些站点的主页上拍摄快照。 Sometime ago everything is working fine, but now everything goes down. 某个时候以前一切正常,但是现在一切都变坏了。 I import my real database from Heroku to localhost (without any images and migrations of paperclip), run the migrations, delete all old files from 'public/system' and run my task again (to take snapshot of all websites). 我将真实的数据库从Heroku导入到localhost(没有任何图像和回形针的迁移),运行迁移,从“ public / system”中删除所有旧文件,然后再次运行任务(以获取所有网站的快照)。

在此处输入图片说明

So, now I have: 所以,现在我有:

  • The paths and original images are generated, but when I try to load them in View, this just show as a broken image. 路径和原始图像已生成,但是当我尝试在View中加载它们时,这只是显示为损坏的图像。
  • Paperclip doesn't generate the path and converted images of :styles . 回形针不会生成:styles的路径和转换后的图像。
  • Sites that don't have image, I can see my default image correctly. 没有图片的网站,我可以正确看到我的默认图片。
  • ImageMagick seems to be working, I try convert some images and worked like a charm. ImageMagick似乎正在工作,我尝试转换一些图像并像魅力一样工作。

Let's take a look at the code. 让我们看一下代码。 Assume that I'm running a task that will perform this task to all sites in my database. 假设我正在运行一个任务,该任务将对数据库中的所有站点执行此任务。 The sites are called "items" in my architecture. 这些站点在我的体系结构中称为“项目”。

Development.rb Development.rb

#config/environments/development.rb

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

Item.rb (model) Item.rb(模型)

My task just call "object.save" of every site in the DB, so my code starts on before_save . 我的任务只是调用数据库中每个站点的“ 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

View 视图

<%= image_tag store.image.url %>

Gemfile 的Gemfile

gem "paperclip"

If I try to run rake paperclip:refresh:missing_styles , the task finish very fast without any error. 如果我尝试运行rake paperclip:refresh:missing_styles ,任务将非常快速地完成而没有任何错误。 But if I try to run rake paperclip:refresh CLASS=Item I got: 但是如果我尝试运行rake paperclip:refresh CLASS=Item我得到了:

Image Paperclip::Errors::NotIdentifiedByImageMagickError

And yes, I already search for it and I didn't found a solution for my case. 是的,我已经进行了搜索,但没有找到适合我的案例的解决方案。


A little tip? 小提示?

When I "inspect element" in my project and try to see the source of the item image, I saw: 当我在项目中“检查元素”并尝试查看项目图像的来源时,我看到了:

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

在此处输入图片说明

But if I go to my project folder, I just see a image called template_21620140109-21209-1yls03opng . 但是,如果转到项目文件夹,我只会看到一个名为template_21620140109-21209-1yls03opng的图像。 Note that doesn't exist any "?1389305824" there. 注意那里不存在任何“?1389305824”。 See the image above. 参见上图。

Well, I think that's it. 好吧,我想就是这样。 What can be the problem? 可能是什么问题? I really need solve this issue, please, help me :/ 我真的需要解决这个问题,请帮我:/


[ Edited on Jan 10, 2013 ] [2013年1月10日编辑]

Item.rb (model) : 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

Now I don't have any error on console while getting images and saving on DB, but Paperclip still don't generate my :styles. 现在,在获取图像并保存到DB上时,控制台上没有任何错误,但是Paperclip仍然不会生成我的:styles。 When I go to log/development.log , I can see this error, but I don't know what I can do to solve: 当我进入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>

I think we're getting closer, please, keep helping me :) 我想我们越来越近了,请继续帮助我:)

I think your problem is here: 我认为您的问题在这里:

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

Image 图片

This might not be the problem, but maybe you could streamline your method to exclude the temporary file: 这可能不是问题,但是也许您可以简化方法以排除临时文件:

# 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

I think the issue is that ImageMagick is not being passed a "real" file, and consequently you're getting the unrecognized image issues 我认为问题在于ImageMagick没有传递“真实”文件,因此您遇到了无法识别的图像问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM