用PHP产生动态的影像图
PHP 动态 产生 影像
2023-06-13 09:13:44 时间
很多人不了解 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);
?>
#######################################################
很重要一点是你不能在这档案中放任何HTMLtags.也不能有空白行在<?和?>tag之前或之後.如果你用这段Script後看到一个不完整的影像,表示你可能在PHP标签以外误打了字元.
以上的script可以由此语法在网页中叫出来:<IMGSRC="button.php3?s=36&text=PHP+is+Cool">
#######################################################
----test.php-----
<html>
<head>
<METAHTTP-EQUIV="content-type"CONTENT="text/html;charset=gb2312">
<title>NewPage1</title>
</head>
<body>
<IMGSRC="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.可参考 PHPFAQ.我建议可以copylibgd.a到 /usr/local/lib和gd*.h相关档案到 /usr/local/include然後
"makeinstall"forFreeTTFlibrary.
可以在这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>NewPage1</title>
</head>
<body>
<formmethod="POST"action="button.php">
<p>文字<inputtype="text"name="text"size="60"></p>
<p>大小<inputtype="text"name="s"size="6"value="14"></p>
<p>断句的位置(wrapbreakposition)<inputtype="text"name="wrappos"size="3"value="20"></p>
<p>背景颜色</p>
<p>红色<inputtype="text"name="bgred"size="6"value="0">
绿色<inputtype="text"name="bggreen"size="8"value="51">
蓝色<inputtype="text"name="bgblue"size="7"value="153"></p>
<p>字元颜色</p>
<p>红色<inputtype="text"name="chred"size="6"value="255">
绿色<inputtype="text"name="chgreen"size="8"value="255">
蓝色<inputtype="text"name="chblue"size="7"value="255"></p>
<p>字型<inputtype="text"name="font"size="20"value="arialbd"></p>
<p>阴影<inputtype="radio"value="yes"checkedname="shadow">是
<inputtype="radio"name="shadow"value="no">否</p>
<p>Cropsize<inputtype="text"name="crop"size="20"value="2.2"></p>
<p>Jpeg品质(0-100)<inputtype="text"name="jpegquality"size="20"value="80"></p>
<p><inputtype="submit"value="Submit"name="B1">
<inputtype="reset"value="Reset"name="B2"></p>
</form>
</body>
</html>
#######################################################
或是直接像上例一样呼叫:
#######################################################
----test.php-----
<html>
<head>
<title>NewPage1</title>
</head>
<body>
<IMGSRC="button.php?s=36&text=PHP+is+Cool">
</body>
</html>
我用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);
?>
#######################################################
很重要一点是你不能在这档案中放任何HTMLtags.也不能有空白行在<?和?>tag之前或之後.如果你用这段Script後看到一个不完整的影像,表示你可能在PHP标签以外误打了字元.
以上的script可以由此语法在网页中叫出来:<IMGSRC="button.php3?s=36&text=PHP+is+Cool">
#######################################################
----test.php-----
<html>
<head>
<METAHTTP-EQUIV="content-type"CONTENT="text/html;charset=gb2312">
<title>NewPage1</title>
</head>
<body>
<IMGSRC="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.可参考 PHPFAQ.我建议可以copylibgd.a到 /usr/local/lib和gd*.h相关档案到 /usr/local/include然後
"makeinstall"forFreeTTFlibrary.
可以在这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>NewPage1</title>
</head>
<body>
<formmethod="POST"action="button.php">
<p>文字<inputtype="text"name="text"size="60"></p>
<p>大小<inputtype="text"name="s"size="6"value="14"></p>
<p>断句的位置(wrapbreakposition)<inputtype="text"name="wrappos"size="3"value="20"></p>
<p>背景颜色</p>
<p>红色<inputtype="text"name="bgred"size="6"value="0">
绿色<inputtype="text"name="bggreen"size="8"value="51">
蓝色<inputtype="text"name="bgblue"size="7"value="153"></p>
<p>字元颜色</p>
<p>红色<inputtype="text"name="chred"size="6"value="255">
绿色<inputtype="text"name="chgreen"size="8"value="255">
蓝色<inputtype="text"name="chblue"size="7"value="255"></p>
<p>字型<inputtype="text"name="font"size="20"value="arialbd"></p>
<p>阴影<inputtype="radio"value="yes"checkedname="shadow">是
<inputtype="radio"name="shadow"value="no">否</p>
<p>Cropsize<inputtype="text"name="crop"size="20"value="2.2"></p>
<p>Jpeg品质(0-100)<inputtype="text"name="jpegquality"size="20"value="80"></p>
<p><inputtype="submit"value="Submit"name="B1">
<inputtype="reset"value="Reset"name="B2"></p>
</form>
</body>
</html>
#######################################################
或是直接像上例一样呼叫:
#######################################################
----test.php-----
<html>
<head>
<title>NewPage1</title>
</head>
<body>
<IMGSRC="button.php?s=36&text=PHP+is+Cool">
</body>
</html>
相关文章
- 基础面试题php
- 【说站】php判断数组元素不为空格的方法
- PHP代码审计入门
- php多进程结合Linux利器split命令实现把大文件分批高效处理详解编程语言
- php抛出异常详解编程语言
- PHP扩展Redis:建立强大的缓存架构(php扩展redis)
- 技巧Linux新手必知的PHP换行技巧(linux换行php)
- Linux更新PHP,提升程序品质(linux更新php)
- 解锁PHP的高效Oracle配置(php配置oracle)
- 文件MySQL更新实现快速PHP文件升级(mysql更新php)
- PHP与MySQL互动:开发动态网站的关键(php与mysql交互)
- PHP与MySQL:如何实现连接?(php怎么连mysql)
- PHP在Linux环境中的路径解析(phplinux路径)
- 如何在Linux下快速搭建PHP环境(linux下php环境搭建)
- PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明
- PHP与MSSQl的兼容性探索(php mssql 兼容)
- 在Linux环境下,使用PHP发送邮件(linux发送邮件php)
- Redis面试题与PHP配合(redis面试题 php)
- 利用PHP实现Redis队列任务的并发处理(redis队列任务php)
- IIS+PHP+MySQL+ZendOptimizer+GD库+phpMyAdmin安装配置
- PHP学习mysql
- discuzauthcode经典php加密解密函数解析
- php下目前为目最全的CURL中文说明
- php更新mysql后获取影响的行数发生异常解决方法
- php变量作用域的深入解析
- php动态生成函数示例
- php开启openssl的方法
- PHP中使用数组指针函数操作数组示例
- PHP中的mongodbgroup操作实例