Resize / crop images for android devices (Rails) - ruby-on-rails

I need to resize/crop images in different resolutions for Android App.
I know, that it's a common question, but I could not find a solution that would suit for me.
The problem is consist in the fact that different devices had different diagonal. The administrator upload to CMS only one image with size 668x1024 (for example). Then I need to send a picture to the device with such resolutions: 400x600, 540x960, etc. I can't just resize each image, because on the phone would appear a background-lines.
So, I'm or violating the aspect ratio (that's not good because picture becomes ugly) or should to crop the image. But because of the different proportions it's hard to do.
I tried mini-magick and g1nn13-image-science, but in the first one I can't take the current images width and height (and because of it I can't realize the cropping algorithm) and the second one gives me an error when I try to require image_science: "no such file to load -- inline"
All help and advices appreciated: some decisions about How To do it (maybe not to do cropping, but some another decision?) or gems that could help me.
Thanks.
ANSWER:
I used fastimage gem for finding picture's current height and width and the method of minimagick gem image.shave.
Example:
current_w, current_h = FastImage.size(object.attach.path)
if current_w < current_h
remove_h = ((current_h - height)/2).round
image.shave("0x#{remove_h}")
remove_w = ((current_w - width)/2).round
image.shave("#{remove_w}x0")
end

There is good gem called dragonfly for rack apps.
Basically you save base image, and then you can pass resolution in the request, and it saves new size in the cache.
Here is some introduction to this gem
Overview:
Android ==(400x600)=> Rails(ImagesController#show) ===> image_url(400x600).jpg

Maybe this fits your needs,
http://www.gra2.com/article.php/using-rmagick-imagemagick-rails
if you don`t want to use any gem you can reach the installed programms with the rails
system
command.

Try to use paperclip, it uses ImageMagick and allows to specify its cropping algorithm, for example:
400x600^
400x600#
400x600>
The ^ # and > are described here
https://github.com/thoughtbot/paperclip

Related

Edit Photos via Photoshop on a server

I wart to create a web app where a user enters certain data via a form and then receives a custom rendered image. The image is from a smart object in a psd. It's kind of like a mock-up which definitely requires needs some photoshop filters to be properly rendered.
This should all happen in real time and should be doable from my understanding since the rendering of a single images doesn't need much computing power
I've done some research and haven't really found a solution the matches my problem. Is it necessary to run Photoshop on a server and then remotely run a photoshop script and then upload the generated image somewhere else?
I've used The After Effects Plugin Template by DataClay in the past which offers similar functionality but for video.
Looking forward to hearing your ideas.
Thanks
You can use the Dataclay plugin to handle still image exports out of After Effects. Make a single-frame duration composition in After Effects and rig the layers with the Templater plugin. Then use the PNG Sequence output module to render out a single frame.
From Dataclay's forums:
Exporting
A few extra steps are required to correctly render a project file as a PNG sequence using Templater. By default, a file rendered as a PNG sequence will have the frame number appended to the end of the file name, i.e.:
filename.png00000, filename.png00001, filename.png00002, etc.
In order to designate where in the filename the frame number should be added, we’ll need to use the output column. First, add a column named output to your data source. Next, add a filename with a set of brackets with five # signs to designate where the frame numbering should be added. For example:
filename[#####] would result in filename00001.png
or
[#####]filename would result in 00001filename.png

ImageProcessing::Error when passing crop argument to variant

I am having a model with has_one_attached :file that I want to process with a custom crop parameter. The attached file is a photo in jpg format that I use for testing.
Before, I was scaling and cropping images this way, which works as intended.
my_model.file.variant(resize_to_fill: [1440, 560, { gravity: 'Center' }])
Now I wanted to change the center / focus point for cropping the image to a custom position. The plain imagemagick command would be convert input.jpg -crop 1440x560+580+120 output.jpg, working as intended on the command line.
The docs state that you can pass almost any imagemagick commant to .variant(), which lead me to try this:
my_model.file.variant(crop: '1440x560+580+120')
This raises the following 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)`.
I read the manual of image_processing and understand how the pipeline works, but I am missing the part of where/how to adapt the pipeline through .variant() in order to get a result.
Am I doing something wrong here or am I just missing some simple part? Otherwise I'd go the way of writing it raw with an image_processing pipeline and work around it.
My environment consists of:
rails - v6.0.2.2
mini_magick gem - v4.10.1
image_processing gem - v1.10.3
ImageMagick 7.0.8-66
Try my_model.file.variant(combine_options: { crop: '1440x560+580+120')

How to include a photo in Moderncv Casual

Well, to start with, I don't know much about Latex. I am failing to include a picture in to the document using "Moderncv Casual". A lot of the CV's and cover letter's template using:
\photo[64pt][0.4pt]{filename}
What's the deal with this? Is it not just to type the pictures's filename, compile, and the picture should be added to the document?
That's exactly it. The \photo macro is set up in such a way that it stores your input and makes it part of the CV title (set with \makecvtitle).
The reasoning behind this is to provide the end-user with a generic command to would capture a picture. However, depending on the template used, this picture may appear on the left/right/middle (or wherever). The generic input abstracts this placement from the rest of the code.
Specific to the command \photo; it is defined inside the class moderncv.cls file as:
\NewDocumentCommand{\photo}{O{64pt}O{0.4pt}m}
{\def\#photowidth{#1}\def\#photoframewidth{#2}\def\#photo{#3}}
An input like
\photo[64pt][0.4pt]{filename}
defines the photo to be kept in \#photo - it references the image file filename (with an image extension) - to have a width of 64pt (stored in \#photowidth) and frame width 0.4pt (stored in \#photoframewidth).

Is there a gem or a quick way to render some prawn code as an image?

I'm looking for a way to re-use my prawn code in other views (HAML). It can also be an image, but I don't need the whole PDF (with the layout and other pages), just a small graphic (chart) that I render in Prawn using a method.
EDIT:
because I'm using Heroku and I'm not saving the file (it's a response to a web request), I'm don't really want to open a pdf file with ImageMagick for example and process it into an image, unless it is my last option (besides coding the graphic again in HTML+CSS).
I don't know about a gem that might do the trick but you can always use RMagick or MiniMagick to covert the generated pdf to an image and scale it down:
require 'RMagick'
pdf = Magick::ImageList.new("doc.pdf")
image = pdf.scale(300, 300)
image.write "doc.png"
Hopefully you'll find this useful.

How to convert PDF files to images using RMagick and Ruby

I'd like to take a PDF file and convert it to images, each PDF page becoming a separate image.
"Convert a .doc or .pdf to an image and display a thumbnail in Ruby?" is a similar post, but it doesn't cover how to make separate images for each page.
Using RMagick itself, you can create images for different pages:
require 'RMagick'
pdf_file_name = "test.pdf"
im = Magick::Image.read(pdf_file_name)
The code above will give you an array arr[], which will have one entry for corresponding pages. Do this if you want to generate a JPEG image of the fifth page:
im[4].write(pdf_file_name + ".jpg")
But this will load the entire PDF, so it can be slow.
Alternatively, if you want to create an image of the fifth page and don't want to load the complete PDF file:
require 'RMagick'
pdf_file_name = "test.pdf[5]"
im = Magick::Image.read(pdf_file_name)
im[0].write(pdf_file_name + ".jpg")
ImageMagick can do that with PDFs. Presumably RMagick can do it too, but I'm not familiar with it.
The code from the post you linked to:
require 'RMagick'
pdf = Magick::ImageList.new("doc.pdf")
pdf is an ImageList object, which according to the documentation delegates many of its methods to Array. You should be able to iterate over pdf and call write to write the individual images to files.
Since I can't find a way to deal with PDFs on a per-page basis in RMagick, I'd recommend first splitting the PDF into pages with pdftk's burst command, then dealing with the individual pages in RMagick. This is probably less performant than an all-in-one solution, but unfortunately no all-in-one solution presents itself.
There's also PDF::Toolkit for Ruby that hooks into pdftk but I've never used it.

Resources