Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
750 views
in Technique[技术] by (71.8m points)

php - Imagick: setting the gravity on a Imagick item

I'm having some real difficulties setting the gravity of an image in Imagick.

I've managed to set the gravity of an ImaickDraw object but I've not been successful setting it in a Imagick object.

Below is the basic code I'm using that the moment. I've just used the same as for ImagickDraw but obviously it isn't working.

$rating = new Imagick("ratings/" . $rating . ".png");
$rating->setGravity (Imagick::GRAVITY_SOUTH);
$im->compositeImage($rating, imagick::COMPOSITE_OVER, 20, 20); 

Any ideas how to set the gravity for an exisiting image rather than a draw object?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your case setGravity method should be applied to $im object. But anyways it looks like the gravity affects only ImagickDraw objects, inserted with drawImage, and there's no way to put an image in a draw like you can do with ImageMagick commands.

So there's two ways to do this:

1st. If your hosting allows functions shell_exec or exec, you can run a command like.

convert image.jpg -gravity south -
  draw "image Over 0,0 0,0 watermak.png" 
  result.jpg`

2nd. Otherwise, you can calculate position of the image being placed on the base image and use compositeImage

$imageHight = $im->getImageHeight();
$imageWith = $im->getImageWidth();

// Scale the sprite if needed.
// Here I scale it to have a 1/2 of base image's width
$rating->scaleImage($imageWith / 2, 0);

$spriteWidth = $rating->getImageWidth();
$spriteHeight = $rating->getImageHeight();

// Calculate coordinates of top left corner of the sprite 
// inside of the image
$left = ($imageWidth - $spriteWidth)/2; // do not bother to round() values, IM will do that for you
$top = $imageHeight - $spriteHeight;

// If you need bottom offset to be, say, 1/6 of base image height,
// then decrease $top by it. I recommend to avoid absolute values here
$top -= $imageHeight / 6;

$im->compositeImages($rating, imagick::COMPOSITE_OVER, $left, $top);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...