RMagick is unable to open file that I know exists - ruby-on-rails

I am running this code in my model on an after_update call:
require 'rmagick'
file = photo.url(:thumb) #created by paperclip
img = Magick::Image.read(file).first
and I get unable to open file `/assets/contacts/1/thumb/image.png' though that file is there and is used throughout the rest of the app, so I don't suspect permissions as an issue. Rails is looking at my assets directory for everything else, and I can access the image via direct URL on my local box, so I know it exists. I do have ImageMagick installed via homebrew, and I ran "identify -list format" and the formats seem to be installed too. I have added it to the gemfile and run bundle install as well. What other things could I try to troubleshoot this?
How do I know what directory RMagick thinks is root, or is there some other reason why I can't read the file? I have read all I can find on Google, forums, Stack, IRC, and no one seems to have an answer.

Paperclip gem stores uploaded images in public folder. So, if your image path inside public folder is:
contacts --> 1 --> thumb --> image.png
Than, you should use path '/contacts/1/thumb/image.png' . Because, if there is no forward slash in URL than rails will by default search inside the assets folder for images.

Related

Can not upload image to Spree app

I try to upload a product image to my Spree app, but i get this error
5errors prohibited this record from being saved:
Attachment Paperclip returned errors for file 'index.jpg' - check ImageMagick installation or image source file.
Attachment Paperclip::Errors::NotIdentifiedByImageMagickError
Attachment Paperclip::Errors::NotIdentifiedByImageMagickError
Attachment Paperclip::Errors::NotIdentifiedByImageMagickError
Attachment Paperclip::Errors::NotIdentifiedByImageMagickError
Should i install a gem, relocate my src image? Whats the problem here
You should to install ImageMagick. Follow these steps for install on Windows:
Download ImageMagick (you need to choose static version for your 32/64 bit OS)
Checked Add application directory to your system path and install
Reopen your command prompt to reload your PATH variables
Run convert -v to check the install worked
Okay so the problem is pretty complicated and after trying to solve it for about 3 hours i finally got it.
First, you have to remove the convert.exe from your System32 files.
We do this, because the Spree app relies on legacy code and when it
tries to upload the image the default convert.exe starts, we don't
want that.
Second you download and install the dynamic installation from the imagemagick site and you have to be sure to check the legacy support so the convert command gets supported.
I dont know if you have to, but i installed the paperclip gem. You may have to do that for it to work.
The whole reason this bug happens, is because Windows. So if you are new to this party, dual boot your station or swap to Ubuntu/Mac whatever. Seriously though, when the app tries to upload it uses the convert command, but convert is already a default Windows command so you can't upload the picture properly. So we disable the command and then we replace it with our imagemagick installation. That's it.Also good luck trying to delete the convert.exe from system32, its a weird process, but you can solve it with some google-fu.
Thanks to Michael for pointing me to right way.

RTesseract causing "no such file or directory" error under Windows 10

I am trying to use RTesseract and mini_magick to do some simple OCR in a Ruby program for some images with white background color and pink font color. Unfortunately, I keep having issues with RTesseract. I have Tesseract-OCR v3.05 installed via the .dll executable found online and stored in my Program Files (x86) folder. I also have ImageMagick v6.9.9 installed via its own executable in Program Files folder (not x86). Here is my code:
require "mini_magick"
require "rtesseract"
RTesseract.configure do |config|
config.processor = "mini_magick"
end
image = MiniMagick::Image.open("input.png")
image = RTesseract.new('input.png').to_s
puts image
As you can see, it is a very simple piece of code. It executes without a problem when I have the RTesseract code commented out, but as soon as I call RTesseract's functionalities, things like the following began to rise up:
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rtesseract-2.1.0/lib/rtesseract.rb:182:in `convert': No such file or directory - tesseract "C:/Users/NORMAL~1/AppData/Local/Temp/20170801-8600-1evxygh.tif" "C:/Users/NORMAL~1/AppData/Local/Temp/1501649603.1925441244" -l eng (RTesseract::ConversionError)
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rtesseract-2.1.0/lib/rtesseract.rb:194:in `to_s'
from testingTesseract.rb:12:in `<main>'
I have tried a lot of different things, but nothing seems to get this confounded gem to work. I would've gone to a different language or wrapper by now, but this gem is central to my project and I desperately need a solution.
When I set the environment variable PATH to C:\Program Files (x86)\Tesseract-OCR, tesseract became a recognized command and convert -version started to give the output Invalid Drive Specification. The error now seems to be that ImageMagick is not recognized/found by the calls.

Rspec errors in Rails app using Nitrous.io - 'cannot open shared object file'

When I run bundle exec rspec spec/ I get the following messages in the console:
Could not open library 'libgtkmm-2.4': libgtkmm-2.4: cannot open shared object file: No such file or directory.
Could not open library 'libgtkmm-2.4.so': libgtkmm-2.4.so: cannot open shared object file: No such file or directory.
Could not open library 'libgtkmm-2.4.so.1': libgtkmm-2.4.so.1: cannot open shared object file: No such file or directory.
Could not open library 'libgtkmm-3.0': libgtkmm-3.0: cannot open shared object file: No such file or directory.
Could not open library 'libgtkmm-3.0.so': libgtkmm-3.0.so: cannot open shared object file: No such file or directory.
Could not open library 'libgtkmm-3.0.so.1': libgtkmm-3.0.so.1: cannot open shared object file: No such file or directory
But, all of the tests pass and my app works just fine. Can anyone fill me in on what may have happened? Should I just scrap this box and clone my repo into a fresh one? I'm using Ruby 2.0.0p247 and Rails 4.0.2 in a Nitrous.io box. Thanks.
Even though unrelated (thank google for finding this), I have been trying to install Nitrogen for Ubuntu today. I get the same error. Seems like a specific GTK library has gone missing recently from either your system or your distro's repository (if you're even using Linux).
If you're using a Linux system, install the libgtkmm packages and if you're lucky enough, your missing library will be in one of those.
Use in Ubuntu this to install all of them:
sudo apt-get install libgtkmm*
Hope this helps.

Rails 3 and rmagick

I have problem with rmagick in Rails 3.
Everything seems to be installed, but I get the following error:
Failed to manipulate with rmagick, maybe it is not an image? Original
Error: unable to open image
`kar/public/uploads/tmp/20110825-1348-30304-9150/thumb_1314198312_by_jannnu11_500.jpg':
# error/blob.c/OpenBlob/2588
File exists. Have good chmod.
This code works on another server.
Any ideas?
In some cases the installation of ImageMagick may have missed some of the file formats required for the image manipulation you are trying to do on that machine.
Try to run identify -list format and see what you are missing from there.
If it turns out that you are just missing the file format, try to uninstall/reinstall and you should be good to go.

.gemrc file specification

I searched everywhere to find the .gemrc file specification but I haven't succeed.
Does anyone know where I can find it?
gem looks for a configuration file .gemrc in your home directory, although you can specify another file on the command-line if you wish (with the --config-file modifier).
There are three things you can specify in the configuration file:
command-line arguments to be used every time gem runs
command-line options for ’’RDoc’’ (used when generating documentation)
GEM_PATH settings
More at gem environment command doc.
'Home' is a Linux/Mac term. What is refers to is the folder where a user's settings appear. You can find out where your settings directory is by doing the following:
on Unix/Linux, open a terminal and type the following command:
echo $HOME
on Windows, open a command-prompt and type the following command:
echo %USERPROFILE%
For me (in Windows 7), this is C:\Users[name]. However, looks like Ruby doesn't set up your .gemrc in that folder by default. Instead, you have to create the file. Open a text editor, copy the YAML style code you need (documentation), and save the file as .gemrc in your home directory (make sure you select all files, not '.txt').
These settings will only affect that individual user. If it's your personal computer, however, you probably don't need to change the settings for all users.
An updated gemrc specification is available at RubyGems Guides (under 'gem environment'). Note that /etc/gemrc applies to all users, while ~/.gemrc applies to an individual.
If the key is a gem command (for example, install:), it specifies arguments to be used with that command.
Here are the other keys that can be specified:
:sources: A YAML array of remote gem repositories to install gems from
:verbose: Verbosity of the gem command. false, true, and :really are the levels
:update_sources: Enable/disable automatic updating of repository metadata
:backtrace: Print backtrace when RubyGems encounters an error
:gempath: The paths in which to look for gems
All of the answers here at time of writing are wrong because the obnoxious website keeps changing. It is at this moment here:
https://guides.rubygems.org/command-reference/#gem-environment
Obviously you should expect it to change constantly at this point.

Resources