rails paperclip - error processing thumbnail - ruby-on-rails

I am running Rails 3.2.8 with Paperclip 3.3.0 on a Windows 7 machine. I am getting the following error when I try to upload an image named "2012-10-26_17.49.13.png": Image There was an error processing the thumbnail for 2012-10-26_17.49.1320121027-1196-l5ejgs
The filename may not be important, but I want to point out that most of the questions I find related to this say error processing the thumbnail for stream instead. No 'stream' on my error. The end of the filename changes randomly each time I try to upload (Paperclip doing some collision avoidance I assume).
The image field is set up in the model like this:
has_attached_file :image, :path => ":rails_root/public/images/:hash_:style.:extension", :url => "/images/:hash_:style.:extension", :hash_secret => "sometext", :default_url => "", :styles => { :thumb => "100x100#" }
validates_attachment_content_type :image, :content_type => ['image/jpeg','image/jpg','image/pjpeg','image/png','image/x-png','image/gif']
The has_attached_file is using :hash at the moment, but I also tried with :basefilename or whatever it was and that didn't affect the results. The :thumb style has been tried as "100x100>" as well.
The field is like this in _form.html.erb:
<div class="field">
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>
And the controller has nothing special in it regarding :image because Paperclip seems to take care of everything for me. At least, that's what I gathered from looking at example code.
I think it might be a problem with ImageMagick. Some of the solutions I found for this error involved the installation location of ImageMagick. I moved mine to C:/ImageMagick6, updated my system path, and added the following lines to development.rb:
Paperclip.options[:command_path] = "C:/ImageMagick6"
Paperclip.options[:swallow_stderr] = false
Paperclip.options[:whiny_thumbnails] = true
I'm not entirely sure if the latter two are needed, but I saw them suggested. Didn't notice any changes from including them. If I open a command prompt, I can access the ImageMagick commands, so the path should be correct.
I also tried to run this with the :styles removed, which allowed me to upload the original image file. I really want the thumbnails for my site, though.
So, what have I missed that is keeping this from working?

Despite having added Paperclip.options[:command_path] = 'C:/progra~2/imagem~1.0-q' to my config/environments/development.rb file, I had to add C:/progra~2/imagem~1.0-q before %SystemRoot%\system32 in the environment path.
I confirmed this by revising the environment path back and forth a few times.

In order to use Paperclip you need ImageMagick library installed.
ImageMagick provides two executables that are required by Paperclip: identify to identify the image format and convert that process the original image and generate the desired formats.
Now, ImageMagick does not come by default with Windows, so you need to install it manually.
Later, you will require to tell Paperclip where it will find the ImageMagick executables, which is all documented in Paperclip README
Please note that if you don't set Paperclip.options[:command_path] to the PATH where ImageMagick was installed, Paperclip will incorrectly attempt to use Windows' convert executable, which is a Filesystem conversion utility and not meant to process images.
I believe that is the error you're currently receiving about generating the thumbnails.
Hope that helps.

You might try using ImageMagick directly via the command line to manipulate an image. At the very least, this should confirm whether or not there's an issue with your ImageMagick installation.

Related

Rails: ckeditor gem not working in production mode with nginx and passenger

I am using the ckeditor WYSIWYG text editor in my rails project. In particular: I am having trouble configuring for image uploads in production mode.
It works fantastic in development mode, and even locally in production mode while using Puma. When I click to Upload a photo and click the Browse Server button. It immediately looks for photos where I expect them to: within /assets/ckeditor_assets/pictures.
The issue is getting it to work in production mode with nginx. When I use nginx in production mode: it returns a 404 Not Found error message. I looked at my server logs and here is what it says:
"/var/www/MYAPP/ckeditor/pictures" failed (2: No such file or directory)
So for some reason it is attempting to find a ckeditor directory within my public directory (because that is a symlink to my public directory). I am not even sure why ckeditor is looking for a ckeditor directory when it should instead be looking within my apps public/assets/ckeditor_assets directory.
I attempted to fix this by creating a ckeditor directory within my public directory, and then putting a pictures directory inside of there. However, when I did that I got a 403 Forbidden error.
I did notice that within the config/initializers/ckeditor.rb file there is this line:
# Customize ckeditor assets path
# By default: nil
#config.asset_path = "http://www.example.com/assets/ckeditor/"
So just to give it a shot I hardcoded where I wanted ckeditor to go and fetch the pictures, but unfortunately that didn't work either.
Any suggestions please let me know. Thanks! I will go ahead and show my ckeditor::picture model file in case that provides any clues:
class Ckeditor::Picture < Ckeditor::Asset
has_attached_file :data,
:url => "/assets/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:path => ":rails_root/public/assets/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:styles => { :content => '800>', :thumb => '118x100#' }
validates_attachment_presence :data
validates_attachment_size :data, :less_than => 2.megabytes
validates_attachment_content_type :data, :content_type => /\Aimage/
def url_content
url(:content)
end
end
A buddy of mine figured it out. It turns out it was simply a configuration required in the nginx.conf file. I'm using nginx and passenger.
ck_editor sends a request to ckeditor/pictures as opposed to MYAPP/ckeditor/pictures. Basically: it ignores the relative path (via passenger) of your app. I just updated the location block regex like so:
# nginx.conf
...
location ~ ^/(RELATIVE_PATH_FOR_APP|ckeditor/pictures)(/.*|$) {
...
}
An alternative is that one might be able to forgo the regex for ckeditor/pictures within the location block by overriding some of the configurations found in the ckeditor config.js file. That way you are specifying the ck_editor routing at the application level as opposed to the server level.

Images not showing - Rails 4 (using Paperclip gem)

I am trying to get images to upload as flyers for events. I do have the parameters configured in the controller. No error is being shown, but the actual image is not rendering in the view (all you see is the filename).
When I upload an image file in production (hosting with Heroku) it saves properly and I can see the file name and size in the console. I can even go to my Amazon S3 console and look at the actual image from there.
This makes it hard for me to tell what's wrong.
I have a hunch that I'm about to try out (among many other things). The only thing I think it could be is that I haven't configured the url to the location of the file in my S3 bucket, but so far I haven't found out exactly how to do that...
In case you're wondering, I do believe I have the code for the view, controller, and model written properly. But the model does make me curious. Here it is just in case:
event.rb
has_attached_file :flyer,
styles: { medium: '300x430#', thumb: '123x170#' }
validates_attachment_content_type :flyer, :content_type => /\Aimage\/.*\Z/
private
def flyer_size
if flyer.size > 5.megabytes
errors.add(:flyer, 'must be less than 5MB')
end
end
Any ideas on what's going on?
EDIT:
When I inspect element this is what it shows.
<img src= "https://s3.amazonaws.com/[bucketname]/events/flyers/000/000/008/medium/JAM.jpg?1436400492" alt="Jam" >
::before
</img>

Broken images when seeding database on heroku

I have a Rails 4 application that uses paperclip to attach photos. My db/seeds.rb file adds some photos for my Person model with lines like this:
Person.create(:first_name => 'Jon', :last_name => 'Snow',
:photo => File.open("#{Rails.root}/app/assets/images/jon-snow.png))
In my app/models/person.rb file I have the :photo as a paperclip attachment where it is cropped and resized:
class Person < ActiveRecord::Base
has_attached_file :photo,
:styles => { :medium => "256x256#", :small => "64x64#", :tiny => "24x24#" },
:default_url => :set_default_avatar,
:url => "/assets/photos/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/photos/:id/:style/:basename.:extension"
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
My problem is that when I try to deploy this to heroku (with the Cedar stack) and then seed the database, the images all come up as broken. However, on my local computer everything comes up completely fine.
The broken image tag that is generated on heroku might look something like:
<img alt="Jon Snow" src="/assets/photos/21/small/jon-snow.png?1386825683">
Does anyone know why this link would break on heroku but not on my computer? I know that heroku is generating the resized photos because the heroku console outputs things similar to what is in this post: Seed images in heroku with paperclip.
However, it is putting them into public/assets instead of in public/assets/photos/:id/:style/:basename.:extension as the controller specifies.
I've tried doing what that linked post mentions, as well as running:
heroku run rake assets:precompile
and a number of other things, but nothing works. Any help would be greatly appreciated.
The issue is the same I described in this answer How to use paperclip with rails and how does it work in deployment?
The legacy Bamboo stack had a read-only file system so you were unable to write on the file-system.
In the new Cedar stack, the file system is no longer read-only, so the upload will not fail. However, you should keep using AWS or any other external storage because Heroku distributes your compiled application across several machines, and it's not guaranteed that the image will be visible from another request. They call it ephemeral filesystem.
In other words, you should keep using AWS or any other storage outside Heroku file-system.
From heroku-dev
The filesystem for the slug is read-only, which means you cannot
dynamically write to the filesystem for semi-permanent storage. The
following types of behaviors are not supported: Caching pages in the
public directory Saving uploaded assets to local disk (e.g. with
attachment_fu or paperclip)
Use AWS S3

Paperclip won't display/detect file size of anything that is not an image

I'm working on an app that allows users to upload videos, pictures, audio clips, and documents. The files are being uploaded with the Paperclip gem and being stored on s3.
Right now I am running some processing on the file sizes after they are uploaded, but paperclip continues to save the file to the database with a file size of '0' on any file that is not .jpg, .gif, .png. I've tried inspecting the file size myself, the upload's length, and several other hair brained ideas.
Is there anyway to monkeypatch, or hijack the request from paperclip to find out what the file size is? Has anyone run into this issue before?
By default, Paperclip is built to process images. If you need to be able to handle other file types, you'll have to look into writing your own Processor: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/processor.rb. This will allow you to process files in whatever way you need when declared in your model:
has_attached_file :scan, :styles => { :text => { :quality => :better } },
:processors => [:ocr]
Here's an important point from the README docs:
NOTE: Because processors operate by turning the original attachment
into the styles, no processors will be run if there are no styles
defined.
This means you'll need to specify some sort of styles in order for your processor or any other processor to get run. For a basic custom processor something as simple as :styles => { :default => true } should be enough to get you going.

Paperclip directory pluralization problem

I've had a system thats been running fine for ages using Rails 3 & Paperclip 2.3.6 for members images.
class Image < ActiveRecord::Base
belongs_to :business
has_attached_file :data, :styles => {:normal => ["665x443#", :jpg],:thumb => ["104x50#", :jpg]}, :convert_options => {:all => "-channel RGB -strip -quality 80"}
end
Now for some reason the route for the images has changed to /system/data/ instead of previous /system/datas/
I cant figure out whats happened. All of the images still reside in /system/datas/ and a member uploaded some new images and Paperclip has put them in /system/data/
Anyone have any ideas whats happened? I cant figure it out. Thanks.
EDIT: Ok, I had a test box still on Rails 3.0.3. My Release version was on 3.0.7. If I roll back to Rails 3.0.3 it's working again - the path /system/data/ becomes /system/datas/ again.
Rails 3.0.5 introduced some additional inflections which meant 'datas' was no longer used as the plural of 'data', which broke the directory naming for Paperclip!
According to the paperclip documentation you can specify a path for saving images in your model using the :path option and providing a path.
You can find the documentation here.

Resources