【问题标题】:PHP : How to create a shadow to an image using an alpha maskPHP:如何使用 alpha 蒙版为图像创建阴影
【发布时间】:2012-09-28 22:41:34
【问题描述】:

我有一个带有 alpha 蒙版的 PNG 图像。我想为这张图片生成一个阴影,它会使用 alpha 蒙版。

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

我想我需要:

  1. 从第一张图片中获取 alpha 蒙版
  2. 用适当的颜色填充,然后进行模糊处理
  3. 创建一个新的空白图像
  4. 先用阴影,再用第一张图片合成

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

编辑:我找到了答案here 的开头,我试了一下,然后告诉你。

谢谢

【问题讨论】:

标签: php image-processing gd alpha-transparency


【解决方案1】:

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

<?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; 

【讨论】:

  • 这行得通!谢谢 !我仍在试图弄清楚它是如何工作的,但它确实有效
  • 其实Image Magick阴影选项使用的是alpha层。就这么简单!
  • @Matthieu ,哈哈,你可以浏览手册了解它是如何工作的
猜你喜欢
  • 2011-09-18
  • 2011-03-22
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
  • 2013-07-20
  • 2011-11-09
  • 1970-01-01
相关资源
最近更新 更多