ImageMagick 如何扭曲或扭斜一张图片?

5

我该如何使用imagemagick将图片从before image变为after image呢?是使用-skew命令还是-distort命令?最好如何在typo3和php中使用呢?

非常感谢您的帮助!

before and after


我认为您忘记接受一个答案了。例如Bonzo的答案 - 我知道它是有效的。 - Kurt Pfeifle
3个回答

9

使用Imagemagick与php和命令行:

// 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");

注意: 1. 这是针对较新版本的Imagemagick - 透视运算符已更改。 2. 您需要使用+distort而不是-distort,因为图像比初始图像边界要大。
在我的网站上,使用php的Imagemagick示例:http://www.rubblewebs.co.uk/imagemagick/operator.php

4
我认为你要找的是Imagick::shearImage函数。它会创建一个棋盘格方块并将其扭曲成平行四边形(将此保存为 PHP 文件并在浏览器中打开以查看):
<?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;
?>

在一个更大的脚本中,要将名为myimg.png的图像剪切并保存为myimg-sheared.png,可以使用以下命令:
$im = new Imagick("myimg.png");
$im->shearImage("transparent", 0, 10);
$im->writeImage("myimg_sheared.png");

如果shearImage不够灵活,您可以尝试使用Imagick::distortImage函数中的Imagick::DISTORTION_PERSPECTIVE方法。

4

透视畸变 可以给你想要的效果。例如:

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

在TYPO3中,您可以通过滥用GIFBUILDERSCALE对象来应用它。例如:
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'
    }
  }
}

我在Typo3中使用imageMagickExec函数尝试扭曲图像,但是没有效果,而旋转等其他操作却可以。你有什么想法吗? - netcase
通过执行我提供的第一段代码,在控制台中测试IM是否按照您的要求运行。它应该会给您几乎想要的结果,因为我已经测试过这段代码。如果不是这样,您将需要检查IM的安装情况。如果是这样,请测试我提供的第二段TypoScript代码。不过我没有测试过那个。 - tmt
谢谢你的帮助。我正在与IM作斗争,仍然无法使扭曲运行,而旋转和其他功能却可以正常工作。 - netcase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接