PHP 图像处理函数 php图像识别技术
qiyuwang 2024-10-15 14:49 18 浏览 0 评论
(1)创建画布 --- 创建资源类型 --- 高度 宽度
resource imagecreate ( int x_size, int y_size ) 创建一个基于普通调色板的图像, 通常支持256色(png、gif、jepg等都支持)
resource imagecreatetruecolor ( int x_size, int y_size ) 创建一个真彩色的图像, 但不能用于gif格式
(2)绘制图像
制定各种颜色 int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
用imagecreate()建立的图像, 也可以填充为背景颜色(注意:第一次调用即可为画布设置背景色),兼有代替函数imagefill的填充功能
(理解:第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色, 即用于imagecreate()建立的画布,但不能用于
imagecreatetruecolor()创建的画布)
图形区域填充 bool imagefill ( resource $image , int $x , int $y , int $color )
可以填充为背景颜色,只能用于imagecreatetruecolor()创建的真彩色图像;
矩形, 圆, 点, 线段, 扇形, 画字(字符, 字符串, freetype)
每一个图像对应一个函数
imagerectangle() 画一个矩形
bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
imagefilledrectangle() 画一矩形并填充
bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageellipse() 画一个椭圆
bool imageellipse ( resource $image , int $cx , int $cy , int $w , int $h , int $color )
imagefilledellipse() 画一个椭圆并填充filled
bool imagefilledellipse ( resource $image , int $cx , int $cy , int $w , int $h , int $color )
imagesetpixel() — 画一个单一像素
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
imageline() — 画一条线段
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imagearc() — 画椭圆弧
bool imagearc ( resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color )
imagearc() 以 cx,cy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。
w和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。
imagefilledarc() 画椭圆弧并填充
bool imagefilledarc ( resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color , int $style )
style:
IMG_ARC_PIE 则产生圆形边界
IMG_ARC_CHORD 只是用直线连接了起始和结束点(IMG_ARC_PIE 和 IMG_ARC_CHORD是互斥的,如果两个都用,IMG_ARC_CHORD生效)。
IMG_ARC_NOFILL 指明弧或弦只有轮廓,不填充。
IMG_ARC_EDGED 指明用直线将起始和结束点与中心点相连,和 IMG_ARC_NOFILL一起使用是画饼状图轮廓的好方法(而不用填充)。
(chord:和弦 pie:馅饼(扇形图) nofill:不填充 edged: 有刃的;有边的)
imagepolygon() — 画一个多边形
bool imagepolygon ( resource $image , array $points , int $num_points , int $color )
imagefilledpolygon() — 画一多边形并填充
bool imagefilledpolygon ( resource $image , array $points , int $num_points , int $color )
<?php
// 创建画布
$image = imagecreatetruecolor(400, 300);
// 填充背景颜色
$bg = imagecolorallocate($image, 0, 0, 0);
// 分配颜色
$col_poly = imagecolorallocate($image, 255, 255, 255);
// 绘制多边形
imagepolygon($image,
array (
0, 0,
100, 200,
300, 200
),
3,
$col_poly);
// 输出图像
header("Content-type: image/png");
imagepng($image);
?>
imagechar() — 水平地画一个字符
bool imagechar ( resource $image , int $font , int $x , int $y , string $c , int $color )
imagechar() 将字符串 c 的第一个字符画在 image 指定的图像中,其左上角位于 x,y(图像左上角为 0, 0),颜色为 color。
如果 font是 1,2,3,4, 5(1最小,5最大),则使用内置的字体(更大的数字对应于更大的字体)。
实例:
<?php
$im = imagecreate ( 100 , 100 );
$string = 'PHP' ;
$bg = imagecolorallocate ( $im , 255 , 255 , 255 );
$black = imagecolorallocate ( $im , 0 , 0 , 0 );
imagechar ( $im , 1 , 0 , 0 , $string , $black );
header ( 'Content-type: image/png' );
imagepng ( $im );
?>
imagecharup() — 垂直地画一个字符
bool imagecharup ( resource $image , int $font , int $x , int $y , string $c , int $color )
imagestring() — 水平地画一行字符串
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
实例:
<?php
// 建立一幅 100X30 的图像
$im = imagecreate ( 100 , 30 );
// 白色背景和蓝色文本
$bg = imagecolorallocate ( $im , 255 , 255 , 255 );
$textcolor = imagecolorallocate ( $im , 0 , 0 , 255 );
// 把字符串写在图像左上角
imagestring ( $im , 5 , 0 , 0 , "Hello world!" , $textcolor );
// 输出图像
header ( "Content-type: image/png" );
imagepng ( $im );
?>
imagestringup() — 垂直地画一行字符串
bool imagestringup ( resource $image , int $font , int $x , int $y , string $s , int $col )
注意:以上四个函数均不支持中文,支持中文则使用imagettftext()函数
imagettftext() — 用 TrueType 字体向图像写入文本
该函数与分辨率无关,输出时总是按照打印机的分辨率输出,无论是放大或缩小,字符总是光滑的,不会有锯齿出现。
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
size:字体的尺寸。根据 GD 的版本,为像素尺寸(GD1)或点(磅)尺寸(GD2)。(1磅等于4/3像素,约1.33像素)
angle:角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。
fontfile:是指要使用的 TrueType 字体的路径,很多情况下字体都放在脚本的同一个目录下。
imagettftext()函数只支持utf-8编码,所以如果创建的网页的编码格式使用GB2312,那么在应用imagettftext()函数输出中文字符串时,
必须应用iconv()函数将字符串的编码格式由GB2312转换为utf-8,否则在输出时将会出现乱码。
注意:如果网页是uft-8的编码,就没必要用iconv()函数,否则就会出现错误, $text=iconv("GB2312", "UTF-8", "LAMP编程--Linux操作系统!");
只能转出"LAMP",而后面的汉字"编程--Linux操作系统!"转不出来
(3)输出图像/保存处理好的图像
1.通过 header() 发送 Content-type: image/jpeg 可以使 PHP 脚本直接输出 JPEG 图像。
注意:header头不要在前面有任何输出 header("content-type:image/jpeg");
2. 输出各种类型(gif, png, jpeg)
imagegif(); 以 GIF 格式将图像输出到浏览器或文件
bool imagegif ( resource $image [, string $filename ] )
imagejpeg(); 以 JPEG 格式将图像输出到浏览器或文件
bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )
imagejpeg() 从 image图像以 filename 为文件名创建一个 JPEG 图像。image参数是 imagecreatetruecolor() 函数的返回值。
filename:参数为可选,如果省略,则原始图像流将被直接输出。(如果保存图片的话,就不需要使用函数header()发送头信息)
要省略 filename 参数而提供 quality 参数, 使用空字符串('',注意不是空格字符' ')或者null。
quality:为可选项,范围从 0(最差质量,文件更小)到 100(最佳质量,文件最大),默认的质量值(大约 75)。
imagepng(); 以 PNG 格式将图像输出到浏览器或文件
bool imagepng ( resource $image [, string $filename ] )
(4)释放资源
imagedestroy() — 销毁一图像
bool imagedestroy ( resource $image )
<?php
$im = imagecreatetruecolor(400, 30); //创建400x300像素大小的画布
$white = imagecolorallocate($im, 255, 255, 255); //创建白色
$grey = imagecolorallocate($im, 128, 128, 128); //创建灰色
$black = imagecolorallocate($im, 0, 0, 0); //创建黑色
imagefilledrectangle($im, 0, 0, 399, 29, $white); //输出一个使用白色填充的矩形作为背景
//如果有中文输出,需要将其转码,转换为UTF-8的字符串才可以直接传递
$text=iconv("GB2312", "UTF-8", "LAMP兄弟连--无兄弟,不编程!");
$font = 'simsun.ttc'; //指定字体,将系统中与simsum.ttc对应的字体复制到当前目录下
imagettftext($im, 20, 0, 12, 21, $grey, $font, $text); //输出一个灰色的字符串作为阴影
imagettftext($im, 20, 0, 10, 20, $black, $font, $text); //在阴影之上输出一个黑色的字符串
header("Content-type: image/png"); //通知浏览器将输出格式为PNG的图像
imagepng($im); //向浏览器中输出PNG格式的图像
imagedestroy($im); //销毁资源,释放内存占用的空间
?>
图像处理第二种方法:
imagecreatefromjpeg() 从 JPEG 文件或 URL 新建一图像
resource imagecreatefromjpeg ( string $filename )
该函数返回一图像标识符, 代表了从给定的文件名取得的图像。
在失败时返回一个空字符串, 并且输出一条错误信息, 不幸地在浏览器中显示为断链接。
为减轻调试工作下面的例子会产生一个错误 JPEG:
<?php
function LoadJpeg($imgname){
$im = @imagecreatefromjpeg($imgname); /* Attempt to open */
if(!$im) { /* See if it failed */
$im = imagecreatetruecolor(150, 30); /* Create a blank image */
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an errmsg */
imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}
?>
imagecreatefrompng() — 从 PNG 文件或 URL 新建一图像
resource imagecreatefrompng ( string $filename )
imagecreatefromgif() -从 GIF 文件或 URL 新建一图像
resource imagecreatefromgif ( string $filename )
其他图像处理函数
获取图像的大小
imagesx() 获取画布的宽度
imagesy() 获取画布的高度
<?php
$img = imagecreatetruecolor(300,200);
echo imagesx($img);
echo imagesy($img);
?>
查看GD库信息
<?php
//从服务器获取GD库的信息
if(function_exists("gd_info")){
$gd = gd_info(); //gd_info()函数返回的数组
$gdinfo = $gd["GD Version"];
}else{
$gdinfo="未知";
}
//从GD库中查看是否支持FreeType字体
$freetype = $gd["FreeType"]?"支持":"不支持";
echo $gdinfo;
echo "<br />";
echo $freetype;
?>
相关推荐
- # 安装打开 ubuntu-22.04.3-LTS 报错 解决方案
-
#安装打开ubuntu-22.04.3-LTS报错解决方案WslRegisterDistributionfailedwitherror:0x800701bcError:0x80070...
- 利用阿里云镜像在ubuntu上安装Docker
-
简介:...
- 如何将Ubuntu Kylin(优麒麟)19.10系统升级到20.04版本
-
UbuntuKylin系统使用一段时间后,有新的版本发布,如何将现有的UbuntuKylin系统升级到最新版本?可以通过下面的方法进行升级。1.先查看相关的UbuntuKylin系统版本情况。使...
- Ubuntu 16.10内部代号确认为Yakkety Yak
-
在正式宣布Ubuntu16.04LTS(XenialXerus)的当天,Canonical创始人MarkShuttleworth还非常开心的在个人微博上宣布Ubuntu下个版本16.10的内...
- 如何在win11的wsl上装ubuntu(怎么在windows上安装ubuntu)
-
在Windows11的WSL(WindowsSubsystemforLinux)上安装Ubuntu非常简单。以下是详细的步骤:---...
- Win11学院:如何在Windows 11上使用WSL安装Ubuntu
-
IT之家2月18日消息,科技媒体pureinfotech昨日(2月17日)发布博文,介绍了3中简便的方法,让你轻松在Windows11系统中,使用WindowsSubs...
- 如何查看Linux的IP地址(如何查看Linux的ip地址)
-
本头条号每天坚持更新原创干货技术文章,欢迎关注本头条号"Linux学习教程",公众号名称“Linux入门学习教程"。...
- 怎么看电脑系统?(怎么看电脑系统配置)
-
要查看电脑的操作系统信息,可以按照以下步骤操作,根据不同的操作系统选择对应的方法:一、Windows系统通过系统属性查看右键点击桌面上的“此电脑”(或“我的电脑”)图标,选择“属性”。在打开的...
- 如何查询 Linux 内核版本?这些命令一定要会!
-
Linux内核是操作系统的核心,负责管理硬件资源、调度进程、处理系统调用等关键任务。不同的内核版本可能支持不同的硬件特性、提供新的功能,或者修复了已知的安全漏洞。以下是查询内核版本的几个常见场景:...
- 深度剖析:Linux下查看系统版本与CPU架构
-
在Linux系统管理、维护以及软件部署的过程中,精准掌握系统版本和CPU架构是极为关键的基础操作。这些信息不仅有助于我们深入了解系统特性、判断软件兼容性,还能为后续的软件安装、性能优化提供重要依据。接...
- 504 错误代码解析与应对策略(504错误咋解决)
-
在互联网的使用过程中,用户偶尔会遭遇各种错误提示,其中504错误代码是较为常见的一种。504错误并非意味着网站被屏蔽,它实际上是指服务器在规定时间内未能从上游服务器获取响应,专业术语称为“Ga...
- 猎聘APP和官网崩了?回应:正对部分职位整改,临时域名可登录
-
10月12日,有网友反映猎聘网无法打开,猎聘APP无法登录。截至10月14日,仍有网友不断向猎聘官方微博下反映该情况,而猎聘官方微博未发布相关情况说明,只是在微博内对反映该情况的用户进行回复,“抱歉,...
- 域名解析的原理是什么?域名解析的流程是怎样的?
-
域名解析是网站正常运行的关键因素,因此网站管理者了解域名解析的原理和流程对于做好域名管理、解决常见解析问题,保障网站的正常运转十分必要。那么域名解析的原理是什么?域名解析的流程是怎样的?接下来,中科三...
- Linux无法解析域名的解决办法(linux 不能解析域名)
-
如果由于误操作,删除了系统原有的dhcp相关设置就无法正常解析域名。 此时,需要手动修改配置文件: /etc/resolv.conf 将域名解析服务器手动添加到配置文件中 该文件是DNS域名解...
- 域名劫持是什么?(域名劫持是什么)
-
域名劫持是互联网攻击的一种方式,通过攻击域名解析服务器(DNS),或伪造域名解析服务器(DNS)的方法,把目标网站域名解析到错误的地址从而实现用户无法访问目标网站的目的。说的直白些,域名劫持,就是把互...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- # 安装打开 ubuntu-22.04.3-LTS 报错 解决方案
- 利用阿里云镜像在ubuntu上安装Docker
- 如何将Ubuntu Kylin(优麒麟)19.10系统升级到20.04版本
- Ubuntu 16.10内部代号确认为Yakkety Yak
- 如何在win11的wsl上装ubuntu(怎么在windows上安装ubuntu)
- Win11学院:如何在Windows 11上使用WSL安装Ubuntu
- 如何查看Linux的IP地址(如何查看Linux的ip地址)
- 怎么看电脑系统?(怎么看电脑系统配置)
- 如何查询 Linux 内核版本?这些命令一定要会!
- 深度剖析:Linux下查看系统版本与CPU架构
- 标签列表
-
- navicat无法连接mysql服务器 (65)
- 下横线怎么打 (71)
- flash插件怎么安装 (60)
- lol体验服怎么进 (66)
- ae插件怎么安装 (62)
- yum卸载 (75)
- .key文件 (63)
- cad一打开就致命错误是怎么回事 (61)
- rpm文件怎么安装 (66)
- linux取消挂载 (81)
- ie代理配置错误 (61)
- ajax error (67)
- centos7 重启网络 (67)
- centos6下载 (58)
- mysql 外网访问权限 (69)
- centos查看内核版本 (61)
- ps错误16 (66)
- nodejs读取json文件 (64)
- centos7 1810 (59)
- 加载com加载项时运行错误 (67)
- php打乱数组顺序 (68)
- cad安装失败怎么解决 (58)
- 因文件头错误而不能打开怎么解决 (68)
- js判断字符串为空 (62)
- centos查看端口 (64)