Action Text displaying image error rails 6 on windows 10 - ruby-on-rails

I am using windows 10 to program ruby on rails 6 with the help of ruby installer. Everything works fine so far until... I use action text for rich text editor.
I followed this guide [https://edgeguides.rubyonrails.org/action_text_overview.html][1] and everything works. But when I attach an image to the text_area, after saving the image cannot be displayed.
When I open the image's url, I see this:
MiniMagick::Error in ActiveStorage::RepresentationsController#show
`magick mogrify -resize-to-limit [1024, 768] C:/Users/.../AppData/Local/Temp/ActiveStorage-5-20200716-3792-28u8ot.jpg` failed with error: mogrify: unrecognized option `-resize-to-limit' # error/mogrify.c/MogrifyImageCommand/6009.
if status != 0 && options.fetch(:whiny, MiniMagick.whiny)
fail MiniMagick::Error, "`#{command.join(" ")}` failed with error:\n#{stderr}"
end
Model: Product
title
description(action text - attach image in text_area)
Display in view: product.description ( attached image cannot be displayed )
Can anyone help me solve this problem?

Add gem 'image-processing' to your Gemfile and
run bundle install

Related

RTesseract not showing the text of images in rails

I install RTesseract in my application but the issue I face is that after reading any image it just give '/f' in output
image = RTesseract.new('ya.png', lang: 'eng')
image.to_s
Output : "\f"
How can I fix this issue?
If the output is just "\f", that means that tesseract didn't detect any words in the image.

"No such file or directory" error on cropped ImageMagick image

I have a function in a DelayedJobs-run background process on my Rails app that receives Base64 image blobs, resizes and crops them, and then saves them:
image64 = Base64.decode64(screenshot)
image = MiniMagick::Image.read(image64)
image.resize "#{image.width * 0.5}x#{image.height * 0.5}"
image.crop("#{image.width}x250") if ("#{image.height}".to_i > 250)
Base64.encode64(image.to_blob)
The addition of the .crop call, however, stops this process working my production CentOS Linux server, resulting in this error from the .to_blob call:
Errno::ENOENT: No such file or directory # rb_sysopen - /tmp/mini_magick20170228-1709-t2rcyo
/path/app/vendor/bundle/ruby/2.4.0/gems/mini_magick-4.6.1/lib/mini_magick/image.rb:175:in `binread'
/path/app/vendor/bundle/ruby/2.4.0/gems/mini_magick-4.6.1/lib/mini_magick/image.rb:175:in `to_blob'
Without cropping, the function works. Running this in the foreground (i.e. in rails console) does not cause an issue.
Updating ImageMagick, ensuring /usr/bin is in PATH and sim-linking identify did not work (per this).
Any help is appreciated.
Per #mmichael, this question / answer shifted the problem. Specifically, changing
image.crop("#{image.width}x250")
to
image.crop("#{image.width}x250+0+0")
allows the .to_blob to happen without issue.
If anyone can explain the issue as to why this happens when run in the background only, I'll gladly accept your answer instead.

RMagick, Tempfile, Paperclip: how to save a image file with large dimensions and small kbs as a thumbnail?

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.

Where do the temp files go when using MiniMagick in a Ruby on Rails app?

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

I can't get rails plugin wicked_pdf to work

I wanted to create PDFs for my rails application using wkhtml2pdf and wicked_pdf.
I downloaded and extracted wkhtml2pdf beta 4 and placed it in /usr/local/bin/wkhtml2pdf
I tried running it on a web site and it gave a nice result.
In my rails application (2.3.4) I installed wicked_pdf:
script/plugin install git://github.com/mileszs/wicked_pdf.git
script/generate wicked_pdf
Everything seemed to be ok.
inside script/console I run the following - (with the following output)
wp = WickedPdf.new
=># WickedPdf:0xb62f2c70 #exe_path="/usr/local/bin/wkhtmltopdf"
HTML_DOCUMENT = "<html><body>Hello World</body></html>"
=> "<html><body>Hello World</body></html>"
pdf = wp.pdf_from_string HTML_DOCUMENT
=> "/usr/local/bin/wkhtmltopdf - - -q"
=> "\n\n\n\n\n\n\n\n\n\n"
of course this isn't good. According to the test the result of my last command should start with "%pdf-1.4"
Any idea what I can do?
Having the same problem. Removed the -q option from the wicked_pdf.rb file on line 19 and then was able to get the proper string on the console.
=> "%PDF-1.4\n1 0 obj\n<<\n/Title ...
This also seems to have solved other problems. The PDF still didn't render correctly when using it from the web site - embedded font issue - on to the next issue now.
Hopefully this will work for you.

Resources