仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1242|回复: 19
打印 上一主题 下一主题

[学习教程] PHP网站制作之用PHP发生静态的影象图[转自奥索]

[复制链接]
柔情似水 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:17:06 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
熟悉HTML/CSS/JS等网页基本元素,完成阶段可自行制作完整的网页,对元素属性达到熟悉程度静态   良多人不懂得  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>   怎样学习,大家都知道编程是1门很枯燥的事业,所以大家一定要有兴趣,可能刚开始打算学的时候是因为别人说php有多好,php多么流行,但是后来伴随着学习的深入,你的这些
柔情似水 该用户已被删除
沙发
 楼主| 发表于 2015-4-12 04:28:40 | 显示全部楼层
因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-21 14:21

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表