我试图修剪图像中只有左侧和右侧使用ImageMagick和PHP可变数量的空白。有谁知道如何做到这一点(也许使用imagemagick以外的东西?)?如何在PHP中使用Imagemagick修剪图像的左侧和右侧?
下面是一个例子。
我有这两个图像:
每个具有被动态地在一个固定的宽度的图像创建的文本的可变量。 我需要做的是修剪掉右边的背景和左侧所以图像打印出来是这样的:
如果ImageMagick的不能做到这一点,我愿意用别的东西,但我将需要帮助,因为我不是一个程序员。谢谢!
这里是我当前的代码,修剪图像的所有方面:
<?php
/* Create the object and read the image in */
$i = '3';
$im = new Imagick("test".$i.".png");
/* Trim the image. */
$im->trimImage(0);
/* Ouput the image */
//header("Content-Type: image/" . $im->getImageFormat());
//echo $im;
/*** Write the trimmed image to disk ***/
$im->writeImage(dirname(__FILE__) . '/test'.$i.'.png');
/*Display Image*/
echo $img = "<img src=\"test".$i.".png\">";
?>
我能够使用以下代码修剪图像的右侧和左侧:exec('convert test4.png -gravity West -background white -splice 5x0 -background black -splice 5x0 -trim + repage -chop 5x0 test44。PNG');谢谢! exec('convert test44.png -gravity East -background white -splice 1x0 -background black -splice 1x0 -trim + repage -chop 1x0 test44.png'); – Nathan
您还可以将裁剪边组合成一个命令(这会更高效,并且会提供更好的质量),例如,对于右边距和底部边距:'convert -gravity北 - 背景白色拼接0x1 -trim + repage -chop 0x1 - 重力西 - 背景白-splice 1x0 -trim + repage -chop 1x0