2012-09-28 83 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,大聲笑你可以瀏覽手冊,瞭解它是如何工作的 –