暂时mark一下。
php提供了丰富的图像处理函数,主要包括如下几种:
①获取图像信息的函数
②创建与销毁图像的函数
③载入图像的函数
④输出图像的函数
⑤分配/取消图像颜色的函数
⑥拷贝图像的函数
⑦合并图像的函数
⑧绘制线段与圆弧的函数
⑨图像填充函数
在使用php的图像处理函数之前,需要开启php.ini中的gd2库扩展
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)
}
常用函数:
(1)getimagesize():此函数主要用于获取图像的大小及相关信息,成功则返回一个数组,失败则返回false
语法:
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");
(2)imagesx(),imagesy():这两个函数分别用来获取图像的宽度和高度,单位为像素,返回值为整型
语法:
int imagesx(resource image)
int imagesy(resource image)
注意:
参数为如 imagecreatetruecolor()、imagecreatefromjpeg() 等函数返回的图像资源
案例:
<?php
$img = imagecreatefromjpeg("images/flower_1.jpg");
echo "图像宽度:",imagesx( $img ),"<br />";
echo "图像高度:",imagesy( $img );
?>
浏览器输出:
图像宽度:350
图像高度:318
(3)如果我们要对图像进行处理,就如其它图像处理软件一样,需要创建一块画布。
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);
?>
(4)图像处理完成后,使用imagedestroy()函数销毁图像资源以释放内存
语法:
bool imagedestroy(resource image)
(5)此系列函数用于从文件或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 )
(6)载入图像案例:
<?
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允许将图像以不同格式输出,该系列函数有:
①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]] )
<img src="https://img-my.csdn.net/uploads/201209/16/1347794010_6339.png" alt="">
php处理图像的函数
最新推荐文章于 2021-03-12 00:15:54 发布