php imagick 图片合成,PHP: Imagick::compositeImage - Manual

Here is an example on how to compose two images into a single one. The Imagick class utilises the exception handling model introduced in PHP5 and thus we will do that as well. Let's presume, that we have a directory in our filesystem, which contains our program and the two images we want to operate on.

{// Let's check whether we can perform the magick.if (TRUE!==extension_loaded('imagick'))

{

throw newException('Imagick extension is not loaded.');

}// This check is an alternative to the previous one.

// Use the one that suits you better.if (TRUE!==class_exists('Imagick'))

{

throw newException('Imagick class does not exist.');

}// Let's find out where we are.$dir=dirname(__FILE__);// Let's read the images.$glasses= newImagick();

if (FALSE===$glasses->readImage($dir.'/glasses.png'))

{

throw newException();

}$face= newImagick();

if (FALSE===$face->readImage($dir.'/face.jpg'))

{

throw newException();

}// Let's put the glasses on (10 pixels from left, 20 pixels from top of face).$face->compositeImage($glasses,Imagick::COMPOSITE_DEFAULT,10,20);// Let's merge all layers (it is not mandatory).$face->flattenImages();// We do not want to overwrite face.jpg.$face->setImageFileName($dir.'/face_and_glasses.jpg');// Let's write the image.if  (FALSE==$face->writeImage())

{

throw newException();

}

}

catch (Exception $e)

{

echo'Caught exception: '.$e->getMessage() ."\n";

}

exit(0);?>Also a couple more words on the Imagick::COMPOSITE_DEFAULT argument. The images we are composing together are separate layers. Not only can we put them in specific order, but we can also choose the way we want them to interfere with each other. And here comes the second argument of the compositeImage method. It can be given either as a constant or as the integer value of that constant. You can use the reflection API of PHP5 to get the list of them.

Reflection::export(newReflectionClass('Imagick'));?>

Just look for COMPOSITE_* constants in the "Constants" section.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值