Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center crop with gravity using Imagick and PHP

Tags:

php

imagick

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)...

like image 520
rynop Avatar asked Nov 04 '11 16:11

rynop


2 Answers

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.

like image 88
Kapil Sharma Avatar answered Nov 01 '22 10:11

Kapil Sharma


Looks like there is not support, here is how I ended up doing it: https://gist.github.com/1364489

like image 3
rynop Avatar answered Nov 01 '22 10:11

rynop