2012-09-28 79 views
0

我有一个带有alpha蒙版的PNG图像。我想为这个图像生成一个阴影,它会使用alpha蒙版。PHP:如何使用alpha蒙版为图像创建阴影

a picture is worth thousand words http://www.brunet.fr/alpha.jpg

我想我需要:

  1. 从第一图像
  2. 获取阿尔法遮掩用适当的颜色填充它,并对其进行模糊处理
  3. 创建一个新的空白白色图片
  4. 先用阴影组成它,第一张图片

但我找不到任何提示,特别是第一部分。

编辑:我发现答案的开头here我试试看,并让你知道。

感谢

+0

先提供您的PNG文件,请。 –

+0

[image example](http://crm.brunet.pro/images/produits/1565.png) – Matthieu

回答

1

首先,你需要安装一个名为php的分机: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; 
+0

这是工作!谢谢 !我仍然试图找出它是如何工作的,但它的工作原理 – Matthieu

+0

实际上,Image Magick shadow选项使用alpha层。这很简单! – Matthieu

+0

@Matthieu,大声笑你可以浏览手册,了解它是如何工作的 –