Page 1 of 1
Shink image if width is larger than ...
Posted: 2016-04-28T02:39:01-07:00
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.
Re: Shink image if width is larger than ...
Posted: 2016-04-28T03:31:29-07:00
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.
Re: Shink image if width is larger than ...
Posted: 2016-04-28T03:50:29-07:00
by mgk89
Yes it's PHP. Actually I do not really know how to use the ' and " correctly.
Re: Shink image if width is larger than ...
Posted: 2016-04-28T04:34:15-07:00
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");
Re: Shink image if width is larger than ...
Posted: 2016-04-28T05:07:31-07:00
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.
Re: Shink image if width is larger than ...
Posted: 2016-04-28T05:39:23-07:00
by mgk89
Thank you guys, the code Bonzo suggested works like a charm! Thanks a lot!!!
Re: Shink image if width is larger than ...
Posted: 2016-04-28T09:58:36-07:00
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!