Skip to content
Advertisement

imageMagick gif is moving out of frame

I have this simple code to rotate a static image with imageMagick on PHP

$url = file_get_contents('https://via.placeholder.com/100x100');

$image = new Imagick();
$image->readImageBlob($url);
$image->resizeImage(128, 128, Imagick::FILTER_UNDEFINED, 0, 0);


$GIF = new Imagick();
$GIF->setFormat("gif");

for ($i = 0; $i < 6; ++$i) {

    $image->setImageDispose(2);
    $image->rotateImage('transparent', $i * 6);
    $image->setImageDelay(20);
    $GIF->addImage($image);
    
}

header("Content-Type: image/gif");
echo $GIF->getImagesBlob();

For some reason, it is being offset with each new image, like this:

wrong rotation

How can I make it to stop moving out of frame?

Advertisement

Answer

I am not an expert in PHP Imagick, but your issue is that rotateImage() always seems to rotate about the top left corner rather than the center. This is contrary to how it is rotated about the center using -rotate in ImageMagick.

Here is alink to someones solution using DISTORTION_SCALEROTATETRANSLATE (int)

PHP – Rotate image on reference point

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement