Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php imagick setGravity function doesn't work with compositeImage() function

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!

like image 774
Onur Kucukkece Avatar asked Aug 12 '12 10:08

Onur Kucukkece


People also ask

What does PHP imagick do?

Imagick is a PHP extension to create and modify images using the ImageMagick library. There is also a version of Imagick available for HHVM. Although the two extensions are mostly compatible in their API, and they both call the ImageMagick library, the two extensions are completely separate code-bases.

What is the use of imagick?

ImageMagick is a software suite that creates, edits, composes, or converts bitmap images. It can read and write images in over 200 formats. We may use ImageMagick commands to resize, rotate, and transform images, adjust image colors, apply various special effects, or draw text, lines, and shapes.

What is imagick library?

Imagick and GD are popular image optimization libraries in PHP applications. While they both perform many of the same actions, they operate a bit different from each other and offer different benefits. Overall, these two libraries are used for: Resizing/cropping images. Modifying the contents of an image.


1 Answers

$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

like image 137
fire Avatar answered Oct 16 '22 16:10

fire