Page 1 of 1
[SOLVED] Conditionally Adding Padding To Images
Posted: 2018-04-24T12:10:07-07:00
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.
Re: Conditionally Adding Padding To Images
Posted: 2018-04-24T12:45:42-07:00
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
Re: Conditionally Adding Padding To Images
Posted: 2018-04-24T13:51:52-07:00
by chipncharge
Cool! Thanks! I will try that out.
Re: Conditionally Adding Padding To Images
Posted: 2018-04-25T14:13:20-07:00
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""
Re: Conditionally Adding Padding To Images
Posted: 2018-04-25T14:52:32-07:00
by fmw42
One of the Windows users will need to help. I do not use Windows and am on Mac
Re: Conditionally Adding Padding To Images
Posted: 2018-04-25T15:26:28-07:00
by chipncharge
Okay. Thanks for your help!
Re: Conditionally Adding Padding To Images
Posted: 2018-04-25T18:39:06-07:00
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.
Re: Conditionally Adding Padding To Images
Posted: 2018-04-26T08:48:46-07:00
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.
Re: Conditionally Adding Padding To Images
Posted: 2018-04-26T12:36:07-07:00
by snibgo
I know nothing about ColdFusion. Perhaps its syntax is more like bash, rather than Windows.
Re: Conditionally Adding Padding To Images
Posted: 2018-04-26T13:32:53-07:00
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!