手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

PHP绘制花边广告展示条

[日期:2012-08-23] 来源:Linux社区  作者:iefreer [字体:大 中 小]

利用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);  

效果图:

linux
相关资讯       PHP 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款