我试图在iMagick中执行以下操作,但我无法让它工作:
检查图像是否超过390像素高,如果图像高度超过390像素,如果图像不保持尺寸。
添加300px宽,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的大图像起作用。
大多数图像都很高而且很薄,但是有一些图像比它们高很宽。
我出错的任何想法?
谢谢,
瑞克
答案 0 :(得分:1)
马克的评论可能是更好的选择。范围尊重重力,并确保最终图像始终为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 );