使用GD和PHP的PNG组合

时间:2010-06-14 21:22:24

标签: php transparency gd alpha

我正在尝试采用矩形png并通过复制背景并将其向下移动1像素和右1像素来使用GD添加深度。我也想保留一个透明的背景。

我在保持透明度方面遇到了一些麻烦。

非常感谢任何帮助。

谢谢!

    $obj = imagecreatefrompng('rectangle.png');
    $depth = 5;
    $obj_width = imagesx($obj);  
    $obj_height = imagesy($obj); 
    imagesavealpha($obj, true); 
        for($i=1;$i<=$depth;$i++){
            $layer = imagecreatefrompng('rectangle.png');
            imagealphablending( $layer, false );
            imagesavealpha($layer, true);

            $new_obj = imagecreatetruecolor($obj_width+$i,$obj_height+$i);
            $new_obj_width = imagesx($new_obj);  
            $new_obj_height = imagesy($new_obj); 
            imagealphablending( $new_obj, false );
            imagesavealpha($new_obj, true);

            $trans_color = imagecolorallocatealpha($new_obj, 0, 0, 0, 127);
            imagefill($new_obj, 0, 0, $trans_color);

            imagecopyresampled($new_obj, $layer, $i, $i, 0, 0, $obj_width, $obj_height, $obj_width, $obj_height);
            //imagesavealpha($new_obj, true); 
            //imagesavealpha($obj, true); 
        }
    header ("Content-type: image/png");
    imagepng($new_obj);
    imagedestroy($new_obj);

1 个答案:

答案 0 :(得分:0)

PHP GD几乎任何相关问题的解决方案都是使用这个小实用程序:

  

http://phpimageworkshop.com/

这是一个基于GD的PHP类...但它有两个“无关紧要”的差异:

非常容易

始终完成工作

两天前我遇到了类似的问题(Join multiple PNG Images into a single one PNG using PHP),但这个图书馆可以节省一天的时间!