php – 用imagick传播图像
作者:互联网
尝试拍摄矩形照片,将其裁剪成方形区域,然后将其遮盖成具有透明背景的圆形.
//$dims is an array with the width, height, x, y of the region in the rectangular image (whose path on disk is $tempfile)
$circle = new \Imagick();
$circle->newImage($dims['w'], $dims['h'], 'none');
$circle->setimageformat('png');
$circle->setimagematte(true);
$draw = new \ImagickDraw();
$draw->setfillcolor('#ffffff');
$draw->circle($dims['w']/2, $dims['h']/2, $dims['w']/2, $dims['w']);
$circle->drawimage($draw);
$imagick = new \Imagick();
$imagick->readImage($tempfile);
$imagick->setImageFormat( "png" );
$imagick->setimagematte(true);
$imagick->cropimage($dims['w'], $dims['h'], $dims['x'], $dims['y']);
$imagick->compositeimage($circle, \Imagick::COMPOSITE_DSTIN, 0, 0);
$imagick->writeImage($tempfile);
$imagick->destroy();
结果是矩形图像,未被切割并且没有被环化.我究竟做错了什么?
示例图片:
$dims的示例输入= {“x”:253,“y”:0,“x2”:438.5,“y2”:185.5,“w”:185.5,“h”:185.5}
粗略的预期产量:
图像我看起来大致像输入图像.
解决方法:
这对我有用:
<?php
//$dims is an array with the width, height, x, y of the region in the rectangular image (whose path on disk is $tempfile)
$tempfile = 'VDSlU.jpg';
$outfile = 'blah.png';
$circle = new Imagick();
$circle->newImage(185.5, 185.5, 'none');
$circle->setimageformat('png');
$circle->setimagematte(true);
$draw = new ImagickDraw();
$draw->setfillcolor('#ffffff');
$draw->circle(185.5/2, 185.5/2, 185.5/2, 185.5);
$circle->drawimage($draw);
$imagick = new Imagick();
$imagick->readImage($tempfile);
$imagick->setImageFormat( "png" );
$imagick->setimagematte(true);
$imagick->cropimage(185.5, 185.5, 253, 0);
$imagick->compositeimage($circle, Imagick::COMPOSITE_DSTIN, 0, 0);
$imagick->writeImage($outfile);
$imagick->destroy();
?>
<img src="blah.png">
我总是试着保持代码简单,直到我让它工作,然后添加所有变量等.这可能是问题,或者你的Imagick版本可能有问题.
It’s namespaced
还是不知道这意味着什么! – 我有点落后于PHP,因为这些天我没有使用它.
标签:imagick,php 来源: https://codeday.me/bug/20190929/1830330.html