• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

学会用PHP产生动态的影像图

php 搞代码 4年前 (2022-01-23) 15次浏览 已收录 0个评论

很多人不了解 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;
本文来源gaodai#ma#com搞*!代#%^码$网!搞代gaodaima码$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 可以由此语法在网页中叫出来:

#######################################################
—-test.php—–

<meta HTTP-EQUIV=”content-type” CONTENT=”text/html; charset=gb2312″>
New Page 1

<body>

#######################################################

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——————–

New Page 1

<body>

文字

大小

断句的位置(wrap break position)

背景颜色

红色
绿色
蓝色

字元颜色

红色
绿色
蓝色

字型

阴影 是

Crop size

Jpeg 品质 (0-100)

#######################################################

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

#######################################################
—-test.php—–

New Page 1

<body>

以上就介绍了学会用PHP产生动态的影像图,包括了动态影像图方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:学会用PHP产生动态的影像图

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址