3

Imagick で画像の重力を設定するのが本当に難しいです。

ImaickDraw オブジェクトの重力を設定することはできましたが、Imagick オブジェクトで設定することに成功していません。

以下は、私が現在使用している基本的なコードです。ImagickDraw と同じものを使用しましたが、明らかに機能していません。

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

描画オブジェクトではなく、既存の画像の重力を設定する方法はありますか?

ありがとう!

4

1 に答える 1

4

あなたの場合、setGravityメソッドはオブジェクトに適用する必要があり$imます。しかし、とにかく、重力は で挿入された ImagickDraw オブジェクトにのみ影響するように見えます。ImageMagickdrawImageコマンドでできるように、画像を描画に入れる方法はありません。

したがって、これを行うには 2 つの方法があります。

1位。ホスティングで関数shell_execまたはが許可されている場合execは、次のようなコマンドを実行できます。

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

2番目。それ以外の場合は、ベース画像に配置されている画像の位置を計算して使用できます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);
于 2011-07-31T10:07:56.473 に答える