How does Rails process images uploaded by a form? - ruby-on-rails

Very simple question.. strangely can't find any intuitive answers anywhere.
I got a HTML form with which allows users to upload an image.
When this form is submitted and goes to a Rails controller, how do I get the image? Suppose I want it in base64.
When I do:
image = params["image"]. I just get a filename... but where is this file? is it in my server? How do I then convert this to base64? I guess the conversion is easy once I know where this file actually is in my server...

params['image'] should be an instance of Rack::Multipart::UploadedFile, so you should be able to access the path on disk by doing params['image'].path.
P.S.: To save a character, most prefer to use symbols since most Rails hashes are HashWithIndifferentAccess and can be accessed using a symbol or a key. So params[:image].path :-)

Related

Can any one help to encode the rails code.I want to share my code in binary executable form?

I want to share my rails code with my client in encoded form so he can not able to read the code.Is it possible in rails
I've just found sth like that: https://www.rubyencoder.com/
Never used it....probably other solutions are available on google

How would you upload an image URL to an external website which generates a new URL for it and retrieve the newly generated url in rails?

I am creating a website which allows users to post links to gif images stored externally. Unfortunately gifs are terribly expensive and luckily I have found the following website which compresses them quite radically: http://gfycat.com/
Currently my app stores the url to the linked image (as of yet it is uncompressed and without the use of gfycat)
What I would like to do is to take the url that the user posts, use it to compress the gif via gfycat.com and then store the newly generated url (pointing to the compressed image) in my database.
How would I do this?
Sorry for the long explanation btw
Just took a look at gfycat.com, seems like they do have an accessible webservice at http://gfycat.com/fetch/:url
If this is the case, and you already have the URL you can very easily grab the 'minified' version of the gif by grabbing the URL and saving the file.
So, I would probably do something like this:
Install carrierwave or another image upload gem of your choice
In the case you used the above gem...
assuming Cat is your model, and you have an uploader attached to it named gif_photo, and also assuming the original 'gif' url is stored as original_gif_url
You can do something like the following (assuming #cat is some instance of the cat model):
#cat.update_attribute(:remote_gif_photo_url, "http://gfycat.com/fetch/#{#cat.original_gif_url}")
as simple as that :)... to get back at the gif, you can simply call #cat.gif_photo_url
(again, assumptions for code above are using carrierwave syntax, but can easily be done with any of its alternatives.)

Creating a table entry while using CarrierWaveDirect

Is it possible to use something like CarrierWaveDirect to upload directly to S3 and still be able to gather some data on the files being uploaded?
For instance, is it possible to change the filename, and save the size in a table before/after the upload? I don't need to do any kind of manipulation to the file either (I was reading some documentation regarding the use of Resque).
I realize that this is a very novice question but I couldn't find the answer anywhere.
After a lot of fiddling, I found out that the S3 secure upload form that the hidden field success_action_redirect returns so params on the uploaded file.

rails 3: how to add (or perhaps substitute) a text string to an existing PDF document?

Users of our app need to print a PDF document we have pre-created, but have a placeholder string in the PDF template "YOUR_NAME_HERE" be replaced with their name. (Or, alternatively, we could no use a placeholder and add a new string with a certain font/style at a certain X,Y offset.)
Doing full PDF creation is overkill, since ALL we need to do is add their name to the PDF doc.
To make it more fun, we're hosted on Heroku which does not have local file storage, so we need to create the final PDF as something displayed in their browser that can (hopefully) be saved to local disk.
Does anyone know of a technique that would let us easily add (or replace) text to an existing PDF document?
I'm not finding anything for editing PDFs in ruby. I would just look into using something like prawn to generate them, even if that is a bit overkill when only a few words are different between each.
If efficiency is an issue, you could convert the pre-made part into a PNG and then just add the text on top. It feels dirty, but it'd probably be quicker than full generation and I don't know what other options you have, since it doesn't seem like anyone has implemented a true PDF editor in ruby yet.
As far as local storage, keep in mind that you do have write access to tmp/ on Heroku, so you can use that as long as you're only going to use the file during a single request.

Generating a QR code in rails

I want to generate QR codes in ruby on rails, to run in the background of my website written in rails. Saw this http://code.google.com/p/qrcode-rails/ but cannot work out how I could get this to work for me. Basically in RoR I want to:
Pass a generator a string, my unique code, a 20 character length number (e.g. 32032928889998887776) and have an image generated with the name 'code'_qr.jpg and saved in a resource folder to be attached to an email that my program will send out.
How would I do this, does anyone know?
And while I'm asking (not so important that I get this answer now) but how would I implement QR code reading in, to get that code back, from a web cam? Thanks.
If you just need to write the data from the URL to a file, you can open up a stream, read from the file, and simply write the data to disk -- just remember to use the same extension (.jpg in this instance.)
Note that you could also simply send the link in the email (or post it as an inline image in the email.) If you really, really want to write it to disk and send it as attachment in your production system, the first-class solution for Ruby image processing is ruby-vips or ImageMagick.
Finally, since it's a disk operation, you're going to want to do it outside the normal web request cycle -- you're probably best off farming the operation out with delayed_job, or at the very least triggering the process with an AJAX request. Both of these give you the advantage that you can present a progress bar for the operation.

Resources