PHP绘制花边广告展示条


利用PHP的GD库,可以动态绘制各种形状的广告条.

如给定文字和背景色,生成具有花边效果的图片banner.

示范代码如下(注意图片背景色为透明):

  1. $img = imagecreatetruecolor(200, 50);  
  2.   
  3. $imageX = imagesx($img);  
  4. $imageY = imagesy($img);  
  5.   
  6. imagealphablending($img, false);  
  7. imagesavealpha($img, true);  
  8.   
  9. $transparent = imagecolorallocatealpha($img, 255,255,255, 127);  
  10. $white = imagecolorallocate($img, 255,255,255);  
  11. $grey = imagecolorallocate($img, 127,127,127);  
  12. imagefilledrectangle($img, 0, 0, $imageX, $imageY, $grey);  
  13. imagefilledrectangle($img, 2, 2, $imageX-4, $imageY-4, $grey);  
  14.   
  15. $font = "./arialbd.ttf";  
  16. $fontSize = 12;  
  17. $text = "THIS IS A TEST";  
  18.   
  19. $textDim = imagettfbbox($fontSize, 0, $font, $text);  
  20. $textX = $textDim[2] - $textDim[0];  
  21. $textY = $textDim[7] - $textDim[1];  
  22.   
  23. $text_posX = ($imageX / 2) - ($textX / 2);  
  24. $text_posY = ($imageY / 2) - ($textY / 2);  
  25.   
  26. imagefilledrectangle($img, 0, 0, $imageX, $imageY, $transparent);  
  27. imagefilledrectangle($img, 0, 10, $imageX, $imageY-10, $grey);  
  28. for ($i=0; $i<10; $i++) {  
  29.     imagefilledellipse($img, 10+$i*20, 10, 20, 20, $grey);  
  30. }  
  31. for ($i=0; $i<10; $i++) {  
  32.     imagefilledellipse($img, 10+$i*20, $imageY-10, 20, 20, $grey);  
  33. }  
  34. imagealphablending($img, true);  
  35. imagettftext($img, $fontSize, 0, $text_posX, $text_posY, $white, $font, $text);  
  36.   
  37. header("Content-Type: image/png");  
  38. imagepng($img);  

效果图:

相关内容