MeV
MeV

Reputation: 3958

PHP - Rotate image on reference point

I am wondering if it is possible to rotate an image using a reference point.

For example, rotating this image by 30 degrees:

enter image description here

instead of having:

enter image description here

I would like to have:

enter image description here

which means rotating the image on the bottom-left point.

I would do this overlapping the rectangle to the original image if needed, and rotating it as needed only after.

Upvotes: 1

Views: 1035

Answers (1)

iainn
iainn

Reputation: 17417

The ImageMagick extension can do this, using the DISTORTION_SCALEROTATETRANSLATE option. You might need to tweak the coordinates and angle a bit to fit your needs.

<?php
$im = new Imagick('sample.png'); 

$args = array( 
    0, // X-point
    300, // Y-point
    1,   // Scale
    -45, // Rotation
); 

$im->distortImage(Imagick::DISTORTION_SCALEROTATETRANSLATE, $args, false);

$im->setImageFormat('png');
file_put_contents('rotated.png', (string) $im);

Upvotes: 4

Related Questions