Shink image if width is larger than ...

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
mgk89
Posts: 3
Joined: 2016-04-28T02:34:25-07:00
Authentication code: 1151

Shink image if width is larger than ...

Post by mgk89 »

Hello,

I'm new to ImageMagick and I'm having troubles with shrinking:

I would like to shrink the image if the width is larger than 460px.
by that code it resizes all images to width 460px.
I tried several combinations with 460x> and 460x\> and 460x\\> as I found it on the web as suggestions, but that does not work.

Code: Select all

exec("convert $new_path -resize 460x -auto-orient $new_path");
I'm using xampp running on windows.

Thanks in advance and sorry for a beginner's question.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Shink image if width is larger than ...

Post by snibgo »

It works fine, for example (Windows CMD window):

Code: Select all

f:\web\im>%IM%convert -size 200x300 xc: -resize "460x>" info:
xc: XC 200x300 200x300+0+0 16-bit sRGB 0.000u 0:00.000

f:\web\im>%IM%convert -size 500x300 xc: -resize "460x>" info:
xc: XC 460x276 460x276+0+0 16-bit sRGB 0.000u 0:00.000

f:\web\im>%IM%convert -size 200x600 xc: -resize "460x>" info:
xc: XC 200x600 200x600+0+0 16-bit sRGB 0.000u 0:00.000

f:\web\im>%IM%convert -size 500x600 xc: -resize "460x>" info:
xc: XC 460x552 460x552+0+0 16-bit sRGB 0.125u 0:00.016
You problem may be in the escaping of ">". Is that PHP? I don't know what (if any) escaping should be.
snibgo's IM pages: im.snibgo.com
mgk89
Posts: 3
Joined: 2016-04-28T02:34:25-07:00
Authentication code: 1151

Re: Shink image if width is larger than ...

Post by mgk89 »

Yes it's PHP. Actually I do not really know how to use the ' and " correctly.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Shink image if width is larger than ...

Post by Bonzo »

You need to use " on windows instead of '

I would try:

Code: Select all

exec("convert $new_path -resize \"460x>\" -auto-orient $new_path");
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Shink image if width is larger than ...

Post by snibgo »

Thanks, Bonzo. I notice you escape the double-quotes, rather than the greater-than. I assume this is so the PHP interpreter doesn't strip the quotes, so the quotes are passed to the Windows command interpreter, so that will not interpret the greater-than as meaning a redirection, and will pass it to ImageMagick.
snibgo's IM pages: im.snibgo.com
mgk89
Posts: 3
Joined: 2016-04-28T02:34:25-07:00
Authentication code: 1151

Re: Shink image if width is larger than ...

Post by mgk89 »

Thank you guys, the code Bonzo suggested works like a charm! Thanks a lot!!!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Shink image if width is larger than ...

Post by Bonzo »

I do not know if it matters in this case but variables in double quotes are evaluated? but in single quotes they are treated literally.

Not a great example but you will get the idea:

Code: Select all

$input = 'image.jpg';
echo "The image is $input";
// Output: The image is image.jpg
echo 'The image is $input';
// Output: The image is $input
Also in the example we have to use " " with the exec and the " " around the 460x> would cause an error. So escaping them treats them as a " and not as the end of the exec command.

Not explained very well but that is how I understand it.

I also tend to err on the safe side when writing my code to make it easier to understand. We may have been able to get away with { } but I have never tried it.

For example when I have variables for the size this will cause an error $widthx$height but {$width}x{$height} is OK

I hope I have not confused you but I do not do a lot of programming now and have methods I have used for years that I know work!
Post Reply