我正在尝试在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" );
当图像被生成时,由于某种原因,大的图像会缩放至388像素高度而小的图像则保持其原始尺寸。
虽然在大型图像上添加100、50后可以正常放置在画布上,但放置位置始终不正确。
大多数图像都是高而瘦的,但也有一些比它们高宽比更大。
请问我做错了哪些地方?
谢谢,
Rick
setImageExtent()
将图像放在300x400的白色画布上会更容易。 - Mark Setchell