>백엔드 개발 >PHP 튜토리얼 >第十四章 处理图像_PHP教程

第十四章 处理图像_PHP教程

WBOY
WBOY원래의
2016-07-13 10:31:59789검색

 

学习要点:
1.创建图像
2.简单小案例

 

      在PHP5 中,动态图象的处理要比以前容易得多。PHP5 在php.ini 文件中包含了GD 扩
展包,只需去掉GD 扩展包的相应注释就可以正常使用了。PHP5 包含的GD 库正是升级的
GD2 库,其中包含支持真彩图像处理的一些有用的JPG 功能。
      一般生成的图形,通过PHP 的文档格式存放,但可以通过HTML 的图片插入方式SRC
来直接获取动态图形。比如,验证码、水印、微缩图等。

 

一.创建图像

创建图像的一般流程:
1).设定标头,告诉浏览器你要生成的MIME 类型。
2).创建一个图像区域,以后的操作都将基于此图像区域。
3).在空白图像区域绘制填充背景。
4).在背景上绘制图形轮廓输入文本。
5).输出最终图形。
6).清除所有资源。
7).其他页面调用图像。

 

设定标头指定MIME 输出类型

<?<span php
</span><span header</span>('Content-Type: image/png'<span );
</span>?>

 

创建一个空白的图像区域

<?<span php
</span><span $im</span> = imagecreatetruecolor(200,200<span );
</span>?>

 

在空白图像区域绘制填充背景

<?<span php
</span><span $blue</span> = imagecolorallocate(<span $im</span>,0,102,255<span );
imagefill(</span><span $im</span>,0,0,<span $blue</span><span );
</span>?>

 

在背景上绘制图形轮廓输入文本

<?<span php
</span><span $white</span> = imagecolorallocate(<span $im</span>,255,255,255<span );
imageline(</span><span $im</span>,0,0,200,200,<span $white</span><span );
imageline(</span><span $im</span>,200,0,0,200,<span $white</span><span );
imagestring(</span><span $im</span>, 5, 80, 20, "Mr.Lee", <span $white</span><span );
</span>?>

 

输出最终图形

<?<span php
imagepng(</span><span $im</span><span );
</span>?>

 

清除所有资源

<?<span php
imagedestroy(</span><span $im</span><span );
</span>?>

 

其他页面调用创建的图形

<span <</span><span img </span><span src</span><span ="Demo4.php"</span><span  alt</span><span ="PHP 创建的图片"</span> <span /></span>

 

二.简单小案例

简单验证码

<?<span php
</span><span header</span>('Content-type: image/png'<span );
</span><span for</span>(<span $Tmpa</span>=0;<span $Tmpa</span><4;<span $Tmpa</span>++<span ){
    </span><span $nmsg</span>.=<span dechex</span>(<span rand</span>(0,15<span ));
}
</span><span $im</span> = imagecreatetruecolor(75,25<span );
</span><span $blue</span> = imagecolorallocate(<span $im</span>,0,102,255<span );
</span><span $white</span> = imagecolorallocate(<span $im</span>,255,255,255<span );
imagefill(</span><span $im</span>,0,0,<span $blue</span><span );
imagestring(</span><span $im</span>,5,20,4,<span $nmsg</span>,<span $white</span><span );
imagepng(</span><span $im</span><span );
imagedestroy(</span><span $im</span><span );
</span>?>

 

加载已有的图像

<?<span php
</span><span header</span>('Content-Type:image/png'<span );
</span><span define</span>('__DIR__',<span dirname</span>(<span __FILE__</span>).'\\'<span );
</span><span $im</span> = imagecreatefrompng(__DIR__.'222.png'<span );
</span><span $white</span> = imagecolorallocate(<span $im</span>,255,255,255<span );
imagestring(</span><span $im</span>,3,5,5,'http://www.yc60.com',<span $white</span><span );
imagepng(</span><span $im</span><span );
imagedestroy(</span><span $im</span><span );
</span>?>

 

加载已有的系统字体

<?<span php
</span><span $text</span> = <span iconv</span>("gbk","utf-8","李炎恢"<span );
</span><span $font</span> = 'C:\WINDOWS\Fonts\SIMHEI.TTF'<span ;
imagettftext(</span><span $im</span>,20,0,30,30,<span $white</span>,<span $font</span>,<span $text</span><span );
</span>?>

 

图像微缩

<?<span php
</span><span header</span>('Content-type: image/png'<span );
</span><span define</span>('__DIR__',<span dirname</span>(<span __FILE__</span>).'\\'<span );
</span><span list</span>(<span $width</span>, <span $height</span>) = <span getimagesize</span>(__DIR__.'222.png'<span );
</span><span $new_width</span> = <span $width</span> * 0.7<span ;
</span><span $new_height</span> = <span $height</span> * 0.7<span ;
</span><span $im2</span> = imagecreatetruecolor(<span $new_width</span>, <span $new_height</span><span );
</span><span $im</span> = imagecreatefrompng(__DIR__.'222.png'<span );
imagecopyresampled(</span><span $im2</span>, <span $im</span>, 0, 0, 0, 0,
<span $new_width</span>, <span $new_height</span>, <span $width</span>, <span $height</span><span );
imagepng(</span><span $im2</span><span );
imagedestroy(</span><span $im</span><span );
Imagedestroy(</span><span $im2</span><span );
</span>?>

 

  注:文章出自李炎恢PHP视频教程,本文仅限交流使用,不得用于商业用途,否则后果自负。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/759625.htmlTechArticle学习要点: 1.创建图像 2.简单小案例 在PHP5 中,动态图象的处理要比以前容易得多。PHP5 在php.ini 文件中包含了GD 扩 展包,只需去掉GD 扩展...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.