Convert portrait and landscape aspect images at the same time

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
RKwasi
Posts: 11
Joined: 2016-03-26T17:07:28-07:00
Authentication code: 1151

Convert portrait and landscape aspect images at the same time

Post by RKwasi »

I have an image upload form where users submit vertical and horizontal format images.

My problem.
I need the resized images to be these sizes:
(they will usually be these exact sizes actually since they submit files that resize perfectly)

thumb:
80w x 120h
or
120w x 80h

middle:
133w x 200h
200w x 133h

resized:
466w x 700h
or
700w x 466h

I'm using a bootstrap/dropzone.js combo form where they drag and drop the images being uploaded.

My issue is how would I be able to make horizontal aspect images 120, 200 or 700w
and vertical aspect images 80 133 and 466w
and do it all in one command line step (actually, one for each size/folder location etc)

This is my last variation, although everything I've tried including this doesn't achieve the final desired sizes, just horizontal images that are 80 wide instead of 120 w etc.

convert -resize 80x120^ c:\inetpub\wwwroot\kp-new\model_images\[THISFILENAME] -quality 75 c:\inetpub\wwwroot\kp-new\model_images\thumb\[THISFILENAME]
convert -resize 133x200^ c:\inetpub\wwwroot\kp-new\model_images\[THISFILENAME] -quality 75 c:\inetpub\wwwroot\kp-new\model_images\middle\[THISFILENAME]
convert -resize 466x700^ c:\inetpub\wwwroot\kp-new\model_images\[THISFILENAME] -quality 75 c:\inetpub\wwwroot\kp-new\model_images\resize\[THISFILENAME]

Is what I'm trying to do even possible?
Can I read into an image and determine if its a vertical or horizontal?

I am doing this on a windows server.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert portrait and landscape aspect images at the same time

Post by fmw42 »

I am not sure I understand. If you resize image, if the aspect ratio changes, you cannot get both the width and height to stay the same. So you would have to crop or pad depending upon how you resize. But if you want one command to resize using your values, then you need to use parenthesis processing and clones.

Code: Select all

convert image.jpg -quality 75 ^
( -clone 0 -resize 80x120^ -gravity center -extent 80x20 +write thumb1.jpg ) ^
( -clone 0 -resize 133x200^ -gravity center -extent 133x200 +write thumb2.jpg ) ^
( -clone 0 -resize 466x700^ -gravity center -extent 466x700 +write thumb3.jpg ) ^
null:
If you do not want or need the cropping from the -extent, then remove -gravity center -extent WxH.


See
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#clone
http://www.imagemagick.org/Usage/files/#write

Please always provide your ImageMagick version and platform/OS (which I assume is Windows from your folder syntax)
RKwasi
Posts: 11
Joined: 2016-03-26T17:07:28-07:00
Authentication code: 1151

Re: Convert portrait and landscape aspect images at the same time

Post by RKwasi »

If someone uploaded a vertical aspect image, I need it to be 466w x 700h max
If someone uploaded a horizontal aspect image, I need it to be 700w x 466h max

They would in all likelihood do a combination of horizontal and vertical aspect images at the same time in the same upload form.

As an example when someone uploads a vertical image it resizes to 80w x120h in the thumbnail folder.
BUT
when they upload a horizontal image it resizes to 80w x53h in the thumbnail folder.
I need the horizontal images to be resized to 120w x 80h

Same for the other 2 sizes.
The horizontal images get sized to a different width, not the same width as the vertical images.

Does that make sense?
RKwasi
Posts: 11
Joined: 2016-03-26T17:07:28-07:00
Authentication code: 1151

Re: Convert portrait and landscape aspect images at the same time

Post by RKwasi »

On my Windows 2012 R2 Server I'm using
ImageMagick-7.0.1-Q8

on my desktop I'm still using
ImageMagick-6.9.3-Q8

I should probably upgrade the vers on my PC to be in sync with my server
RKwasi
Posts: 11
Joined: 2016-03-26T17:07:28-07:00
Authentication code: 1151

Re: Convert portrait and landscape aspect images at the same time

Post by RKwasi »

Found a post here on the forum that pointed me in the right direction.

Doing this:
convert -resize "120x120>" c:\inetpub\wwwroot\kp-new\model_images\[THISFILENAME] -quality 75 c:\inetpub\wwwroot\kp-new\model_images\thumb\[THISFILENAME]
convert -resize "200x200>" c:\inetpub\wwwroot\kp-new\model_images\[THISFILENAME] -quality 75 c:\inetpub\wwwroot\kp-new\model_images\middle\[THISFILENAME]
convert -resize "700x700>" c:\inetpub\wwwroot\kp-new\model_images\[THISFILENAME] -quality 75 c:\inetpub\wwwroot\kp-new\model_images\resize\[THISFILENAME]


.....works perfectly.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Convert portrait and landscape aspect images at the same time

Post by Bonzo »

Your input image name/path should come before the -resize
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert portrait and landscape aspect images at the same time

Post by fmw42 »

Well then try

Code: Select all

convert image.jpg -quality 75 ^
( +clone -resize 700x700^ +write thumb1.jpg ) ^
( +clone -resize 200x200^ +write thumb2.jpg ) ^
( +clone -resize 120x120^ +write thumb3.jpg ) ^
null:
On IM 7 replace convert with magick.
Post Reply