开发者

PNG composition using GD and PHP

I am trying to take a rectangular png and add depth using GD by duplicating the 开发者_StackOverflow社区background and moving it down 1 pixel and right 1 pixel. I am trying to preserve a transparent background as well.

I am having a bunch of trouble with preserving the transparency.

Any help would be greatly appreciated.

Thanks!

    $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);


The solution to virtually any related problem with PHP GD is using this small utility:

http://phpimageworkshop.com/

It´s a PHP Class based on GD... but it´s has two "insignificant" differences:

Extremely easy

Always get the work done

Two days ago I have a similar problem (Join multiple PNG Images into a single one PNG using PHP) but this Library saves the day!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜