歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> PHP繪制花邊廣告展示條

PHP繪制花邊廣告展示條

日期:2017/3/1 10:11:59   编辑:Linux編程

利用PHP的GD庫,可以動態繪制各種形狀的廣告條.

如給定文字和背景色,生成具有花邊效果的圖片banner.

示范代碼如下(注意圖片背景色為透明):

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

效果圖:

Copyright © Linux教程網 All Rights Reserved