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

1.1k views Asked by At

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.

I create a Rails Task to take snapshots from the home page of some sites. 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).

enter image description here

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.
  • Paperclip doesn't generate the path and converted images of :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.

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

#config/environments/development.rb

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

Item.rb (model)

My task just call "object.save" of every site in the DB, so my code starts on 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

gem "paperclip"

If I try to run rake paperclip:refresh:missing_styles, the task finish very fast without any error. But if I try to run rake paperclip:refresh CLASS=Item I got:

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

enter image description here

But if I go to my project folder, I just see a image called template_21620140109-21209-1yls03opng. Note that doesn't exist any "?1389305824" there. 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 ]

Item.rb (model):

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. When I go to log/development.log, I can see this error, but I don't know what I can do to solve:

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 :)

1

There are 1 answers

5
Richard Peck On

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