PHP绘制花边广告展示条
PHP绘制花边广告展示条
利用PHP的GD库,可以动态绘制各种形状的广告条.
如给定文字和背景色,生成具有花边效果的图片banner.
示范代码如下(注意图片背景色为透明):
- $img = imagecreatetruecolor(200, 50);
- $imageX = imagesx($img);
- $imageY = imagesy($img);
- imagealphablending($img, false);
- imagesavealpha($img, true);
- $transparent = imagecolorallocatealpha($img, 255,255,255, 127);
- $white = imagecolorallocate($img, 255,255,255);
- $grey = imagecolorallocate($img, 127,127,127);
- imagefilledrectangle($img, 0, 0, $imageX, $imageY, $grey);
- imagefilledrectangle($img, 2, 2, $imageX-4, $imageY-4, $grey);
- $font = "./arialbd.ttf";
- $fontSize = 12;
- $text = "THIS IS A TEST";
- $textDim = imagettfbbox($fontSize, 0, $font, $text);
- $textX = $textDim[2] - $textDim[0];
- $textY = $textDim[7] - $textDim[1];
- $text_posX = ($imageX / 2) - ($textX / 2);
- $text_posY = ($imageY / 2) - ($textY / 2);
- imagefilledrectangle($img, 0, 0, $imageX, $imageY, $transparent);
- imagefilledrectangle($img, 0, 10, $imageX, $imageY-10, $grey);
- for ($i=0; $i<10; $i++) {
- imagefilledellipse($img, 10+$i*20, 10, 20, 20, $grey);
- }
- for ($i=0; $i<10; $i++) {
- imagefilledellipse($img, 10+$i*20, $imageY-10, 20, 20, $grey);
- }
- imagealphablending($img, true);
- imagettftext($img, $fontSize, 0, $text_posX, $text_posY, $white, $font, $text);
- header("Content-Type: image/png");
- imagepng($img);
效果图:
评论暂时关闭