Aleksei Kornushkin
Aleksei Kornushkin

Reputation: 2279

imagick::distortimage

I'm going to set image perspective. I have image of laptop with blank polygon enter image description here

Another image needs to be pulled on a blank area. Like this: enter image description here

So, I have this code for dynamical distortion:

$controlPoints = array( 0, 0,
                             0, 0,
                             0, $im->getImageHeight(),
                             0, $im->getImageHeight(),
                             $im->getImageWidth(), 0,
                             $im->getImageWidth(), 0,
                             $im->getImageWidth(), $im->getImageHeight(),
                            $im->getImageWidth(), $im->getImageHeight());
     /* Perform the distortion */ 
     $im->distortImage(Imagick::DISTORTION_PERSPECTIVE, $controlPoints, true);

How can I set $controlPoints array? I can't just set 4 coordinates to each corner of image? Unfortunately, documention for imageick::distort image is poor.

Problem is solved by using another distortion method:

$im->cropImage( 125, 121, $center_x, $center_y ); 
     $controlPoints = array(
                    0,0, 35,20, # top left 
                    190,0, 150,30, # top right
                    0,205, -16,105, # bottom right
                    176,135, 115,105 # bottum left
                    );
     /* Perform the distortion */ 
     $im->distortImage(Imagick::DISTORTION_BILINEAR, $controlPoints, true);

Upvotes: 1

Views: 3077

Answers (1)

Dan Caragea
Dan Caragea

Reputation: 1794

The control points should be pairs of 4, as many as you need but at least 3 pairs. The meaning of control points is source_x, source_y, destination_x, destination_y

So it basically tells where should points from the source image go in the destination image.

In your case you will need 4 pairs, one for each corner of the rectangle:

$controlPoints = array(
    0, 0, <destination_x>, <destination_y>,
    0, $im->getImageHeight(), <destination_x>, <destination_y>,
    $im->getImageWidth(), 0, <destination_x>, <destination_y>,
    $im->getImageWidth(), $im->getImageHeight(), <destination_x>, <destination_y>
);

Obviously, you will have to figure out each destination coordinate and replace in the array above.

Upvotes: 11

Related Questions