您的位置 首页 php

PHP扩展函数 图片处理函数GD2

有些函数系统也帮忙打包好,以xxx.dll文件存放在ext、extras文件夹中,需要开启配置才能使用。这些函数称为扩展函数(extend)。

一、 解决中文截取乱码mb_substr

echo mb_substr(“中abc”,0,2,”utf-8″);

二、 图片处理函数GD2

GD(graphic device,图形设备),负责在屏幕和打印机上输出信息。GD2是GDI的后续版本。要使用GD2,首先应该把网页打散作为图像(header),接着创建Graphics类对象(简单的来说,Graphics类对象就相当于画布,没画布我们在什么地方绘图呢?),然后调用一系列绘图方法即可,最后再生成图片、释放资源(destroy)。看下面的代码

步骤:

1、声明:把当前页面通过写代码的方式作为图片生成,语法:

header(“content-type:image/ jpeg ”);

2、绘图

3、生成图片(注意格式对应)

语法: imagejpeg();

4、释放资源(销毁材料)

语法:imagedestory();

其中绘图重要函数

//画点

Imagesetpixel

//画线

Imageline

//画矩形

Imagerectangle

//画圆

Imageellipse

//画实心矩形

Imagefilledrectangle

//画实心圆

imagefilledellipse

//画文字 不支持中文

Imagestring

//画竖排文字

Imagestringup

// 画文字支持中文

Image TTF text

所有的绘画工作完成,可以使用img标签调用!!!

<img src=”xxxx.php”>

示例源码:

<?php

header(“content-type:image/jpeg”);

//执行绘图

//创建画布

$im=imagecreate(200,100);

//填充背景色

imagecolorallocate($im,100,100,100);

//定义颜色

$red=imagecolorallocate($im,255,255,255);

//画点

imagesetpixel($im,5,5,$red);

//画线

imageline($im,0,50,200,50,$red);

//画矩形

imagerectangle($im,100,10,130,40,$red);

//画圆

imageellipse($im,50,50,10,30,$red);

//画实心的矩形

imagefilledrectangle($im,70,10,100,40,$red);

//画实心圆

imagefilledellipse($im,40,20,10,30,$red);

//定义字体

$ font =”STXINGKA.TTF”;

//画文字

imagestring($im,$font,100,30,’I LIKE TEACHER HE!’,$red);

//画竖排文字

imagestringup($im,$font,180,90,’I LOVE TEACHER HE!’,$red);

//画支持中文的文字

imagettftext($im,14,30,80,70,$red,$font,’我爱何老师!’);

//生成图片

imagejpeg($im);

//想一想为什么在生成图片之后无法绘画

imagesetpixel($im,15,15,$red);

//释放资源

imagedestroy($im);

?>

TIPS:练习下多边形的多点坐标,可以使用图片热点技术。

利用GD2和前面的知识画标准验证码:

<?php

header(“content-type:image/jpeg”);

include_once(“CheckNum.php”);

$chnCount=6;//验证码位数

$chn=CreateCheckNumber($chnCount,7);

$fontSize=40;//字体的大小(宽度)

$width=ceil($chnCount*($fontSize+0.5));//验证码宽度

$height=$fontSize*2-2;//验证码高度

$im=imagecreate($width,$height);

imagecolorallocate($im,243,243,243);

$fontColor=imagecolorallocate($im,70,147,251);

$font=’STXINGKA.TTF’;

imagettftext($im,$fontSize,0,($width-$fontSize*$chnCount+$fontSize)/2,($height-$fontSize)/2+$fontSize,$fontColor,$font,$chn);

//画边框 注意边框粗细

imagerectangle($im,0,0,$width-1,$height-1,$fontColor);

//画干扰线

$lineColor=imagecolorallocate($im,249,62,89);

for($i=0;$i<2;$i++)

imageline($im,0,rand(1,$height-1),$width,rand(1,$height-1),$lineColor);

//画干扰点

for($i=0;$i<100;$i++){

$pixelColor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($im,rand(1,$width-1),rand(1,$height-1),$pixelColor);

}

imagejpeg($im);

imagedestroy($im);

?>

文字水印:

以现有的图片作为画布再画上文字。

Imagecreatefromjpeg

<?php

header(“content-type:image/gif”);

//以现有的图片作为背景来创建画布

$im=imagecreatefromgif(“menggou.gif”);

$color=imagecolorallocate($im,255,255,255);

$font=’STKAITI.TTF’;

imagestring($im,$font,5,5,’

imagegif($im);

imagedestroy($im);

?>

图片水印:

把. png 图片作为水印使用imagecopy函数即可。

获取图片的宽度和高度

Imagesx、imagesy

<?php

header(“content-type:image/jpeg”);

//创建背景,作业:根据背景图创建对应格式的画布

$bgurl=”bg.jpg”;//改变背景图片格式

//$extName=mb_substr($bgurl,strrpos($bgurl,’.’), strlen ($bgurl)-strrpos($bgurl,’.’));//背景图片扩展名

$extName=strrchr($bgurl,’.’);

switch(strtolower($extName)){

case ‘.jpg’:$bg=imagecreatefromjpeg($bgurl);break;

}

$water=imagecreatefrompng(‘logo.png’);

//获取水印图片的宽高度

$w=imagesx($water);

$h=imagesy($water);

//作业:要求水印图片出现在右上角、做下角、右下角、居中

imagecopy($bg,$water,0,0,0,0,$w,$h);

//作业完善:背景图片水印图片的2倍及以上大才生成水印,否则不生成是原图

imagejpeg($bg);

imagedestroy($bg);

?>

作业:

1、在网站根目录下放4个图片,分别命名为1.gif.png.bmp.jPg、 2.gif、3.png、4.bmp 生成图片水印

2、图片水印图片可以在不同的位置出现,定义变量控制为左上角、右上角、左下角、右下角、居中

3、背景图片水印图片的2倍及以上大才生成水印,否则不生成是原图。

统计图

柱状图实现思路:

用户传递的参数为数组,数组中元素个数为多少个单位;纵方向一般划分10个等距离单位意味数组100%,数组中的值在纵方向按百分比显示;缩放柱状图控制单位距离。

实现效果如下:

完整代码:

<?php

header(“content-type:image/jpeg”);

/* 建议按300*550来缩放,否则会变形 */

$pSize=50;//单位距离,控制缩放

$pNameArr= explode (‘,’,$_REQUEST[‘dw’]);//单位名称

$arr=explode(‘,’,$_REQUEST[‘sj’]);//需要生成统计图的数组数据

$im=imagecreate($pSize*(count($arr)+2),$pSize*(10+1));

imagecolorallocate($im,243,243,243);

$black=imagecolorallocate($im,0,0,0);

//画X轴

imageline($im,0,imagesy($im)-1,imagesx($im),imagesy($im)-1,$black);

//画Y轴

imageline($im,1,0,1,imagesy($im),$black);

//算出数组元素中所有元素的值

$ sum =0;

foreach($arr as $v){

$sum+=$v;

}

//画X轴上等距线

$lheight=10;//等距线高度

//字体

$font=”../fonts/STKAITI.TTF”;

for($i=0;$i<count($arr);$i++){

imageline($im,$pSize*($i+1),imagesy($im)-1,$pSize*($i+1),imagesy($im)-1-$lheight,$black);

//画矩形

imagerectangle($im,$pSize*($i+1)-$pSize/2+3,imagesy($im)-$arr[$i]/$sum*$pSize*10-1-1,$pSize*($i+1)+$pSize/2-3,imagesy($im)-1,$black);

$rcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagefilledrectangle($im,$pSize*($i+1)-$pSize/2+4,imagesy($im)-$arr[$i]/$sum*$pSize*10-1,$pSize*($i+1)+$pSize/2-4,imagesy($im)-2,$rcolor);

imagettftext($im,16,30,$pSize*($i+1)-$pSize/2+4,imagesy($im)-$arr[$i]/$sum*$pSize*10-4,$rcolor,$font,$arr[$i].$pNameArr[$i]);

}

//画Y轴上等距线

for($i=1;$i<=10;$i++){

imageline($im,1,imagesy($im)-$pSize*$i,1+$lheight,imagesy($im)-$pSize*$i,$black);

imagestring($im,2,10,imagesy($im)-$pSize*$i-5,$i*10,$black);

}

imagestringup($im,5,$pSize/2-5,$pSize*5,'(%)’,$black);

imagejpeg($im);

imagedestroy($im);

?>

饼状图实现效果如下:

<?php

header(“content-type:image/jpeg”);

/* 建议按350*200来缩放,否则会变形 */

//定义圆半径

$raduis=100;

$arr=explode(‘,’,$_REQUEST[‘sj’]);//需要生成统计图的数组数

$pNameArr=explode(‘,’,$_REQUEST[‘dw’]);//单位名称

//计算数组总值

$sum=0;

//$maxFontSize=strlen($arr[0]);//最大文字内容长度

foreach($arr as $v){

$sum+=$v;

//$maxFontSize=strlen($v)>$maxFontSize?strlen($v):$maxFontSize;

}

//foreach($pNameArr as $v)

//$maxFontSize=strlen($v)>$maxFontSize?strlen($v):$maxFontSize;

//字体

$font=”../fonts/STKAITI.TTF”;

$fontSize=13;//字体大小

$im=imagecreate($raduis*2+180,$raduis*2);

imagecolorallocate($im,243,243,243);

$black=imagecolorallocate($im,0,0,0);

imageellipse($im,$raduis,$raduis,$raduis*2-1,$raduis*2-1,$black);

$startP=0;//起始角度

for($i=0;$i<count($arr);$i++){

$pieColor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagefilledarc($im,$raduis,$raduis,$raduis*2-3,$raduis*2-3,$startP,$startP+$arr[$i]/$sum*360,$pieColor,IMG_ARC_EDGED);

$startP+=$arr[$i]/$sum*360;

//数据提示

imagettftext($im,$fontSize,0,$raduis*2+$fontSize*3,$fontSize*($i+1)+6,$pieColor,$font,$arr[$i].$pNameArr[$i].'(‘.round($arr[$i]/$sum*100).’%)’);

imagefilledrectangle($im,$raduis*2+$fontSize,10+$fontSize*$i,$raduis*2+$fontSize*2,6+$fontSize*$i+$fontSize,$pieColor);

}

imagejpeg($im);

imagedestroy($im);

?>

补充:好吧,验证码比较难看,应大家要求,换个好看点的

复制代码

1.<?php

2.session_start();

3.header(“content-type:image/png”); //设置创建图像的格式

4.$image_width=70; //设置图像宽度

5.$image_height=18; //设置图像高度

6.srand(microtime()*100000); //设置随机数的种子

7.for($i=0;$i<4;$i++){ //循环输出一个4位的随机数

8. $new_number.=rand(0,9);

9.}

10.$_SESSION[check_checks]=$new_number; //将获取的随机数验证码写入到SESSION变量中

11.

12.$num_image=imagecreate($image_width,$image_height); //创建一个画布

13.imagecolorallocate($num_image,255,255,255); //设置画布的颜色

14.for($i=0;$i<strlen($_SESSION[check_checks]);$i++){ //循环读取SESSION变量中的验证码

15. $font=mt_rand(3,5); //设置随机的字体

16. $x=mt_rand(1,8)+$image_width*$i/4; //设置随机字符所在位置的X坐标

17. $y=mt_rand(1,$image_height/4); //设置随机字符所在位置的Y坐标

18. $color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); //设置字符的颜色

19. imagestring($num_image,$font,$x,$y,$_SESSION[check_checks][$i],$color); //水平输出字符

20.}

21.imagepng($num_image); //生成PNG格式的图像

22.imagedestroy($num_image); //释放图像资源

23.?>

三、header函数、 iconv 、get_loaded_extensions函数

header函数

1、声明图片

Header(“content-type:image/xxxx”);

2、当前页面强制转码

header(“content-type:text/html;charset=utf-8”);

3、强制跳转

header(“location:”);

……

详细请参阅 学玩网 header函数的用法

注意事项:header函数前不能有空格、回车(\n)、换行(\r)等非法内容;也不能包含任何输出的语句;不能放任何HTML标签,但是 可以放到 <html> 之前

iconv字符串强制转码:

iconv(“原来的编码”,”转出后的编码”,$str);

Get_loaded_extensions() 判断是否开启对应函数库,返回支持库字符串数组

文章来源:智云一二三科技

文章标题:PHP扩展函数 图片处理函数GD2

文章地址:https://www.zhihuclub.com/149306.shtml

关于作者: 智云科技

热门文章

网站地图