ImageProcessing::Error when passing crop argument to variant - ruby-on-rails

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

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

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.

Resize / crop images for android devices (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

Convert favicon.ico to png to using ImageMagick Procedurally

It looks like ImageMagick does not always convert a single favicon.ico file to a predictable single png file - for some favicon's, it generates a bunch of other favicon-01.png, favicon-02.png, etc... Is there a way to figure out which one's the actual converted favicon you want - or figure out how many got generated, to delete the unwanted ones?
I came across with the same problem while I was trying to convert blogger's favicon and I solved it by using -flatten parameter of Imagemagick like this:
convert "favicon.ico" -thumbnail 16x16 -alpha on -background none -flatten "favicon.png"
This likely happens because there are multiple images in the icon file - this is to provide differet resolutions for different contexts. Presumably you'd want to run a search in the target directory for favicon*.png, then check the dimensions of each one to find the one you wanted (deleting the others as you go).
I guess some of these are animated gifs. You can take the first one as described here:
http://www.imagemagick.org/script/command-line-processing.php
i.e.:
$magick> convert 'images.gif[0]' image.png
I don't have ImageMagic installed, but you might try the above for all favicon.ico, it might work fine.
Otherwise, you would probably need to write a script to check for favicon-01.png and, if it exists, rename it to favicon.png and delete favicon-*.png (provided you don't have anything else named like that in the working folder).

java ProcessBuilder doesn't work right with Imagemagick on windows

I'm using im4java to call imagemagick from a grails app. The command line I'm trying to get it to use is
convert "c:\ZonesG\web-app\spresources\summarypage_images\00\09\18\myimage.jpg" -resize 100x100 "c:\ZonesG\web-app\spresources\summarypage_images\00\09\18\myimage_thumbnail100.jpg"
or
convert c:\ZonesG\web-app\spresources\summarypage_images\00\09\18\myimage.jpg -resize 100x100 c:\ZonesG\web-app\spresources\summarypage_images\00\09\18\myimage_thumbnail100.jpg
The args passed to ProcessBuilder look correct. There are 5 separate Strings as follows.
convert
c:\ZonesG\web-app\spresources\summarypage_images\00\09\18\myimage.jpg
-resize
100x100
c:\ZonesG\web-app\spresources\summarypage_images\00\09\18\myimage_thumbnail100.jpg
I get an error that the -resize parameter is not valid when I run it through processbuilder. That makes it seem as if it is calling the convert function, but something has happened to the parameters so that it no longer recognizes them. It's not the hypens in the directories. I tried it to different directories and it still doesn't work.
When I run it from the command line it works perfectly.
I've tried adding double quotes around the two file names. Again it works great in the command window but doesn't work from the java code.
Any Ideas?
Thanks for your comment Jonathan,
Meanwhile, I discovered what was happening.
The order of the args mattered more when calling from the ProcessBuilder. I changed it so that the resize and geometry are first and the input image file is next. That made it work.
I had this error too. It is actually because it is running a program from Microsoft in Windows called "convert". To get im4java to run the correct you need to make an environment variable. Eg: IM4JAVA_TOOLPATH=C:\Program Files (x86)\ImageMagick-6.6.4-Q16\

Resources