center crop with gravity using Imagick and PHP

rynop picture rynop · Nov 4, 2011 · Viewed 10.4k times · Source

I'm looking to center crop and image using Imagick PHP apis (not command line version of Imagick).

Essentially I want to do what is possible via command line, using API. Here is an example via command line: http://www.imagemagick.org/Usage/crop/#crop_gravity

Here is what I'm doing (not working). It always crops the upper left corner of the source:

        $this->imagickObj->setGravity(\Imagick::GRAVITY_CENTER);
        $this->imagickObj->cropImage(300,250,0,0);
        $this->imagickObj->setImagePage(0, 0, 0, 0);

Why is the setGravity not applying to the image before the crop? http://www.php.net/manual/en/function.imagick-setgravity.php says it should apply to the object (in this case the single image)...

Answer

Kapil Sharma picture Kapil Sharma · Sep 26, 2012

Its too late for the original person who asked the question but for future visitors, correct solution is

bool Imagick::cropThumbnailImage ( int $width , int $height )

Sorry for late reply but I too stuck here just 30 mins ago and first google result redirected me here. Hope same will not happen with others.