Dynamically render a complex image in Rails with a template - ruby-on-rails

I am trying to add an action to my Rails app rendering a PNG image specific for a given user - some sort of a badge with user picture, name and some standard text. I want to embed the images in emails to be sent out to the users.
The whole badge is fairly complex image, so I am looking for an approach where I can reuse some sort of template image that I just need to update with a user picture and his name.
Would appreciate any examples or ideas on how to do it in rails (what gems can be helpful). Is it easy to have a Rails app to use SVG as a template and then convert it to PNG?

Check out this question
Basically, there are a few different gems you could use to create or manipulate images. Also check ruby toolbox

Related

Add images into rails article

With Rails 5.1 and a pgsql database: I have a pretty standard Article model with :title and :description. Users can make your typical text-only articles.
What if users wanted to embed a gif or image into the post, like between paragraphs? I see a lot of this type of format on other website blogs and I am not sure how to achieve this with Rails.
Note also I am using simple_format(article) to display, so any formatting done on creation should hold.
Any ideas?
There is actually quite a bit of work involved with this:
If you need to allow your users to upload images from their
computer, you will need to get your head around the Paperclip
gem (or similar). This will store the image on the server.
You'll want to determine how big can images be, what to name the
images once uploaded and what file formats are permitted to prevent
users uploading all sorts of files.
Next on the front-end, you can either use a rich text editor (eg. CKEditor or TinyMCE are the main ones) to embed the
IMG tag(s) into your Article text, or structure that layout yourself
(eg. "Always display an image before/after the article text"). Do
your images need captions? Do they need to be clickable?
Lastly, in your Show template, don't forget to use Rail's image tag helpers rather than your standard IMG tags. This enables you
to easily reference the images on the server without getting lost in
the asset pipeline.
There are some videos out there showing how all this is done, eg. https://www.youtube.com/watch?v=Z5W-Y3aROVE. Good luck!

What are my options for resizing an image in my rails application?

I would honestly appreciate feedback, or an on edit, on what I should change about this post instead of all the down votes. I'm new to web development, and I had an honest question. If this isn't the place to ask it, please point me in the right direction.
I have a Ruby on Rails application where part of the core purpose involves displaying images that I pull from the Facebook Graph API. And I want to be able to display the images in a uniform size.
What I'm hoping I can do: I'm trying to just store the id of the image from Facebook to my database and resize the image after I pull it from Facebook's servers. I was thinking that this would save on storage costs. I've been looking at the RMagick gem that binds Ruby to the ImageMagick library, specifically their resize method.
Another option I've considered: I'm thinking about adding a cropping feature. To do this though, I think I would need to set up a storage service, like Amazon's S3. Then I would pull from the image storage service where the cropped images are stored instead of pulling from Facebook and resizing client-side.
Are there any options I might not be considering? And how accurate are my examples to approaches that could be taken.
If you have a huge number of images to show, then go with "resize - store - show". If less then go with an on-the-fly process.
Here are a few well-known gems that help you process images, but choose according to your requirement:
CarrierWave
Paperclip
Dragonfly
refile (new gem)
See "Compare CarrierWave ,Paperclip and Dragonfly in rails" and "Refile: Fixing Ruby File Uploads".

Rails: How to use one form file_field to upload multiple selected files at once?

I'm trying to make an image heavy web app that allows you to upload multiple images. However, I don't want to pollute my site with a bunch of file_fields because it looks ugly and is also inconvenient, since the user has to upload multiple pics one by one like that. Is there a way to make it so that when you click the file_field box that you can select/highlight multiple files at once and upload them all at once? If so, how would I target each uploaded image so that I can display each of them?
Short answer: you can't. field_field just elaborates into input tags of type file which, by specification, can only load a single file at a time.
You can, though, have a look at other solutions which can be fit into a Rails application such as jQuery-File-Upload or Plupload

Best way to display non-public images in a rails view

I am trying to find the best way to render confidential images in a view, without storing the images within the rails application flat-filesystem, as I have no idea where to place the images. I am storing the image data binary as :text in a sqlite3 database table and successfully display the images using
<% s = "data:image/png;base64,#{ActiveSupport::Base64.encode64(#my_image)}"%>
<img style = 'width:100%; height:600px' src = '<%= s %>'/>
This works for me in Firefox and Chrome, but my client cannot get the images to display. I'll find out in an hour or two what browser they are using. Client says they want the image src url to look like a relative path within a controller's folder, which seems to contradict the notion of not storing the image in the flat-file system.
I think I am missing something very small here, but I would like to know the proper way to store images and documents in an application that are not public to all users. If my question is not clear or you need information, please let me know and I will provide more information.
I have read of attachment_fu and paperclip, but they appear to allow attachment downloads, and I just need to display an image inline on a page. Any help is greatly appreciated. Thank you much in advance.
You can keep files in non-public repositories and have controllers action with send_file(path, options = {}) It allows you store files somewhere on the hard disc and keep access logic inside your controller.
Have you tried the paperclip gem? You can upload images to amazon and amazon allows you to set permissions for files...if you want to do it that way.
As Artem says, Amazon is a great way to achieve this. But if I get you right, they want to see an URL to the image directly (i.e. be able to type the source into the address-field if they want to).
You need to decide wether everyone should be able to access the image (given they know the name/path), or to have authentication, in which case I don't think a relative path is worth anything.
Can't you just have an image-folder containing all images (not accessible by URL), and a table to lookup wether userX is allowed to see imageY?

dynamic web badge in rails without javascript

I'm trying to create a web badge with user ranking. Intend for it to be used in forums where javascript might not be enabled. I have come across this
<img src='http://www.x.com/vendor.gif.aspx?id=123' style='width:50px; height:50px'/>
from How does one get about making a dynamic web badge using client side javascript?
But i'm using ruby on rails.
Anybody has an alternative method or some hints? Thanks much.
You can create an image using RMagick (uses imagemagick). Use the annotate method to put text in your image.
You don't need JavaScript. Just create a Rails action, which serves the image or whatever you want to be the badge.
<img src="/users/123/badge" ....>
You may want to ask how to create an image with Rails, but that will be a different story ;-)
(ImageMagick comes to mind)
You can create the image with RMagick on the server and then return it to the browser.
Here is a link to an article explaining how to create images.
You'll then need the send_data method in Rails.
I'll try and dig up a link that explains this exactly.

Resources