PHP :如何使用 alpha 蒙版为图像创建阴影



我有一个带有阿尔法蒙版的PNG图像。我想为这个图像生成一个阴影,它将使用 alpha 蒙版。

一张图片胜过千言万语 http://www.brunet.fr/alpha.jpg

我想我需要:

  1. 从第一张图像中获取 Alpha 蒙版
  2. 用适当的颜色填充它,并模糊它
  3. 创建新的空白白色图像
  4. 首先用阴影和第一个图像构图

但我在任何地方都找不到一些提示,尤其是第一部分。

编辑:我在这里找到了答案的开始,我尝试一下并让您知道。

谢谢

首先,你需要安装名为 php ext : Imagick

<?php 
/* Read the image into the object */ 
$im = new Imagick( 'a.png' ); 
$im->setImageFormat("png"); 
/* Make the image a little smaller, maintain aspect ratio */ 
$im->thumbnailImage( 200, null ); 
/* Clone the current object */ 
$shadow = $im->clone(); 
/* Set image background color to black 
        (this is the color of the shadow) */ 
$shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) ); 
/* Create the shadow */ 
$shadow->shadowImage( 80, 3, 5, 5 ); 
/* Imagick::shadowImage only creates the shadow. 
        That is why the original image is composited over it */ 
$shadow->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 ); 
/* Display the image */ 
header( "Content-Type: image/png" ); 
echo $shadow; 

最新更新