zoukankan      html  css  js  c++  java
  • PHP学习8——图像处理

    主要内容:

    1. 加载GD库
    2. 创建图像
    3. 绘制点,线,矩形,多边形,椭圆,弧线
    4. 绘制文字
    5. 通过GD库生成验证码

    其实吧,学习图像方法的最大作用,好像就是为了制作验证码。

    所以此专题,不如叫做制作验证码。

    1、加载GD库

    PHP5不仅可以处理文本数据,还可以处理多种格式的图像,安装了GD库,需要通过php.ini来加载GD库。

    可以通过phpinfo()方法来确认,如果有gd栏目就说明GD库加载成功了。

    phpinfo.php

    <?php
    	phpinfo();
    ?>
    

    2、创建图像

    GD库处理图像的操作都是先在内存处理,操作完成后再以数据流的方式输出到浏览器,有4个步骤:

    • 创建画布
    • 绘制图像
    • 输出图像
    • 释放资源

    创建画布

    创建画布就是在内存区中开辟一块存储区

    imagecreate.php

    <?php
    	//创建普通画布
    	$img1=imagecreate(100,100) or die("创建普通画布失败<br/>");
    	echo "创建普通画布成功<br/>";
    	
    	//创建真彩画布
    	$img2=imagecreatetruecolor(200,300) or die("创建真彩画布失败<br/>");
    	echo "创建真彩画布成功<br/>";
    	
    	//获取图像的大小
    	echo "图像1的x:".imagesx($img1)."<br/>";
    	echo "图像1的y:".imagesy($img1)."<br/>";
    	echo "图像2的x:".imagesx($img2)."<br/>";
    	echo "图像2的y:".imagesy($img2)."<br/>";
    ?>
    

      输出

    创建普通画布成功
    创建真彩画布成功
    图像1的x:100
    图像1的y:100
    图像2的x:200
    图像2的y:300
    

    其他的方法常用:

    • imagecreatefromgif()通过gif文件或者url新建一个图像
    • imagecreatefromjpeg()通过JPEG文件或者url新建一个图像
    • imagecreatefrompng()通过png或者url新建一个图像
    • imagecreatefrombmp()通过bmp或者url新建一个图像

    输出图像

    imagegif($image,$filename)输出一个gif格式图像到浏览器,如果第二个参数指定了,则以文件形式输出

    imagejpeg($image,$filename)

    imagepng($image,$filename)

    imageoutput.php

    <?php
    	//创建画布
    	$image1=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    	$image2=imagecreatefrompng("C:/xampp/htdocs/1.png");//使用本地资源
    
    	//在输出图像之前清理输出缓冲,否则缓冲中有其他字符串,图像会乱码
    	ob_clean();
    	
    	//指定输出类型为图像,否则乱码,chrome,Firefox,Opera等浏览器不兼容
    	header("Content-type:image/jpeg");
    	
    	//输出图像到浏览器
    	imagejpeg($image1) or die("创建图像失败<br/>");
    	imagejpeg($image2) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image1,$image2);
    ?>
    

      输出

    一些方法说明:

    ob_clean();用于清理输出缓冲,否则缓冲中有其他字符串会使得图片乱码,最后变为2进制数据

    header("Content-type:image/jpeg");指定输出类型为图像,是为了解决浏览器的兼容性问题,否则乱码,chrome,Firefox,Opera等浏览器不兼容,只有IE兼容。

    imagedestroy($image1);用于释放资源,让出内存。

    设置颜色

    前面已经学习了如何建立一张画布,但是没有“彩笔”,绘画就还没有开始,下面来创建“彩笔”,设置颜色。

    imagecolorallocate($image,$red,$green,$blue),会返回一个标识,代表了给定的RGB成分组成的颜色。

    imagecolorallocate.php

    <?php
    	//创建画布
    	$image=imagecreate(200,200);
    
    	//第一次设置的通常为背景色
    	$red=imagecolorallocate($image,255,0,0);
    	//设置画笔为蓝色
    	$blue=imagecolorallocate($image,0,0,255);
    	
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      输出

    区域填充

    imagefill($image,$x,$y,$color)它会将(x,y)点出颜色相同并且相邻颜色替换为$color设置的颜色。

    这里使用rand(0,255)产生一个大于等于0,小于255的随机数来设置颜色。

    imagefill.php

    <?php
    	//创建画布
    	$image=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    
    	//第一次设置的通常为背景色
    	$rand=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));//使用随机数设置颜色
    	//设置画笔为蓝色
    	//$blue=imagecolorallocate($image,0,0,255);
    	
    	//区域填充
    	imagefill($image,10,10,$rand);
    	
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      输出

    绘制点,线,矩形,多边形,椭圆,弧线

    点和线

    imagesetpixel($image,$x,$y,$color)在(x,y)位置使用$color颜色绘制点

    imageline($image,$x1,$y1,$x2,$y2,$color)在(x1,y1)到(x2,y2)位置使用$color颜色绘制线

    利用下面的这段代码可以在生成验证码的时候生成干扰元素。

    imagesetpixel_line.php

    <?php
    	//创建画布
    	//$image=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    	$image=imagecreate(200,200);
    	
    	//第一次设置的通常为背景色
    	$black=imagecolorallocate($image,0,0,0);
    	//设置画笔为蓝色
    	$white=imagecolorallocate($image,255,255,255);
    	
    	//绘制点
    	for($i=0;$i<1000;$i++){
    		imagesetpixel($image,rand(0,200),rand(0,200),$white);
    	}
    	//绘制线
    	for($i=0;$i<10;$i++){
    		imageline($image,rand(0,200),rand(0,200),rand(0,200),rand(0,200),$white);
    	}
    	
    	
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      输出

    矩形

    imagerectangle($image,$x1,$y1,$x2,$y2,$color)绘制矩形

    imagefilledrectangle($image,$x1,$y1,$x2,$y2,$color)填充矩形

    rectangle.php

    <?php
    	//创建画布
    	//$image=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    	$image=imagecreate(300,300);
    	
    	//第一次设置的通常为背景色
    	$black=imagecolorallocate($image,0,0,0);
    	//设置画笔为蓝色
    	$white=imagecolorallocate($image,255,255,255);
    	
    	//绘制矩形
    	imagerectangle($image,10,10,100,100,$white);
    	//填充矩形
    	imagefilledrectangle($image,50,50,100,100,$white);
    	
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      输出

    多边形

    imagepolygon($image,$arr_points,$num_points,$color)绘制多边形,$arr_points多边形顶点数组数必须大于等于$num_points定点总数

    imagefilledpolygon($image,$arr_points,$num_points,$color)填充多边形

    imagepolygon.php

    <?php
    	//创建画布
    	//$image=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    	$image=imagecreate(300,300);
    	
    	//第一次设置的通常为背景色
    	$black=imagecolorallocate($image,0,0,0);
    	//设置画笔为蓝色
    	$white=imagecolorallocate($image,255,255,255);
    	
    	$arr_points=array(10,10,50,20,40,30,100,120,200,30,30,150);
    	//绘制多边形
    	imagepolygon($image,$arr_points,6,$white);
    	//填充多边形
    	imagefilledpolygon($image,$arr_points,5,$white);
    	
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      输出

    绘制椭圆

    imageellipse($image,$cx,$cy,$width,$height,$color)绘制椭圆

    imagefilledellipse($image,$cx,$cy,$width,$height,$color)绘制椭圆

    imageellipse.php

    <?php
    	//创建画布
    	//$image=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    	$image=imagecreate(300,300);
    	
    	//第一次设置的通常为背景色
    	$black=imagecolorallocate($image,0,0,0);
    	//设置画笔为蓝色
    	$white=imagecolorallocate($image,255,255,255);
    	
    	//绘制椭圆
    	imageellipse($image,100,100,200,100,$white);
    	//填充椭圆
    	imagefilledellipse($image,100,100,200,100,$white);
    	
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      输出

    绘制弧线

    imagearc($image,$cx,$cy,$width,$height,$start,$end,$color)绘制弧线,$start是开始角度,$end是结束角度

    imagefilledarc($image,$cx,$cy,$width,$height,$start,$end,$color,$mode)填充弧线,$start是开始角度,$end是结束角度

    imagearc.php

    <?php
    	//创建画布
    	//$image=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    	$image=imagecreate(300,300);
    	
    	//第一次设置的通常为背景色
    	$black=imagecolorallocate($image,0,0,0);
    	//设置画笔为蓝色
    	$white=imagecolorallocate($image,255,255,255);
    	
    	//绘制椭圆
    	imagearc($image,100,100,200,200,0,90,$white);
    	//填充椭圆
    	imagefilledarc($image,100,100,200,200,0,45,$white,IMG_ARC_PIE);
    	
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      输出

    绘制文字

    imagestring($image,$font,$x,$y,$data,$color)水平绘制一行字符串,使用$font字体(内置字体为1,2,3,4,5)写$data内容,(x,y)位置为内容左上角。

    imagestringup($image,$font,$x,$y,$data,$color)垂直绘制一行字符串

    imagechar($image,$font,$x,$y,$data,$color)水平绘制一个字符

    imagecharup($image,$font,$x,$y,$data,$color)垂直绘制一个字符

    imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text)使用truetype字体向图像写入文本

    使用$font字体(内置字体为1,2,3,4,5),如果使用其他字体用imageloadfont()

    imagestring.php

    <?php
    	//创建画布
    	//$image=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    	$image=imagecreate(300,300);
    	
    	//第一次设置的通常为背景色
    	$black=imagecolorallocate($image,0,0,0);
    	//设置画笔为蓝色
    	$white=imagecolorallocate($image,255,255,255);
    	
    	$data="imagestring";
    	//绘制文字
    	imagestring($image,1,100,20,$data,$white);
    	imagestringup($image,3,30,150,$data,$white);
    	imagestring($image,5,100,100,$data,$white);
    
    	//绘制字符
    	imagechar($image,5,200,20,$data,$white);
    
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      输出

    使用字库,字库文件默认在:C:WindowsFonts

    imagettftext.php

    <?php
    	//创建画布
    	//$image=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    	$image=imagecreate(500,500);
    	
    	//第一次设置的通常为背景色
    	$black=imagecolorallocate($image,0,0,0);
    	//设置画笔为白色
    	$white=imagecolorallocate($image,255,255,255);
    	
    	$data="hello PHP";
    	//绘制truetype文字
    	imagettftext($image,55,0,50,250,$white,"C:/Windows/Fonts/Arial.ttf",$data);
    	imagettftext($image,55,90,250,400,$white,"C:/Windows/Fonts/Arial.ttf",$data);
    	imagettftext($image,55,45,100,370,$white,"C:/Windows/Fonts/Arial.ttf",$data);
    	imagettftext($image,55,135,370,330,$white,"C:/Windows/Fonts/Arial.ttf",$data);
    
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      输出

    通过GD库生成验证码

    在网上注册一些账号,常常遇到注册码输入,生成验证码步骤:

    1. 创建画布
    2. 随机绘制字符
    3. 绘制干扰元素
    4. 输出图像到浏览器
    5. 释放资源

    check_data.php

    <?php
    	//创建画布
    	//$image=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    	$image=imagecreate(200,100);
    	
    	//第一次设置的通常为背景色
    	$black=imagecolorallocate($image,0,0,0);
    	//设置画笔为白色
    	$white=imagecolorallocate($image,255,255,255);
    	
    	//产生随机干扰线条
    	for($i=0;$i<9;$i++){
    		imageline($image,rand(0,200),rand(0,100),rand(0,200),rand(0,100),$white);
    	}
    	//产生随机干扰点
    	for($i=0;$i<200;$i++){
    		imagesetpixel($image,rand(0,200),rand(0,100),$white);
    	}
    	//产生4个字符的字符串
    	for($i=0,$data='';$i<4;$i++){
    		switch(rand(1,3)){
    			case '1':$ch=rand(0,9);break;
    			case '2':$ch=sprintf('%c',rand(97,122));break;
    			case '3':$ch=sprintf('%c',rand(65,90));break;
    		}
    		$data.=$ch;
    	}
    	
    	//绘制truetype文字
    	imagettftext($image,32,rand(1,15),20,70,$white,"C:/Windows/Fonts/Arial.ttf",$data);
    
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      可能输出

    最后来一个好玩的图像

    他的实现代码如下

    <?php
    	//创建画布
    	//$image=imagecreatefromjpeg("http://pic.58pic.com/58pic/13/19/83/30J58PICFBZ_1024.jpg");//使用网络资源
    	$image=imagecreate(500,500);
    	
    	//第一次设置的通常为背景色
    	$white=imagecolorallocate($image,255,255,255);
    
    	//设置画笔
    	$black=imagecolorallocate($image,0,0,0);
    	
    	$data="Hello PHP!";
    	//绘制truetype文字
    	for($i=0;$i<360;$i+=5){
    		imagettftext($image,35,$i,250,250,$black,"C:/Windows/Fonts/Arial.ttf",$data);
    	}
    
    	//输出图像前清理输出缓冲
    	ob_clean();
    	//解决chrome兼容性问题
    	header("Content-type:image/jpeg");
    	//输出图像到浏览器
    	imagejpeg($image) or die("创建图像失败<br/>");
    	
    	//释放资源
    	imagedestroy($image);
    ?>
    

      

     ok,就到这里吧。

  • 相关阅读:
    《程序员修炼之道——从小工到专家》读后感二
    2019.10.14课堂总结
    《程序员修炼之道——从小工到专家》读后感一
    2019.09.23课堂总结
    回文序列判断
    动手动脑
    2018/10/21动手动脑及类的创建
    动手动脑-java重载
    第二次上机实验体会
    Java第一次上机实验源代码
  • 原文地址:https://www.cnblogs.com/1906859953Lucas/p/9439758.html
Copyright © 2011-2022 走看看