zl程序教程

您现在的位置是:首页 >  后端

当前栏目

PHP图像函数

PHP 函数 图像
2023-09-11 14:19:07 时间

(1)常见的验证码哪些?
   图像类型、语音类型、视频类型、短信类型等

 

(2)使用验证码的好处在哪里?

①防止恶意的破解密码
如一些黑客为了获取到用户信息,通过不同的手段向服务器发送数据,验证猜测用户信息的准确性。
②防止恶意的刷票,论坛灌水
这种在论坛中长期存在灌水的现象,比如贴吧,不断发送帖子。现在百度贴吧已经针对用户发帖的时间做了一个时间的限制,当一定时间内发帖过多是无法继续发帖的。
③防止恶意的请求
如用户提交一个表单信息,通过不断向后台请求数据信息造成服务器资源的浪费,以及恶意的攻击。
④趣味性
这一点属于个人观点,当我们提交表单的时候,如遇到一些有趣的验证码方式,能够增强用户对网站的喜爱程度。
⑤获取用户信息
这一点在现目前的站点中是屡见不鲜的事情了。如我们注册一个站点的账号,需要通过手机验证码才可以注册成功。网站在拿到用户的电话号码之后会给手机号主发送一些营销信息。

 

(3)环境配置

只需要本地安装了PHP的GD扩展库即可使用。如何查看呢,创建一个PHP文件在该文件中写入<?php phpinfo();?>,访问该文件,如果查看到了GD库已经安装了,则可以使用。

没有的,自行安装,百度很多教程。

GD 库

使用 PHP 图像处理函数,需要加载 GD 支持库。请确定 php.ini 加载了 GD 库:extension = php_gd2.dll

使用 gd_info() 函数可以查看当前安装的 GD 库的信息:

<?php
var_dump(gd_info());
?>

输出信息:

Array
(
    [GD Version] => bundled (2.1.0 compatible)
    [FreeType Support] => 1
    [FreeType Linkage] => with freetype
    [T1Lib Support] => 
    [GIF Read Support] => 1
    [GIF Create Support] => 1
    [JPEG Support] => 1
    [PNG Support] => 1
    [WBMP Support] => 1
    [XPM Support] => 1
    [XBM Support] => 1
    [WebP Support] => 1
    [JIS-mapped Japanese Font Support] => 
)

 

【四】图像处理函数

(1)获取图像信息

①getimagesize():获取图像尺寸,类型等信息

    获取图像大小及相关信息,成功返回一个数组,失败则返回 FALSE 并产生一条 E_WARNING 级的错误信息

<?php
$array = getimagesize("images/flower_1.jpg");
print_r($array);
?>

 浏览器显示:

Array
(
    [0] => 350   //图像宽度的像素值
    [1] => 318   //图像高度的像素值
    [2] => 2     //图像的类型,返回的是数字
//1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),
//9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
[3] => width="350" height="318" //宽度和高度的字符串,可以直接用于 HTML 的 <image> 标签 [bits] => 8 //图像的每种颜色的位数,二进制格式 [channels] => 3 //图像的通道值,RGB 图像默认是 3 [mime] => image/jpeg //图像的 MIME 信息,此信息用来在 HTTP Content-type 头信息中发送正确的信息,如:header("Content-type: image/jpeg"); )

②imagesx():获取图像宽度

     获取图像的宽度,单位为像素,返回值为整型

③imagesy():获取图像高度

    用于获取图像的高度,语法及用法同 imagesx() 

imagesx()和imagesy()参数为如 imagecreatetruecolor()、imagecreatefromjpeg() 等函数返回的图像资源

<?php
    $img = imagecreatefromjpeg('./upload/01.jpg');
    // echo "$img";输出Resource id #3
    echo "图像宽度:",imagesx( $img ),"<br />";
    echo "图像高度:",imagesy( $img );
?>

 

(2)创建图像

①imagecreate():创建一幅空白图像

如果我们要对图像进行处理,就如其它图像处理软件一样,需要创建一块画布。imagecreate() 和 imagecreatetruecolor() 函数用于创建一幅空白图像。

参数 x ,y 分别为要创建图像的宽度和高度像素值,返回一个图像资源

<?php
    header("Content-type: image/png");
    //创建图像
    $im = imagecreate(200, 50) or die("创建图像资源失败");
    //图片背景颜色
    $bg = imagecolorallocate($im, 255, 255, 255);
    //文字颜色
    $text_color = imagecolorallocate($im, 0, 0, 255);
    //水平画一行字,要输出中文等需要 TTF 字体支持的请使用 magettftext() 函数
    imagestring($im, 5, 0, 0, "Hello world!", $text_color);
    //以PNG格式输出图像
    imagepng($im);
    //销毁图像资源
    imagedestroy($im);
?>

该例子以图像格式输出一行文字:Hello world! 。例子中用到的其他函数,将在后面逐一介绍。

②imagecreatetruecolor():创建一幅真彩色空白图像

功能与 imagecreate() 类似,创建一幅真彩色的图像,从而支持更为丰富的色彩。

语法:resource imagecreatetruecolor( int x, int y )

注意:本函数不能用于 GIF 文件格式。

 

(3)销毁图像

imagedestroy()

图像处理完成后,使用 imagedestroy() 指令销毁图像资源以释放内存,虽然该函数不是必须的,但使用它是一个好习惯。

语法:bool imagedestroy( resource image ),具体使用可见上面创建图像例子。

 

(4)载入图像

imagecreatefrom 系列函数用于从文件或 URL 载入一幅图像,成功返回图像资源,失败则返回一个空字符串

①imagecreatefromgif():创建一块画布,并从 GIF 文件或 URL 地址载入一副图像

②imagecreatefromjpeg():创建一块画布,并从 JPEG 文件或 URL 地址载入一副图像

③imagecreatefrompng():创建一块画布,并从 PNG 文件或 URL 地址载入一副图像

④imagecreatefromwbmp():创建一块画布,并从 WBMP 文件或 URL 地址载入一副图像

⑤imagecreatefromstring():创建一块画布,并从字符串中的图像流新建一副图像

<?php
header("Content-type: image/jpeg");
//创建并载入一幅图像
$im = imagecreatefromjpeg("./on1e.jpg");
//错误处理
if(!$im){
    $im  = imagecreatetruecolor(150, 30);
    $bg = imagecolorallocate($im, 255, 255, 255);
    $text_color  = imagecolorallocate($im, 0, 0, 255);
    //填充背景色
    imagefilledrectangle($im, 0, 0, 150, 30, $bg);
    //以图像方式输出错误信息
    imagestring($im, 3, 5, 5, "Error loading image", $text_color);
} else {
    //输出该图像
    imagejpeg($im);
}
?>

 在该例子中,我们载入并输出原图。由于 PHP 对图像创建错误没有友好的错误提示,因此我们自定义了错误处理信息。

 

 (4)定义颜色

为图像分配颜色,返回一个标识符,代表了由给定的 RGB 成分组成的颜色,如果分配失败则返回 -1 。

语法:int imagecolorallocate( resource image, int red, int green, int blue )

参数 red,green 和 blue 分别是所需要的颜色的 红,绿,蓝 成分,取值范围 0 - 255。

<?php
header("Content-type: image/png");
//创建图像
$im = imagecreate(200, 50) or die("创建图像资源失败");
//图片背景颜色并填充
$bg = imagecolorallocate($im, 255, 255, 255);
//以PNG格式输出图像
imagepng($im);
//销毁图像资源
imagedestroy($im);
?>

注意:

①对于用 imagecreate() 建立的图像,第一次调用 imagecolorallocate() 会给图像填充背景色(如上面例子)。

②对于用 imagecreatetruecolor() 建立的图像,则需要使用别的指令如 imagefill() 填充背景。

 

(5)颜色透明度

imagecolorallocatealpha() 和 imagecolorallocate() 用法相同,但多了一个额外的透明度参数 alpha,其值从 0 到 127。0 表示完全不透明,127 表示完全透明。

语法:int imagecolorallocatealpha( resource image, int red, int green, int blue, int alpha )

 

(6)去除定义颜色

imagecolordeallocate() 函数用于取消先前由 imagecolorallocate() 和imagecolorallocatealpha() 函数为图像分配的颜色。

语法:bool imagecolordeallocate( resource image, int color )

<?php
header("Content-type: image/png");
//创建图像
$im = imagecreate(200, 50) or die("创建图像资源失败");
//图片背景颜色并填充
$bg = imagecolorallocate($im, 255, 255, 255);
//去除定义颜色
imagecolordeallocate($im, 255, 255, 255);
//以PNG格式输出图像
imagepng($im);
//销毁图像资源
imagedestroy($im);
?>

 

(7)复制图像

imagecopy() 函数用于拷贝图像或图像的一部分,成功返回 TRUE ,否则返回 FALSE 

语法:bool imagecopy( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,int src_w, int src_h )

参数说明
dst_im 目标图像
src_im 被拷贝的源图像
dst_x 目标图像开始 x 坐标
dst_y 目标图像开始 y 坐标,x,y同为 0 则从左上角开始
src_x 拷贝图像开始 x 坐标
src_y 拷贝图像开始 y 坐标,x,y同为 0 则从左上角开始拷贝
src_w (从 src_x 开始)拷贝的宽度
src_h (从 src_y 开始)拷贝的高度
<?php
header("Content-type: image/jpeg");
//创建目标图像
$dst_im = imagecreatetruecolor(150, 150);
//源图像
$src_im = @imagecreatefromjpeg("./one.jpg");
//拷贝源图像左上角起始 150px 150px
imagecopy( $dst_im, $src_im, 0, 0, 0, 0, 150, 150 );
//输出拷贝后图像
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);
?>

 

(8)调整大小

imagecopyresized() 函数用于拷贝图像或图像的一部分并调整大小,成功返回 TRUE ,否则返回 FALSE 

语法:bool imagecopyresized( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )

本函数参数可参看 imagecopy() 函数,只是本函数增加了两个参数(注意顺序):dst_w:目标图像的宽度;dst_h:目标图像的高度。

imagecopyresized() 的典型应用就是生成图片的缩略图

<?php
header("Content-type: image/jpeg");
//原图文件
$file = "./one.jpg";
// 缩略图比例
$percent = 0.5;
// 缩略图尺寸
list($width, $height) = getimagesize($file);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 加载图像
$src_im = imagecreatefromjpeg($file);
$dst_im = imagecreatetruecolor($newwidth, $newheight);
// 调整大小
imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//输出缩小后的图像
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);
?>

 

(9)合并图像(水印制作实例)

imagecopymerge():拷贝并合并图像的一部分,成功返回true,否则返回false

语法:bool imagecopymerge( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,int src_w, int src_h, int pct )

参数说明
dst_im 目标图像
src_im 被拷贝的源图像
dst_x 目标图像开始 x 坐标
dst_y 目标图像开始 y 坐标,x,y同为 0 则从左上角开始
src_x 拷贝图像开始 x 坐标
src_y 拷贝图像开始 y 坐标,x,y同为 0 则从左上角开始拷贝
src_w (从 src_x 开始)拷贝的宽度
src_h (从 src_y 开始)拷贝的高度
pct 图像合并程度,取值 0-100 ,当 pct=0 时,实际上什么也没做,反之完全合并。


当为 pct = 100 时对于调色板图像本函数和 imagecopy() 完全一样,参考阅读

imagecopy():拷贝图像或图像的一部分。

该函数的一个典型应用就是将图像打上水印标识

 

(10)绘制线段与圆弧

①imageline():绘制一条线段

语法:bool imageline( resource image, int x1, int y1, int x2, int y2, int color )

用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角坐标为 0,0)画一条线段。例子:

<?php
    header("Content-type: image/png");
    $im = imagecreate(300, 300)or die("创建图像资源失败");
    $red = imagecolorallocate($im, 255, 0, 0);//第一个背景色
    $bg = imagecolorallocate($im, 204, 204, 204);//第二个前景色
    imageline($im,0,30,200,30,$bg);
    imagepng($im);
    imagedestroy($im);
?>

②imagesetstyle():设定画线风格

imagesetstyle() 设定所有画线的函数(例如 imageline() 和 imagepolygon())

在使用特殊颜色 IMG_COLOR_STYLED 或者用 IMG_COLOR_STYLEDBRUSHED 画一行图像时所使用的风格。如果成功则返回 TRUE ,失败则返回 FALSE 。

语法:bool imagesetstyle( resource image, array style )

style 参数是像素组成的数组。imageline() 函数配合 imagesetstyle() 可以画一条虚线段:

<?php
header("Content-type: image/png");
$im = imagecreate(300, 50)or die("创建图像资源失败");
$bg = imagecolorallocate($im, 204, 204, 204);
$red = imagecolorallocate($im, 255, 0, 0);
// 画一条虚线,5 个红色像素,4 个背景像素
$style = array($red, $red, $red, $red, $red, $bg, $bg, $bg, $bg);
imagesetstyle($im, $style);
imageline($im, 0, 20, 200, 20, IMG_COLOR_STYLED);
imagepng($im);
imagedestroy($im);
?>

③imagearc():绘制椭圆弧(包括圆弧)

语法:bool imagearc(resource image, int cx, int cy, int w, int h, int s, int e, int color )

参数说明
image 图像资源,欲绘制椭圆弧的图像
cx 椭圆中心 x 坐标
cy 椭圆中心 y 坐标
w 椭圆宽度
h 椭圆高度
s 起始点,0 表示 3 点钟方向
e 角度,360 表示完全封闭
color 图像颜色

 

<?php
header("Content-type: image/png");
$im = imagecreate(200, 200)or die("创建图像资源失败");
$bg = imagecolorallocate($im, 204, 204, 204);//第一个背景色
$red = imagecolorallocate($im, 255, 0, 0);//第二个为前景色
imagearc($im, 100, 100, 150, 150, 0, 360, $red);
imagepng($im);
imagedestroy($im);
?>

绘制虚线圆:

<?php
header("Content-type: image/png");
$im = imagecreate(200, 200)or die("创建图像资源失败");
$bg = imagecolorallocate($im, 204, 204, 204);//第一个背景色
$red = imagecolorallocate($im, 255, 0, 0);//第二个为前景色
$style = array($red, $red, $red, $red, $red, $bg, $bg, $bg, $bg);//虚线
imagesetstyle($im, $style);
imagearc($im, 100, 100, 150, 150, 0, 360, IMG_COLOR_STYLED);
imagepng($im);
imagedestroy($im);
?>

 

(10)图像填充

①imagefill():填充图像区域。

语法:bool imagefill( resource image, int x, int y, int color )

x,y 分别为填充的起始 x 坐标和 y 坐标,与 x, y 点颜色相同且相邻的点都会被填充。

提示:对于用 imagecreate() 建立的图像,第一次调用 imagecolorallocate() 会自动给图像填充背景色。

<?php
header("Content-type: image/png");
$im = imagecreatetruecolor(200, 200);
$red = imagecolorallocate($im, 255, 0, 0);
//用 $red 颜色填充图像
imagefill( $im, 0, 0, $red );
imagepng($im);
imagedestroy($im);
?>

②imagefilledarc():画一椭圆弧并填充

该函数典型应用之一是画饼状统计图。

语法:bool imagefilledarc( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )

该函数参数用法可参考绘制椭圆弧函数 imagearc() ,只是本函数增加 style 参数表示填充方式

填充方式说明
IMG_ARC_PIE 普通填充,产生圆形边界
IMG_ARC_CHORD 只是用直线连接了起始和结束点,与 IMG_ARC_PIE 方式互斥
IMG_ARC_NOFILL 指明弧或弦只有轮廓,不填充
IMG_ARC_EDGED 指明用直线将起始和结束点与中心点相连
<?php
header('Content-type: image/png');
$im = imagecreatetruecolor(100, 100);
$red = imagecolorallocate($im, 255, 0, 0);
imagefilledarc($im, 50, 50, 100, 50, 0, 360 , $red, IMG_ARC_EDGED);
imagepng($im);
imagedestroy($im);
?>

③imagefilledrectangle():画一矩形并填充。

imagefilledrectangle() 函数画一矩形并填充。

语法:bool imagefilledrectangle( resource image, int x1, int y1, int x2, int y2, int color )

x1,y1为左上角左边,x2,y2为右下角坐标。该函数典型应用之一是柱状统计图。例子:

<?php
header('Content-type: image/png');
$im = imagecreatetruecolor(200, 200);
$yellow = imagecolorallocate($im, 255, 255, 0);
imagefilledrectangle($im, 20, 150, 40, 200, $yellow);
imagefilledrectangle($im, 50, 80, 70, 200, $yellow);
imagepng($im);
imagedestroy($im);
?>

④imagefilledpolygon():画一多边形并填充

语法:bool imagefilledpolygon( resource image, array points, int num_points, int color )

参数说明
image 图像资源,欲绘制多边形的图像
points 按顺序包含有多边形各顶点的 x 和 y 坐标的数组
num_points 顶点的总数,必须大于 3
color 图像的颜色

 绘制一个用红色填充的六边形例子:

<?php
header('Content-type: image/png');
$points = array(
            50, 50,    // Point 1 (x, y)
            100, 50,     // Point 2 (x, y)
            150, 100,     // Point 3 (x, y)
            150, 150,    // Point 4 (x, y)
            100, 150,     // Point 5 (x, y)
            50, 100    // Point 6 (x, y)
            );

$im = imagecreatetruecolor(200, 200);
$red = imagecolorallocate($im, 255, 0, 0);
imagefilledpolygon($im, $points, 6, $red);
imagepng($im);
imagedestroy($im);
?>

 

 

注意:

(1)网页调用

对于 PHP 生成的图片,如果要直接在普通网页中显示而不是通过 header 输出,可以通过如下的方式调用:

<img src="pic.php" />