I use carrierwave and mini_magick to upload images. In development everything is fine, but in production it raises FloatDomainError (Infinity) when i try to upload an image. I have several projects hosted at the same server and everything is fine with uploading.
I use Rails 3.0.10.
Any ideas how can i fix it? Thanks
I had the same problem. The problem is mini_magick. If the image file it runs identify on is erroneous, identify will output some kind of error, e.g.
identify: Corrupt JPEG data: 7929 extraneous bytes before marker 0xed `image.jpg' # warning/jpeg.c/EmitMessage/230.
11811 8665
mini_magick tries to parse the error message as the dimension, and the result is 0. This results in a division by zero which results in the exception you mentioned. This is the reason why it only fails with some images.
identify has a -quiet options to turn off these warning messages. I have forked mini_magick at https://github.com/fschwahn/mini_magick and added the quiet option. I hope this change will be pulled in (or the problem will be fixed in a more elegant way). However, for now you can use my fork by adding the following to your Gemfile:
gem 'mini_magick', :git => 'git://github.com/fschwahn/mini_magick.git'
Fixed that with replacing resize_and_fill to resize_and_pad. Still don't understand its strange behavior.
I was using the Ubuntu Imagemagick package version 6.7. I upgraded to 6.8 following the instructions here: https://askubuntu.com/questions/267746/how-can-i-install-the-latest-upstream-version-of-imagemagick-without-compiling and it worked.
I got this error with the newest gem update, when I generated an image thumbnail for my pdf file.
This code fails:
version :thumb do
process :resize_to_fill => [260, 192]
process :convert => :png
process :set_content_type
process :thumbnail_pdf
end
I solved it by replacing the order of the lines. The key was that before resizing MiniMagic should first convert thumbnail to image and after that should try to resize.
Here is solution which worked for me. Maybe it'll help for someone.
process :convert => :png
process :resize_to_fill => [260, 192]
Related
I can't get ActiveStorage images to work in production. I want to use a resized image (variant) within the body of the PDF I'm generating.
= image_tag(#post.image.variant(resize_to_limit: [150, 100]))
It worked in development but in production generating the PDF hangs indefinitely unless I take that line out.
I've tried things like #post.image.variant(resize_to_limit: [150, 100]).processed.url and setting Rails.application.default_url_options = { host: "example.com" }
Ironically when I restart Passenger it sends the PDF to the browser and it actually looks fine. The image is included.
This is similar:
= wicked_pdf_image_tag(#post.image.variant(resize_to_limit: [150, 100]).processed.url)
Rails 7.0.3, Ruby 3.1.2, wicked_pdf 2.6.3
Thanks to #Unixmonkey I added passenger_min_instances 3; to my server block in Nginx config and it worked initially but would hang Passenger under load. Since I didn't have the RAM to throw at increasing that number I came up with a different solution based on reading images from file.
= image_tag(active_storage_to_base64_image(#post.image.variant(resize_to_limit: [150, 100])))
Then I created a helper in application_helper.rb
def active_storage_to_base64_image(image)
require "base64"
file = File.open(ActiveStorage::Blob.service.path_for(image.processed.key))
base64 = Base64.encode64(file.read).gsub(/\s+/, '')
file.close
"data:image/png;base64,#{Rack::Utils.escape(base64)}"
end
I've hard coded it for PNG files as that's all I needed. Only works for Disk storage. Would welcome improvements
Uploading files in my Rails app via Carrierwave / MiniMagick. Trying to create previews of the first page of PDFs. Ran into a few issues:
1) Some PDFs convert but the background is all black. Images and comments are visible
2) Some PDFs result in this error:
ImageProcessing::Error - Source format is multi-layer, but destination format is single-layer. If you care only about the first layer, add `.loader(page: 0)` to your pipeline. If you want to process each layer, see https://github.com/janko/image_processing/wiki/Splitting-a-PDF-into-multiple-images or use `.saver(allow_splitting: true)`.:
My codes looks like this:
version :thumb do
process :convert => 'jpg'
process :resize_to_limit => [50, 50]
def full_filename (for_file = model.file.file)
"preview_thumb.jpg"
end
end
Not clearly understanding which MiniMagick command line arg to add and how to add them.
There is this well known issue with Paperclip.
https://github.com/thoughtbot/paperclip/issues/1924
How do I configure my model so that this stupid spoofing validation will work?
Before the problem was discovered I was using:
validates_attachment_content_type :csv_import, :content_type => 'text/csv'
But that would not work on some versions of Windows. On Windows 7 Professional I get this error:
[paperclip] Content Type Spoof: Filename delivery_detail.csv (application/octet-stream from Headers, [#<MIME::Type:0x00000005077f38 #content_type="text/csv", #raw_media_type="text", #raw_sub_type="csv", #simplified="text/csv", #media_type="text", #sub_type="csv", #extensions=["csv"], #encoding="8bit", #system=nil, #registered=true, #url=["IANA", "RFC4180"], #obsolete=nil, #docs=nil>, #<MIME::Type:0x000000050c7f60 #content_type="text/comma-separated-values", #raw_media_type="text", #raw_sub_type="comma-separated-values", #simplified="text/comma-separated-values", #media_type="text", #sub_type="comma-separated-values", #extensions=["csv"], #encoding="8bit", #system=nil, #registered=false, #url=nil, #obsolete="!", #docs="use-instead:text/csv", #use_instead=["text/csv"]>] from Extension), content type discovered from file command: text/plain. See documentation to allow this combination.
Has anyone ever succeded in making paperclip upload csv files?
I tried every possible workaround from Github issue reports and nothing has worked. I need to see working example solution.
update 1
sonianand11 commented on 2 Oct 2014
https://github.com/thoughtbot/paperclip/issues/1470
This works, but it involves switching off content validation, Is there a better way to do it?.
I came up with the following solution:
add to the model:
validates_attachment_content_type :my_csv_uploaded_file, content_type: ['text/plain', 'text/csv', 'application/vnd.ms-excel']
and to the initializer:
Paperclip.options[:content_type_mappings] = { csv: 'application/vnd.ms-excel' }
It worked for me. It was tested using Windows 7 Professional
I have a Rails rake task that is processing a batch of images. It strips out the white background (using RMagick), replaces it with a transparent layer, writes it to a tempfile and then saves it as a PNG on Amazon S3 (using Paperclip).
It works for the bulk of the images. However, it runs into an error for at least 1 image. Can someone help me figure out why and how to fix it?
Code sample:
require 'RMagick'
require 'tempfile'
include Magick
task :task_name => :environment do
x = Item.find(128) # image 128 is the one giving me trouble
sourceImage = Image.read(x.image_link_hires)
processedImage = sourceImage[0].transparent("white")
tempImageFile = Tempfile.new(["processed_image",".png"])
processedImage.write("png:" + tempImageFile.path)
x.image_transparent = tempImageFile
x.save!
end
The error message:
rake aborted! Validation failed: Image transparent C:/Users/Roger/AppData/Local/Temp/processed_image20130107-8640-1ck71i820130107-8640-i6p91w.png is not recognized by the 'identify' command., Image transparent C:/Users/Roger/AppData/Local/Temp/processed_
image20130107-8640-1ck71i820130107-8640-i6p91w.png is not recognized by the 'identify' command.
This message appears upon running the last line (the save operation).
Tempfile problem with small files?
I think the error has something to do with Tempfile not actually writing a file to the temp path. This error may have to do with small filesize? The specific image that it's having trouble with has an usually amount of white space, so the resulting filesize after processing is about 30k for an 800x800 pixel image.
How can I verify if this is the case? And if it is, how can I work around it?
Other observations:
When I write the trouble image to a normal file (rather than Tempfile), it saves successfully locally.
The task works fine for other images, which tend to be much bigger (~1-2MB)
After processedImage.write, I've checked tempImageFile.size. It says that it's 30kb as expected.
When I observe the temp file directory when the rake task runs, I can see the temp files being created when the task is run other images successfully. The files seem to show up when processedImage.write runs. However, for the trouble image, I don't see temp files ever being created.
Thanks for any advice.
Update 7 Jan 2013
I've investigated this more. I reran #1 above, but attempted to save onto S3 with Paperclip. This generated the same error message.
So now I believe the issue is that this is a small file in terms of bytes (32kb), but with a decent height and width (800x800). Paperclip is trying to save a thumbnail version of it, which is 90x90. Typically this generates a filesize that is <1% the original, which I assume is the source of the errors.
If anyone has an elegant workaround / fix for this, I'd appreciate hearing about it.
I'm using MiniMagick to perform some image resizing on images uploaded through a multi-part form. I need to generate a few different types of images from the originally uploaded file. Here's the code that's performing the image processing:
// Generates a thumbnail image
mm = MiniMagick::Image.open(Rails.root.join('public', 'uploads', new_url))
mm.resize(thumbnail_dimensions.join("x"))
mm.write(Rails.root.join('public', 'uploads', "t_"+new_url))
// Generates cropped version
mm_copy = MiniMagick::Image.open(Rails.root.join('public', 'uploads', new_url))
mm_copy.crop('200x200')
mm_copy.write(Rails.root.join('public', 'uploads', "c_"+new_url))
new_url is the path to the image in the public folder. The thumbnail routine works perfectly. When the app goes to start processing the cropped version, that is where things start breaking and I can't for the life of me figure it out. I receive the following error when from this code:
No such file or directory - /tmp/mini_magick20110627-10055-2dimyl-0.jpg
I read some stuff about possible race conditions with the garbage collector in Rails but I wasn't able to resolve the issue. I tried this from the console as well and can create MiniMagick instances but receive the No such file error there as well. At this point, I have no idea where to go so I'm hoping someone here has some helpful suggestions. Thanks for your help!
Details:
OS: Ubuntu (Lucid Lynx)
Rails Version: 3.0.7
Ruby Version: 1.8.7
MiniMagick Version: 3.3
Did you installed ImageMagick?
If not,
try sudo apt-get install ImageMagick,
and then restart your webrick server
it's probably the race condition which is mentioned here:
https://ar-code.lighthouseapp.com/projects/35/tickets/6-race-condition-with-temp_file
here's one fix:
http://rubyforge.org/tracker/index.php?func=detail&aid=9417&group_id=1358&atid=5365
alternatively, and probably easier, you could try this:
// Generates a thumbnail image
mm = MiniMagick::Image.open(Rails.root.join('public', 'uploads', new_url))
mm_copy = mm.clone # clone the opened Image, instead of re-opening it
mm.resize(thumbnail_dimensions.join("x"))
mm.write(Rails.root.join('public', 'uploads', "t_"+new_url))
// Generates cropped version
mm_copy.crop('200x200')
mm_copy.write(Rails.root.join('public', 'uploads', "c_"+new_url))