Using aspect-ratio orientation to select bg image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
motumbo
Posts: 16
Joined: 2011-07-26T12:56:12-07:00
Authentication code: 8675308

Using aspect-ratio orientation to select bg image

Post by motumbo »

I have to use the aspect-ratio orientation (portrait vs landscape) of an image to dynamically select between two backgrounds. I have to use the 'convert' tool.

ie if width > height (of my_photo.jpg) use bg_1.png as background and if width <= height (of my_photo.jpg) use bg_2.png as background.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using aspect-ratio orientation to select bg image

Post by fmw42 »

What version of IM are you using and on what platform?

If on Unix-- linux/mac, then

aspect=`convert image -format "%[fx:w/h>1?1:0]" info:`
if [ $aspect -eq 1 ]; then
# process for landscape
else
# process for portrait
fi

If on windows, the syntax will be different. I am not a windows user, so see http://www.imagemagick.org/Usage/windows/
motumbo
Posts: 16
Joined: 2011-07-26T12:56:12-07:00
Authentication code: 8675308

Re: Using aspect-ratio orientation to select bg image

Post by motumbo »

Linux ImageMagick 6.5.4-7 2011-01-17

I was wondering if I can accomplish this with a single call (I'm using PHP), right now I'm using this:

convert bg_1.png ( my_photo.jpg -thumbnail 600x600 ) -geometry +200+120 -compose Multiply -composite output.jpg

maybe combining:
-set filename:aspect '%[fx:w/h>1?1:0]'
and
'bg_%[filename:aspect].png'
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using aspect-ratio orientation to select bg image

Post by fmw42 »

[ `convert image -format "%[fx:w/h>1?1:0]" info:` -eq 1 ] && # process for landscape || # process for portrait
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Using aspect-ratio orientation to select bg image

Post by anthony »

Code: Select all

bg=bg_1.png ;
[ `convert image.png -format "%[fx:w/h>1?1:0]" info:` -eq 1 ] && bg=bg_2.png ;
convert image.png $bg  ....do your process....  output.png
Just put it all in the 'string to be executed in PHP. The PHP exec() function calls a shell which can process multiple shell commands, or even a small shell script!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
motumbo
Posts: 16
Joined: 2011-07-26T12:56:12-07:00
Authentication code: 8675308

Re: Using aspect-ratio orientation to select bg image

Post by motumbo »

Thanks!
User avatar
michelle
Posts: 31
Joined: 2011-08-24T06:45:50-07:00
Authentication code: 8675308

Re: Using aspect-ratio orientation to select bg image

Post by michelle »

*perfect*, thanks Fred - this really helped me ammend my overly complex, convaluted attempt. Thank-you.

C:\tmp>convert landscape.tif -format "%[fx:w/h>1?1:0]" info:
1

C:\tmp>convert portrait.pdf -format "%[fx:w/h>1?1:0]" info:
0

C:\tmp>convert portrait.pdf -rotate 90 was-portrait__now-landscape.png

:D michelle
Post Reply