问题
I'm using php Imagick class for a project
I try to composite an image changing the gravity of the image
What I mean is, I want to composite the target image to middle or to the top center
I use
....
$imageOrg->setGravity(imagick::GRAVITY_CENTER); //I wrote this for an example, position will be set by the visitor
$imageOrg->compositeImage($over, Imagick::COMPOSITE_DEFAULT, 0, 0);
....
But either setGravity() or setImageGravity() functions don't work.
Please help!
回答1:
$imageOrg->compositeImage($over, Imagick::COMPOSITE_DEFAULT, (((($imageOrg->getImageWidth()) - ($over->getImageWidth())))/2), (((($imageOrg->getImageHeight()) - ($over->getImageHeight())))/2));
Basically what you're doing is setting the left offset of your image to your Container's width, minus your composite image's width, divided by two, this will offset it enough to center horizontally. Then you do the exact same thing for the height, and it's centered vertically.
I had the same type of problem, best I can figure Gravity settings only apply to Drawing contexts, ie: Text, annotations
来源:https://stackoverflow.com/questions/11921433/php-imagick-setgravity-function-doesnt-work-with-compositeimage-function