Ric*_*ans 4 php imagemagick imagick
我正在尝试在 iMagick 中执行以下操作,但无法使其正常工作:
检查图像是否超过 390 像素高,如果是,则将其调整为 390 像素高,如果不是保持尺寸。
添加一个白色画布,宽 300 像素,高 400 像素,然后将图像放在画布的中心。
我的代码是:
$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" );
Run Code Online (Sandbox Code Playgroud)
生成图像时,由于某种原因,大图像被缩放到 388 像素高,小图像保留其原始尺寸。
尽管在将 100,50 添加到合成图像的大图像上确实有效,但在画布上的放置始终是不正确的。
大多数图像都是又高又瘦,但也有一些比高度更宽。
我哪里出错了?
谢谢,
瑞克
马克的评论可能是更好的选择。Extent 尊重重力,并确保最终图像始终为 300x400。要使用 将图像放置在中心Imagick::compositeImage,您需要计算偏移量 - 这很容易,因为您已经拥有主题和画布图像的宽度/高度。
$canvas = new Imagick();
$finalWidth = 300;
$finalHeight = 400;
$canvas->newImage($finalWidth, $finalHeight, 'white', 'jpg' );
$offsetX = (int)($finalWidth / 2) - (int)($newWidth / 2);
$offsetY = (int)($finalHeight / 2) - (int)($newHeight / 2);
$canvas->compositeImage( $im, imagick::COMPOSITE_OVER, $offsetX, $offsetY );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2157 次 |
| 最近记录: |