netcase
netcase

Reputation: 1359

imagemagick skew or distort an image

Well how could I change the before image to the after image by using imagemagick? Is it the -skew command or the -distort, and how can I use it preferably in typo3 and php?

Any help is appreciated!

before and after

Upvotes: 5

Views: 6955

Answers (3)

Bonzo
Bonzo

Reputation: 5299

Using Imagemagick with php and the command line:

// Working on the original image size of 400 x 300
$cmd = "before.jpg -matte -virtual-pixel transparent".  
" +distort Perspective \"0,0 0,0  400,0 400,22  400,300 400,320  0,300 0,300 \" "; 
exec("convert $cmd perspective.png");

Note: 1/ This is for later versions of Imagemagick - the perspective operator has change. 2/ You need to use +distort not -distort as the image is larger than the initial image boundrys.

Examples of Imagemagick with php usage on my site http://www.rubblewebs.co.uk/imagemagick/operator.php

Upvotes: 9

tmt
tmt

Reputation: 8614

Perspective distortion should give you what you want. Example:

convert original.png -matte -virtual-pixel white +distort Perspective '0,0,0,0 0,100,0,100 100,100,90,110 100,0,90,5' distorted.png

In TYPO3 you could apply it by (ab)using the SCALE object of the GIFBUILDER. Example:

temp.example = IMAGE
temp.example {  
  file = GIFBUILDER
  file {
    format = jpg
    quality = 100
    maxWidth = 9999
    maxHeight = 9999
    XY = [10.w],[10.h]

    10 = IMAGE
    10.file = fileadmin/original.png

    20 = SCALE
    20 {
      params = -matte -virtual-pixel white +distort Perspective '0,0,0,0 0,100,0,100 100,100,90,110 100,0,90,5'
    }
  }
}

Upvotes: 4

dank.game
dank.game

Reputation: 4719

I think what you're looking for is the Imagick::shearImage function. This creates a checkerboard square and distorts it into a parallelogram (save this as a PHP file and open in your browser to see):

<?php
$im = new Imagick();
$im->newPseudoImage(300, 300, "pattern:checkerboard");
$im->setImageFormat('png');
$im->shearImage("transparent", 0, 10);
header("Content-Type: image/png");
echo $im;
?>

Within a larger script, to shear an image named myimg.png and save it as myimg-sheared.png, you can use:

$im = new Imagick("myimg.png");
$im->shearImage("transparent", 0, 10);
$im->writeImage("myimg_sheared.png");

If shearImage isn't versatile enough, you can try the Imagick::DISTORTION_PERSPECTIVE method via the Imagick::distortImage function.

Upvotes: 4

Related Questions