网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > PHP技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.如何对PHP程序中的常见漏洞进行攻.
.PEAR:常用模块.
.oracle资料库函式库.
.PHP变量命名建议.
.理解PHP中的MVC编程之控制器.
.php中文乱码问题.
.解决phpMyAdmin2.6以上版本数据乱.
.Pear::Pager和AJAX的整合应用.
.PHP下防止单引号,双引号在接受页.
.PHP中常用的几个mysql语句.
.用PHP读写NTFS文件系统下的文件摘.
.怎样在php中使用PDF文档功能.
.PHP利用COM对象访问SQLServer、A.
.在字符串中把网址改成超级链接.
.做一个有下拉功能的留言版.
.使用PHP编写基于Web的文件管理系.
.PHP 5.0中多态性的实现方案浅析.
.直接读取数据库信息的三种方法.
.PHP与JavaScript之间中文base64码.
.如何使用PHP编程说明.

用PHP产生动态的影像图

发表日期:2001-3-9


很多人不了解  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">&nbsp;&nbsp;&nbsp;  
     绿色<input type="text" name="bggreen" size="8" value="51">&nbsp;&nbsp;&nbsp;  
      蓝色<input type="text" name="bgblue" size="7" value="153"></p>
<p>字元颜色</p>
<p>红色 <input type="text" name="chred" size="6" value="255">&nbsp;&nbsp;&nbsp;
      绿色 <input type="text" name="chgreen" size="8" value="255">&nbsp;&nbsp;&nbsp;  
     蓝色 <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> 
上一篇:在Windows版的PHP中使用ADO 人气:11842
下一篇:动态新闻发布的实现及其技巧 人气:12705
浏览全部的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐