Imagick: setting the gravity on a Imagick item

后端 未结 1 1723
死守一世寂寞
死守一世寂寞 2020-12-21 06:34

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

相关标签:
1条回答
  • 2020-12-21 07:07

    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);
    
    0 讨论(0)
提交回复
热议问题