设置画布颜色的方法:1、使用“imagecolorallocate(image,red,green,blue)”语句;2、使用“imagecolorallocatealpha(image,red,green,blue,alpha)”语句。
本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑
方法1:imagecolorallocate() 函数
imagecolorallocate() 函数可以为一个图像资源分配颜色,如果在图像中需要设置多种颜色,只要多次调用该函数即可。函数的语法格式如下所示:
imagecolorallocate(resource $image, int $red, int $green, int $blue):
其中,$image 为要设置颜色的图像资源,imagecolorallocate() 函数会返回一个标识符,代表了由给定的 RGB 成分组成的颜色;$red,$green 和 $blue 分别是所需要的颜色的红,绿,蓝成分,取值范围是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。
提示:如果是使用 imagecreate() 函数创建的图像资源,在第一次调用 imagecolorallocate() 函数时会默认为其填充背景色。
【示例】使用 imagecolorallocate() 函数为图像设置颜色。
<?php $image = imagecreate(100, 100); $blue = imagecolorallocate($image, 0, 0, 255); $red = imagecolorallocate($image, 255, 0, 0); $green = imagecolorallocate($image, 0, 255, 0); header('Content-type:image/jpeg'); imagejpeg($image); imagedestroy($image); ?>
运行结果如下图所示:
方法2:使用imagecolorallocatealpha() 函数
imagecolorallocatealpha() 函数的作用和 imagecolorallocate() 相同,但多了一个额外的设置透明度的参数 alpha,函数的语法格式如下:
imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha)
其中,$image 为要设置颜色的图像资源;$red,$green 和 $blue 分别是所需要的颜色的红,绿,蓝成分,取值范围是 0 到 255 的整数或者十六进制的 0x00 到 0xFF;$alpha 用来设置颜色的透明的,取值范围在 0 到 127 之间,0 表示完全不透明,127 则表示完全透明。
【示例】使用 imagecolorallocatealpha() 函数为图像设置颜色。
<?php $size=300; $image=imagecreatetruecolor($size,$size); //用白色背景加黑色边框画个方框 $back=imagecolorallocate($image,255,255,255); $border=imagecolorallocate($image,0,0,0); imagefilledrectangle($image,0,0,$size-1,$size-1,$back); imagerectangle($image,0,0,$size-1,$size-1,$border); $yellow_x=100; $yellow_y=75; $red_x=120; $red_y=165; $blue_x=187; $blue_y=125; $radius=150; //用alpha值分配一些颜色 $yellow=imagecolorallocatealpha($image,255,255,0,75); $red=imagecolorallocatealpha($image,255,0,0,75); $blue=imagecolorallocatealpha($image,0,0,255,75); //画3个交迭的圆 imagefilledellipse($image,$yellow_x,$yellow_y,$radius,$radius,$yellow); imagefilledellipse($image,$red_x,$red_y,$radius,$radius,$red); imagefilledellipse($image,$blue_x,$blue_y,$radius,$radius,$blue); //不要忘记输出正确的header! header('Content-type:image/png'); //最后输出结果 imagepng($image); imagedestroy($image); ?>
运行结果如下图所示:
推荐学习:《PHP视频教程》
以上是php怎么设置画布颜色的详细内容。更多信息请关注PHP中文网其他相关文章!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具