Resize while keeping aspect ratio with some options

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
nilambara
Posts: 5
Joined: 2014-11-20T20:49:55-07:00
Authentication code: 6789

Resize while keeping aspect ratio with some options

Post by nilambara » 2019-11-25T17:22:44-07:00

Hello,

I'm getting started with the API and finding some difficulty figuring out how some calls map to command-line parameters. IM 7.0.9 on macOS. The command I'm trying to replicate:

Code: Select all

magick convert -resize 160x160 -background transparent -colorspace srgb -depth 8 -gravity center -extent 160x160 -strip file.pdf[0] …
Command-line parameters are well documented though it seems that the API is less so. For example, I would expect that -resize would act like ResizeImage and allow me to preserve aspect ratio. As the API develops, maybe it would be a good idea to be able to make available all possible command-line options through the API if they aren't yet.

I'm using Go Imagick, the equivalent C commands are listed as comments:

Code: Select all

_ = mw.SetDepth(8)
_ = mw.SetExtract("160x160") // MagickSetExtract

mw.ReadImage(filePath + "[0]")
	
pixel := imagick.NewPixelWand()
pixel.SetColor("none")
_ = mw.SetImageBackgroundColor(pixel) // MagickSetImageBackgroundColor

_ = mw.SetImageGravity(imagick.GRAVITY_CENTER) // MagickSetImageGravity
_ = mw.SetImageExtent(160, 160)                // MagickSetImageExtent

_ = mw.StripImage()
MagickSetExtract allows me to resize without needing to calculate aspect ratio so I'm using that. Background using either MagickSetGravity or MagickSetImageGravity, and SetImageGravity seem to have no effect. I get a white background with the image left-aligned. I think there was some older thread about gravity not having an effect. I'm not sure if I'm missing something or there are some things not yet implemented. Help would be great.

I'm also open to suggestions if the command should be different, e.g, it seems I need to specify colorspace for it to render correctly.

snibgo
Posts: 12400
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resize while keeping aspect ratio with some options

Post by snibgo » 2019-11-25T17:34:04-07:00

You posted in a MagickWand subforum. MagickWand is an API for ImageMagick, maintained by the same team.

However, your query is actually about IMagick. This is a separate product, that provides a PHP interface to ImageMagick. IMagick is maintained by a separate team. IMagick may not have an API for all features of ImageMagick, especially recent features.

There is a separate subforum for IMagick, so I have moved your question there.

I know almost nothing about IMagick. See its documentation, eg https://phpimagick.com/Imagick/resizeIm ... cropZoom=1
snibgo's IM pages: im.snibgo.com

nilambara
Posts: 5
Joined: 2014-11-20T20:49:55-07:00
Authentication code: 6789

Re: Resize while keeping aspect ratio with some options

Post by nilambara » 2019-11-25T17:40:06-07:00

I am using the Go package. As there wasn't a forum for it, and all the commands map to C, I had posted there.

snibgo
Posts: 12400
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resize while keeping aspect ratio with some options

Post by snibgo » 2019-11-25T17:59:14-07:00

The best documentation for MagickWand functions is in the source code. See https://github.com/ImageMagick/ImageMag ... MagickWand . For example, MagickResizeImage() is in magick-image.c. There is generally one MagickWand function per CLI operation.

For further detail, you can go down one level to MagickCore. For example, MagickResizeImage() calls the MagickCore function ResizeImage in resize.c. There you will find all the gory detail.
snibgo's IM pages: im.snibgo.com

Post Reply