【小白笔记】PHP学习之路 (29) --图像处理、绘制像素、矩形、多边形

来源:互联网 发布:东方犹太人知乎 编辑:程序博客网 时间:2024/05/01 06:14

点、线、矩形、多边形、椭圆、圆弧绘制:

imagesetpixel()      绘制像素点。

imageline()      绘制一条线。

imagerectangle()      绘制矩形。

imagefilledrectangle()      在图像上绘制填充的矩形。

imagecolorsforindex()       取得某索引的RGBA颜色。返回含red、green、blue、alpha的数组。alpha为127时全透明。

imageellipse()      绘制一个椭圆。如果指定的宽高相等则绘制一个圆。

imagefilledellipse()      绘制一个填充的椭圆。

imagearc()       画一个椭圆弧。w和 h 分别指定了椭圆的宽度和高度,起始和结束点参数以角度指定。以顺时针方向绘画。

imagefilledarc()      画一椭圆弧且填充

imagepolygon()      画一个多边形。具体参数见手册。

imagefilledpolygon()      绘制一个填充的多边形。

相关设置:

imagesetbrush()      设置画线用的笔刷。

imagesetthickness()      设置画线的粗细。

imagesetstyle()      设定画线的风格。

imagesettile()      设定用于填充的贴图。



图像叠加、旋转:

imagerotate()    将图像旋转指定的角度。可以用指定的颜色填充旋转后的空白区域。

                            传递第四个非零参数,将忽略掉原图像的透明色。返回旋转后的图像资源。

imagecolortransparent()      传递一个参数时获取图像透明色的索引。如果没有透明色,返回-1。

                                                  传递第二个颜色参数,将透明色改变为指定的颜色。

imagecolorset()      给指定调色板索引设定颜色。和上一个函数配合使用,可将透明色改变为背景色。

imagecolorsforindex()      获得指定索引的rgba颜色。以关联数组(red、green、blue、alpha)形式返回。

imagecopy()      将一个图像复制到另一张图像上,可以指定图像的位置、宽度。

imagecopymerge()      拷贝图像并合并。与imagecopy()用法一样,只不过增加了一个参数,可以指定透明度(融合度)。

                                         注:此函数不支持透明图像的拷贝,例如把一个部分透明的png水印拷贝到新图像上,那么水印

                                                 原本的透明信息会丢失,整个会变成不透明,此时传递的最后一个透明度参数的作用是指定

                                                 这个已经变得不透明的水印的整体透明度。

                                                 建议使用imagecopyresampled(),因为它支持透明图像的拷贝。

imagecopymergegray()      用法与上一个函数一样,只不过将目标图转换为灰度图像(原图色彩不变)。

imagecopyresampled()      拷贝图像并可以指定拷贝尺寸和大小,也可调整高宽比。此函数适合用来制作缩略图。

imagecopyresized()      用法完全同上。区别在于此函数在缩放后不会重新取样,因此可能会造成颜色信息的丢失。

                                          建议尽量使用imagecopyresampled()。


色彩判断、转换:

imagecolorstotal()      获取一幅图像中调色板颜色的总数。

imageistruecolor()      判读图像是否为真彩色图像。

imagetruecolortopalette()      将真彩色图像转换为调色板图像。第二个参数指定是否平滑过渡,第三个参数指定转化后的颜色数。

imagecolorat()      获得指定位置的颜色索引值;

                                对于真彩色图像,可以再使用此函数之后再使用imagecolorsforindex()将其转化为包含rgba的数组。

                                也可以通过移位加掩码取得各颜色的值。

imagecolorset()      将某一指定索引色修改为新的颜色。可以和imagecolorat()结合使用。


tips:

imagejpeg()      可传递第二个参数将其保存为文件,传递第三个参数0~100指定压缩比。

创建一个图像资源后,在脚本结束前应当使用destroy()函数将其关闭。

虽然php具有一套垃圾回收机制,但是建议初学者仍然手动将其关闭。


进制转换:

hexdec()      将十六进制转换为十进制。

dechex()      将十进制转换为十六进制。

bin2hex()      将二进制转化为十六进制。

hex2bin()      转换十六进制字符串为二进制字符串

octdec()      八进制转换为十进制

decoct()      十进制转换为八进制

decbin()      十进制转换为二进制

bindec()      二进制转换为十进制

一般用0开头的数字表示八进制数,用0x开头的数字字母串表示十六进制数,用0b开头表示二进制数。


以下仅为个人练习、作用法格式参考,详细请查询php手册:

<?php/* $img = imagecreatetruecolor ( 300, 300);$w = imagesx ( $img );$h = imagesy ( $img );$red = imagecolorallocate ( $img, 255, 0, 0 );$white = imagecolorallocate ( $img, 255, 255, 255 );$color_line = imagecolorallocate ( $img, 180, 180, 180 );imageline ( $img, 0, 0, 299, 0, $color_line );imageline ( $img, 299, 0, 299, 299, $color_line );imageline ( $img, 299, 299, 0, 299, $color_line );imageline ( $img, 0, 299, 0, 0, $color_line );for($i = 0; $i < 299; $i ++) {$color_rand = imagecolorallocate ( $img, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );imagesetpixel ( $img, mt_rand ( 0, $w ), mt_rand ( 0, $h ), $color_rand );}header ( "Content-type:image/png" );imagepng ( $img ); *//* $rec_img = imagecreatetruecolor(500, 500);$yellow = imagecolorallocate($rec_img, 255, 255, 0);imagefill($rec_img, 0, 0, $yellow);$black = imagecolorallocate($rec_img, 0, 0, 0);$white = imagecolorallocate($rec_img, 255, 255, 255);//imagerectangle($rec_img, 0, 100, 400, 400, $black);$w = imagesx($rec_img);$h = imagesy($rec_img);for ($i = 0; $i < 50; $i++) {imagesetthickness($rec_img, mt_rand(1,10));$color_rand = imagecolorallocate ( $rec_img, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );imagerectangle($rec_img, mt_rand(0, $w), mt_rand(0, $h), mt_rand(0, $w), mt_rand(0, $w), $color_rand);}header("Content-type:image/png");imagepng($rec_img); */$filename = 'pic3.jpg';$img = imagecreatefromjpeg($filename);$imgInfo = getimagesize($filename);$blue = imagecolorallocate($img, 0, 0, 200);imagefill($img, 0, 0, $blue);imagefilledrectangle($img, 0, 0, 100, 100, $blue);header("Content-type:image/png");imagepng($img);?>

绘制饼状图:

function PieChart($img,$x=300,$y=300,$w=400,$h=80){$ip = array("baidu"=>1550,"google"=>1820,"sohu"=>1500,"sina"=>1200,'youdao'=>666);asort($ip);$sum = array_sum($ip);for ($i = 0;$i < 20;$i++){$m = 0;foreach($ip as $k=>$v){if($i==0){eval('$color_'.$k.' = imagecolorallocate($img,mt_rand(0,230),mt_rand(0,240),mt_rand(0,250));');}eval('$color_tmp = $color_'.$k.';');imagefilledarc($img, $x, $y-$i, $w, $h, $m,  min(ceil($m+$v/$sum*360),360), $color_tmp, IMG_ARC_PIE);$m += ceil($v/$sum*360);}}}$img = imagecreatetruecolor(600, 600);imagefill($img, 11, 11, imagecolorallocate($img, 255, 255, 255));PieChart($img);header("Content-type:image/png");imagepng($img);

结果:


0 0
原创粉丝点击