[PHP] GD库(七):分配、取消图像颜色 imagecolorallocate 与 imagecolordeallocate 函数
2017-07-30 01:25
555 查看
[PHP] GD库(七):分配、取消图像颜色 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 分配、取消图像颜色 imagecolorallocate 与 imagecolordeallocate 函数
- 6. PHP 分配、取消图像颜色 imagecolorallocate 与 imagecolordeallocate 函数
- PHP图像处理之使用imagecolorallocate()函数设置颜色例子
- PHP图像处理之使用imagecolorallocate()函数设置颜色例子
- php图像函数 imagecreatetruecolor()和imagecreate()的区别
- [PHP] GD库(五):载入图像 imagecreatefrom_gif_jpeg_png 系列函数
- php图像函数 imagecreatetruecolor()和imagecreate()的区别
- php imagecreate调用imagecolorallocate无法改变颜色
- php图像函数 imagecreatetruecolor()和imagecreate()的区别
- [PHP] GD库(九)合并图像 imagecopymerge 函数(水印制作实例)
- [PHP] GD库(十一)图像填充 imagefill、imagefilledarc 与 imagefilledrectangle() 函数
- [PHP] GD库(三):获取图像信息 getimagesize 函数
- [PHP] GD库(四):创建图像、销毁图像 imagecreate 与 imagedestroy 函数
- php有现成的函数getimagesize用于获取图像的尺寸
- SWT 的字体(Font)、颜色(Color)、图像(Image)和绘图
- [PHP] GD库(六):输出图像 imagegif 、imagejpeg 与 imagepng 函数
- PHP扩展之图像处理1——GD库使用及相关函数
- PHP载入图像imagecreatefrom_gif_jpeg_png系列函数用法分析
- PHP GD库生成图像的几个函数总结
- 2. PHP 获取图像信息 getimagesize 函数