.

PHP 图像处理18

文章作者:eKing
发表时间:2016年11月20日 08:01
浏览次数:363

PHP 图像处理

PHP 图像处理

PHP 提供了丰富的图像处理函数,主要包括:

获取图像信息

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

imagesx():获取图像宽度。

imagesy():获取图像高度。

创建图像

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

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

销毁图像资源

imagedestroy():销毁图像资源。

载入图像

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

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

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

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

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

输出图像

imagegif():以 GIF 格式将图像输出到浏览器或文件

imagejpeg():以 JPEG 格式将图像输出到浏览器或文件

imagepng():以 PNG 格式将图像输出到浏览器或文件

imagewbmp():以 WBMP 格式将图像输出到浏览器或文件

分配/取消图像颜色

imagecolorallocate():为图像分配颜色。

imagecolordeallocate():取消先前由 imagecolorallocate() 等函数为图像分配的颜色。

拷贝图像

imagecopy():拷贝图像。

imagecopyresized():拷贝图像并调整大小。

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

imagecopymerge():拷贝并合并图像的一部分。

绘制线段与圆弧

imageline():绘制一条线段。

imagesetstyle():设定画线风格。

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

图像填充

imagefill():填充图像区域。

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

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

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

GD 库

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

extension = php_gd2.dll

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

<?php

var_dump(gd_info());

?>

输出大致如下:

array(12) {

["GD Version"]=>

string(27) "bundled (2.0.34 compatible)"

["FreeType Support"]=>

bool(true)

["FreeType Linkage"]=>

string(13) "with freetype"

["T1Lib Support"]=>

bool(true)

["GIF Read Support"]=>

bool(true)

["GIF Create Support"]=>

bool(true)

["JPG Support"]=>

bool(true)

["PNG Support"]=>

bool(true)

["WBMP Support"]=>

bool(true)

["XPM Support"]=>

bool(false)

["XBM Support"]=>

bool(true)

["JIS-mapped Japanese Font Support"]=>

bool(false)

}

PHP 获取图像信息 getimagesize 函数

getimagesize() 函数用于获取图像尺寸,类型等信息。

imagesx() 函数用于获取图像的宽度。

imagesy() 函数用于获取图像的高度。

getimagesize()

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

语法:

array getimagesize( string filename )

例子:

<?php

$array = getimagesize("images/flower_1.jpg");

print_r($array);

?>

浏览器显示如下:

Array

(

[0] => 350

[1] => 318

[2] => 2

[3] => width="350" height="318"

[bits] => 8

[channels] => 3

[mime] => image/jpeg

)

返回结果说明

索引 0 给出的是图像宽度的像素值

索引 1 给出的是图像高度的像素值

索引 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 给出的是一个宽度和高度的字符串,可以直接用于 HTML 的 <image> 标签

索引 bits 给出的是图像的每种颜色的位数,二进制格式

索引 channels 给出的是图像的通道值,RGB 图像默认是 3

索引 mime 给出的是图像的 MIME 信息,此信息可以用来在 HTTP Content-type 头信息中发送正确的信息,如:

header("Content-type: image/jpeg");

提示

如您要在自己的电脑上运行本教程中图像处理的例子,请将教程中用到的图片下载到本地 images 文件夹下备用:

http://www.5idev.com/Public/Images/article/flower_1.jpg

http://www.5idev.com/Public/Images/article/logo_mark.gif

imagesx()

imagesx() 函数用于获取图像的宽度,单位为像素,返回值为整型。

语法:

int imagesx( resource image )

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

imagesy()

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

语法:

int imagesy( resource image )

例子:

<?php

$img = imagecreatefromjpeg("images/flower_1.jpg");

echo "图像宽度:",imagesx( $img ),"<br />";

echo "图像高度:",imagesy( $img );

?>

浏览器输出:

图像宽度:350

图像高度:318

PHP 创建图像、销毁图像 imagecreate 与 imagedestroy 函数

imagecreate() 和 imagecreatetruecolor() 函数用于创建一幅空白图像。

imagedestroy() 函数用于销毁图像资源。

imagecreate()

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

语法:

resource imagecreate( int x, int y )

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

例子:

<?

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()

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

语法:

resource imagecreatetruecolor( int x, int y )

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

imagedestroy()

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

语法:

bool imagedestroy( resource image )

具体使用可见上面创建图像例子。

PHP 载入图像 imagecreatefrom_gif_jpeg_png 系列函数

imagecreatefrom 系列函数用于从文件或 URL 载入一幅图像。

载入图像

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

该系列函数有:

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

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

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

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

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

语法:

resource imagecreatefromgif( string filename )

resource imagecreatefromjpeg( string filename )

resource imagecreatefrompng( string filename )

resource imagecreatefromwbmp( string filename )

resource imagecreatefromstring( string image )

例子:

<?

header("Content-type: image/jpeg");

//创建并载入一幅图像

$im = @imagecreatefromjpeg("images/flower_1.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 对图像创建错误没有友好的错误提示,因此我们自定义了错误处理信息。

提示

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

<img src="pic.php" />

PHP 输出图像 imagegif 、imagejpeg 与 imagepng 函数

imagegif()、imagejpeg()、imagepng() 和 imagewbmp() 函数分别允许以 GIF、JPEG、PNG 和 WBMP 格式将图像输出到浏览器或文件。

PHP 输出图像

PHP 允许将图像以不同格式输出:

imagegif():以 GIF 格式将图像输出到浏览器或文件

imagejpeg():以 JPEG 格式将图像输出到浏览器或文件

imagepng():以 PNG 格式将图像输出到浏览器或文件

imagewbmp():以 WBMP 格式将图像输出到浏览器或文件

语法:

bool imagegif ( resource image [, string filename] )

bool imagejpeg ( resource image [, string filename [, int quality]] )

bool imagepng ( resource image [, string filename] )

bool imagewbmp ( resource image [, string filename [, int foreground]] )

参数说明: 参数 说明

image 欲输出的图像资源,如 imagecreate() 或 imagecreatefrom 系列函数的返回值

filename 可选,指定输出图像的文件名。如果省略,则原始图像流将被直接输出。

quality 可选,指定图像质量,范围从 0(最差质量,文件最小)到 100(最佳质量,文件最大),默认75 ,imagejpeg() 独有参数

foreground 可选,指定前景色,默认前景色是黑色,imagewbmp() 独有参数

绘制一个圆弧并保存到 images 目录下:

<?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,"images/circle.png");

imagedestroy($im);

?>

在 images 目录下就会生成一个 circle.png 文件。

PHP 分配、取消图像颜色 imagecolorallocate 与 imagecolordeallocate 函数

imagecolorallocate() 函数用于为图像分配颜色。

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

imagecolorallocate()

imagecolorallocate() 函数用于为图像分配颜色,返回一个标识符,代表了由给定的 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, 204, 204, 204);

//设定文字颜色

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

//水平画一行字

imagestring($im, 5, 0, 0, "Hello world!", $red);

//以PNG格式输出图像

imagepng($im);

//销毁图像资源

imagedestroy($im);

?>

提示

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

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

imagecolorallocatealpha()

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

语法:

int imagecolorallocatealpha( resource image, int red, int green, int blue, int alpha )

imagecolordeallocate()

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

语法:

bool imagecolordeallocate( resource image, int color )

例子:

<?

$im = @imagecreate(200, 50) or die("创建图像资源失败");

$bg = imagecolorallocate($im, 255, 0, 0);

imagecolordeallocate($im, $bg);

?>

PHP 拷贝图像 imagecopy 与 imagecopyresized 函数

imagecopy() 函数用于拷贝图像或图像的一部分。

imagecopyresized() 函数用于拷贝部分图像并调整大小。

imagecopy()

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("images/flower_1.jpg");

//拷贝源图像左上角起始 150px 150px

imagecopy( $dst_im, $src_im, 0, 0, 0, 0, 150, 150 );

//输出拷贝后图像

imagejpeg($dst_im);

imagedestroy($dst_im);

imagedestroy($src_im);

?>

imagecopyresized()

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 = "images/flower_1.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);

?>

上面的例子将原图缩小为原来的一半尺寸。

PHP 合并图像 imagecopymerge 函数(水印制作实例)

imagecopymerge() 函数用于拷贝并合并图像的一部分。

imagecopymerge()

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():拷贝图像或图像的一部分。

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

<?

header("Content-type: image/jpeg");

//原始图像

$dst = "images/flower_1.jpg";

//得到原始图片信息

$dst_im = imagecreatefromjpeg($dst);

$dst_info = getimagesize($dst);

//水印图像

$src = "images/logo.gif";

$src_im = imagecreatefromgif($src);

$src_info = getimagesize($src);

//水印透明度

$alpha = 30;

//合并水印图片

imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0],$dst_info[1]-$src_info[1],0,0,$src_info[0],

$src_info[1],$alpha);

//输出合并后水印图片

imagejpeg($dst_im);

imagedestroy($dst_im);

imagedestroy($src_im);

?>

浏览器显示水印图片效果如下:

PHP 绘制线段与圆弧 imageline、imagesetstyle 与 imagearc 函数

imageline() 函数用于绘制一条线段。

imagearc() 函数用于绘制椭圆弧(包括圆弧)。

imagesetstyle() 函数用于设定画线风格。

imageline()

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("创建图像资源失败");

$bg = imagecolorallocate($im, 204, 204, 204);

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

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

imagepng($im);

imagedestroy($im);

?>

浏览器输出图像如下:

参考阅读

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

imagecolorallocate():为图像分配颜色。

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()

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 图像填充 imagefill、imagefilledarc 与 imagefilledrectangle() 函数

imagefill() 函数用于图像区域填充。

imagefilledarc() 函数画一椭圆弧并填充。

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

imagefilledpolygon() 函数画一多边形并填充。

imagefill()

imagefill() 函数用于区域填充。

语法:

bool imagefill( resource image, int x, int y, int color )

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

例子:

<?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);

?>

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

imagefilledarc()

imagefilledarc() 函数画一椭圆弧并填充。

语法:

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

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

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_PIE);

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()

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);

?>

上一篇:PHP Session17
下一篇:PHP 数学计算19