dougu4027 2017-03-14 10:33
浏览 348

调整图像大小并放置在画布中心

I'm trying to do the following in iMagick and I can't get it to work:

Check if image is over 390 pixels high, if it is then resize it to 390 pixels high, if it isn't keep the dimensions.

Add a white canvas, 300px wide by 400px high and then place the image into the center of that.

My Code is:

$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" );

When the images are produced the large ones are scaled to 388 pixels high for some reason and the small ones are left to their original dimensions.

The placing on the canvas is always incorrect although does work on the large images with 100,50 added to the composite image.

Most of the images are tall and thin however there are a few that are wider than they are tall.

Any ideas where I am going wrong ?

Thanks,

Rick

  • 写回答

1条回答 默认 最新

  • dro44817 2017-03-14 15:14
    关注

    Mark's comments may be the better option. Extent respects the gravity, and ensures that the finial image will always be 300x400. For placing the image in the center using Imagick::compositeImage, you'll need to calculate the offset -- which is easy as you already have the width/height of the subject & canvas images.

    $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 );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?