侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

用PHP产生动态的影像图

php  /  管理员 发布于 7年前   162

很多人不了解  PHP 可以产生非HTML的资料.这是对产生影像图非常有用的.可以从 database 产生一个简单的广告横图或更简单只产生一个图形按钮 .

我用 TTF 字型在以下的范例中  
我通常取名作 'button.php3':

#######################################################
-----button.php3------
<?
  Header("Content-type: image/gif");
  if(!isset($s)) $s=11;
  $size = imagettfbbox($s,0,"fonts/TIMES.TTF",$text);
  $dx = abs($size[2]-$size[0]);
  $dy = abs($size[5]-$size[3]);
  $xpad=9;
  $ypad=9;
  $im = imagecreate($dx+$xpad,$dy+$ypad);
  $blue = ImageColorAllocate($im, 0x2c,0x6D,0xAF);
  $black = ImageColorAllocate($im, 0,0,0);
  $white = ImageColorAllocate($im, 255,255,255);
  ImageRectangle($im,0,0,$dx+$xpad-1,$dy+$ypad-1,$black);
  ImageRectangle($im,0,0,$dx+$xpad,$dy+$ypad,$white);
  ImageTTFText($im, $s, 0, (int)($xpad/2)+1, $dy+(int)($ypad/2), $black, "fonts/TIMES.TTF", $text);
  ImageTTFText($im, $s, 0, (int)($xpad/2), $dy+(int)($ypad/2)-1, $white, "fonts/TIMES.TTF", $text);
  ImageGif($im);
  ImageDestroy($im);
?>
#######################################################
很重要一点是你不能在这档案中放任何HTML tags.也不能有空白行在 <?和 ?> tag 之前或之後. 如果你用这段Script後看到一个不完整的影像, 表示你可能在PHP标签以外误打了字元.

以上的 script 可以由此语法在网页中叫出来: <IMG SRC="button.php3?s=36&text=PHP+is+Cool">  

#######################################################
----test.php-----  

<html>
<head>
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gb2312">
<title>New Page 1</title>
</head>

<body>  

<IMG SRC="button.php3?s=36&text=PHP+is+Cool">  

</body>
</html>
#######################################################  

test.php结果会像这样: .

's' 参数是设定字型大小 .  

这是当 s=18 时:  

注I:
字型路径 "/fonts/TIMES.TTF" 可由windows/fonts目录下取得 TIMS.TTF 字型档 Copy 至你网站的目录 fonts下即可测试 至於中文的表现 尚待各位网友提供心得  

注意 我先画了一个黑色方块区再用白色位移产生 3D 效果.也陬L法在浅色背景中看出来 但你可以把背景色改为深色看看此效果. 字型也做了同样效果表现立体感.  

你要先确定你的安装 PHP 时有设定支援  GD 和 TTF. 可参考  PHP FAQ . 我建议可以 copy libgd.a 到  /usr/local/lib 和 gd*.h 相关档案到  /usr/local/include 然後   
'make install' for FreeTTF library.  

可以在这http://rover.wiesbaden.netsurf.de/~kikita/ 找到钗httf 字型哦!

注:

以下的原始码改进了上面的弁?可多行文字显示:
#######################################################
--------------button.php-----------------
<?

Header("Content-type: image/jpeg");
if(!isset($bgred)) $bgred=0;
if(!isset($bggreen)) $bggreen=51;
if(!isset($bgblue)) $bgblue=153;
if(!isset($chred)) $chred=255;
if(!isset($chgreen)) $chgreen=255;
if(!isset($chblue)) $chblue=255;
if(!isset($shadow)) $shadow="yes";
if(!isset($wrappos)) $wrappos=20;
if(!isset($crop)) $crop=2.2;
if(!isset($jpegquality)) $jpegquality=80;
if(!isset($s)) $s=11;
$savetext=$text;
$text=wordwrap($text,$wrappos," ",0);
if (!isset($font)) $fontname="/www/ttfonts/arialbd.ttf";
else
$fontname="/www/ttfonts/".$font.".ttf";
$size = imagettfbbox($s,0,$fontname,$text);
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
$upper=abs($size[5]);
$under=$size[1];
$th=$upper-$under;
$xpad=9;
if (substr_count($text,chr(13))>=1)
{
  $mult=(substr_count($text,chr(13)));
  $ypad=($mult*$crop*$s)+$s;
}
else $ypad=($crop-2)*$s;
$im = imagecreate($dx+$xpad,$th+$ypad);
$color = ImageColorAllocate($im, $bgred,$bggreen,$bgblue);
$black = ImageColorAllocate($im, 0,0,0);
$fontcolor = ImageColorAllocate($im, $chred,$chgreen,$chblue);
ImageRectangle($im,0,0,$dx+$xpad-1,$th+$ypad-1,$black);
ImageRectangle($im,0,0,$dx+$xpad,$th+$ypad,$white);
if ($shadow=="yes")
ImageTTFText($im, $s, 0, (int)($xpad/2)-2+1, $th+2+(int)($ypad/2)-3, $black, $fontname, $text);
ImageTTFText($im, $s, 0, (int)($xpad/2)-2, $th+2+(int)($ypad/2)-1-3, $fontcolor, $fontname, $text);
Imagejpeg($im,"",$jpegquality);
ImageDestroy($im);

?>
#######################################################
这可以下面这个 form 来产生:
#######################################################
----------test.php--------------------
<html>

<head>
<title>New Page 1</title>
</head>

<body>

<form method="POST" action="button.php">
<p>文字<input type="text" name="text" size="60"></p>
<p>大小<input type="text" name="s" size="6" value="14"></p>
<p>断句的位置(wrap break position) <input type="text" name="wrappos" size="3" value="20"></p>
<p>背景颜色</p>
<p>红色<input type="text" name="bgred" size="6" value="0">     
     绿色<input type="text" name="bggreen" size="8" value="51">     
      蓝色<input type="text" name="bgblue" size="7" value="153"></p>
<p>字元颜色</p>
<p>红色 <input type="text" name="chred" size="6" value="255">   
      绿色 <input type="text" name="chgreen" size="8" value="255">     
     蓝色 <input type="text" name="chblue" size="7" value="255"></p>
<p>字型 <input type="text" name="font" size="20" value="arialbd"></p>
<p>阴影 <input type="radio" value="yes" checked name="shadow">是  
               <input type="radio" name="shadow" value="no">否</p>
<p>Crop size <input type="text" name="crop" size="20" value="2.2"></p>
<p>Jpeg 品质 (0-100) <input type="text" name="jpegquality" size="20" value="80"></p>
<p><input type="submit" value="Submit" name="B1">
      <input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
#######################################################

或是直接像上例一样呼叫:  

#######################################################
----test.php-----  

<html>
<head>
<title>New Page 1</title>
</head>

<body>  

<IMG SRC="button.php?s=36&text=PHP+is+Cool">  

</body>
</html> 


  • 上一条:
    最简单的PHP程序--记数器
    下一条:
    PHP4与PHP3中一个不兼容问题的解决方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客