[SOLVED] Conditionally Adding Padding To Images

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
chipncharge
Posts: 6
Joined: 2018-04-24T08:27:16-07:00
Authentication code: 1152

[SOLVED] Conditionally Adding Padding To Images

Post by chipncharge »

Okay, I've searched a good bit for this and I've not had any luck.

I process product images when they are uploaded to a website to fit inside a width of 600 x 600. My problem is that if an image is too short, I need to pad the image so that it is AT LEAST 250 pixels tall and 250 pixels wide. So, a picture that is processed to 600 x 77 should be padded vertically to 250 pixels, creating an image that is 600 x 250. Or, an image that is 120 x 600 would be processed to 250 x 600. The 250-pixel minimum is a Google requirement.

If I use "-extent 250x250^", the image is cropped to 250 x 250. How do I add padding only if it's needed without cropping it to 250 x 250?

Version ImageMagick 7.0.7-22 Q16 x64 on Windows Server 2008 R2 Datacenter.
Last edited by chipncharge on 2018-04-26T13:33:23-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditionally Adding Padding To Images

Post by fmw42 »

In IM 7, you can do conditional processing with %{fx: ..] calculations in line. So try these:

Code: Select all

magick -size 600x100 xc:black black.png
magick black.png -gravity center -background white -extent "%[fx:w<250?250:w]x%[fx:h<250?250:h]" result1.png

magick -size 100x600 xc:black black.png
magick black.png -gravity center -background white -extent "%[fx:w<250?250:w]x%[fx:h<250?250:h]" result2.png

magick -size 600x600 xc:black black.png
magick black.png -gravity center -background white -extent "%[fx:w<250?250:w]x%[fx:h<250?250:h]" result3.png
chipncharge
Posts: 6
Joined: 2018-04-24T08:27:16-07:00
Authentication code: 1152

Re: Conditionally Adding Padding To Images

Post by chipncharge »

Cool! Thanks! I will try that out.
chipncharge
Posts: 6
Joined: 2018-04-24T08:27:16-07:00
Authentication code: 1152

Re: Conditionally Adding Padding To Images

Post by chipncharge »

I can't get it to work when the system runs it. I believe it's an issue with the quotes around the conditions but I've tried escaping them a number of different ways and I still can't get it. Any ideas?

This runs (but doesn't do what I want):

Code: Select all

C:\path\cmd.exe /S /C ""C:\path\magick.exe" "C:\path\800x77.jpg" -resize 600x600 -strip -quality 85 -gravity center -background white -extent 250x250 "C:\path\result250x250.jpg""
This doesn't run at all. The error message is "The filename, directory name, or volume label syntax is incorrect.":

Code: Select all

C:\path\cmd.exe /S /C ""C:\path\magick.exe" "C:\path\800x77.jpg" -resize 600x600 -strip -quality 85 -gravity center -background white -extent "%[fx:w<250?250:w]x%[fx:h<250?250:h]"  "C:\path\result600x250.jpg""
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditionally Adding Padding To Images

Post by fmw42 »

One of the Windows users will need to help. I do not use Windows and am on Mac
chipncharge
Posts: 6
Joined: 2018-04-24T08:27:16-07:00
Authentication code: 1152

Re: Conditionally Adding Padding To Images

Post by chipncharge »

Okay. Thanks for your help!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Conditionally Adding Padding To Images

Post by snibgo »

Why are you using cmd.exe? You can if you want, and it will work, but it just adds a layer of complexity that you probably don't need.

I assume that "C:\path\magick.exe" actually exists.

Your second command has a "<" character, twice. This has a special meaning (redirection) that you don't want, so escape it with a "^" character.

If you run that command within a BAT script, don't forget to double each "%" sign.
snibgo's IM pages: im.snibgo.com
chipncharge
Posts: 6
Joined: 2018-04-24T08:27:16-07:00
Authentication code: 1152

Re: Conditionally Adding Padding To Images

Post by chipncharge »

"Why are you using cmd.exe?"

I'm not the original author of our system code so I don't know why but I'm looking into removing it. Thanks for the recommendation.

"I assume that "C:\path\magick.exe" actually exists."
Yes.

"Your second command has a "<" character, twice. This has a special meaning (redirection) that you don't want, so escape it with a "^" character."

That fixes it when I run it from the command prompt. Thanks! However, when the ColdFusion web app runs it, the image is cropped at 250x250 rather than the conditional padding I'm looking for. I'm guessing no one on here has experience running ImageMagick in CF. :lol:
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Conditionally Adding Padding To Images

Post by snibgo »

I know nothing about ColdFusion. Perhaps its syntax is more like bash, rather than Windows.
snibgo's IM pages: im.snibgo.com
chipncharge
Posts: 6
Joined: 2018-04-24T08:27:16-07:00
Authentication code: 1152

Re: Conditionally Adding Padding To Images

Post by chipncharge »

This was the final solution for anyone looking:

Code: Select all

C:\path\cmd.exe /S /C ""C:\path\magick.exe" "C:\path\800x77.jpg" -resize 600x600 -strip -quality 85 -gravity center -background white -extent ""%[fx:w^<250?250:w]x%[fx:h^<250?250:h]""  "C:\path\result600x250.jpg""
I had to double up the double quotes around the conditional code. Thanks both of you for the help!
Post Reply