【发布时间】:2017-03-14 10:33:37
【问题描述】:
我正在 iMagick 中尝试执行以下操作,但无法正常工作:
检查图像是否超过 390 像素高,如果是,则将其调整为 390 像素高,如果不保持尺寸。
添加一个 300px 宽 x 400px 高的白色画布,然后将图像放在画布的中心。
我的代码是:
$im = new Imagick("test.jpg");
$imageprops = $im->getImageGeometry();
$width = $imageprops['width'];
$height = $imageprops['height'];
if($height > '390'){
$newHeight = 390;
$newWidth = (390 / $height) * $width;
}else{
$newWidth = $imageprops['width'];
$newHeight = $imageprops['height'];
}
$im->resizeImage($newWidth,$newHeight, Imagick::FILTER_LANCZOS, 0.9, true);
$canvas = new Imagick();
$canvas->newImage(300, 400, 'white', 'jpg');
$canvas->compositeImage($im, Imagick::COMPOSITE_OVER, 100, 50);
$canvas->writeImage( "test-1.jpg" );
生成图像时,由于某种原因,大的图像被缩放到 388 像素高,而小的图像则保留其原始尺寸。
在画布上的放置总是不正确,尽管在合成图像中添加了 100,50 的大图像上确实有效。
大多数图像又高又瘦,但也有一些图像比高宽。
任何想法我哪里出错了?
谢谢,
瑞克
【问题讨论】:
-
您可能会发现将重力设置为 CENTER,将背景颜色设置为白色,然后使用
setImageExtent()将图像放在 300x400 的白色画布上会更容易。 -
if($height>390)不带引号。
标签: php imagemagick imagick