知识学习

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 3829|回复: 0

PHP 分配图像颜色 imagecolorallocate 与 imagecolordeallocate 函数

[复制链接]

升级   100%

152

主题

0

回帖

752

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
752
发表于 2020-9-15 15:52:42 | 显示全部楼层 |阅读模式

imagecolorallocate()
imagecolorallocate() 函数用于为图像分配颜色,返回一个标识符,代表了由给定的 RGB 成分组成的颜色,如果分配失败则返回 -1 。
语法:
  1. imagecolorallocate( resource image, int red, int green, int blue )
复制代码
参数 red,green 和 blue 分别是所需要的颜色的 红,绿,蓝 成分,取值范围 0 - 255。
例子:
  1. <?php
  2. header("Content-type: image/png");
  3. //创建图像
  4. $im = @imagecreate(200, 50) or die("创建图像资源失败");
  5. //图片背景颜色并填充
  6. $bg = imagecolorallocate($im, 204, 204, 204);
  7. //设定文字颜色
  8. $red = imagecolorallocate($im, 255, 0, 0);
  9. //水平画一行字
  10. imagestring($im, 5, 0, 0, "Hello world!", $red);
  11. //以PNG格式输出图像
  12. imagepng($im);
  13. //销毁图像资源
  14. imagedestroy($im);
  15. ?>
复制代码
提示
  • 对于用 imagecreate() 建立的图像,第一次调用 imagecolorallocate() 会给图像填充背景色(如上面例子)。
  • 对于用 imagecreatetruecolor() 建立的图像,则需要使用别的指令如 imagefill() 填充背景。

imagecolorallocatealpha()
imagecolorallocatealpha() 和 imagecolorallocate() 用法相同,但多了一个额外的透明度参数 alpha,其值从 0 到 127。0 表示完全不透明,127 表示完全透明。
语法:
  1. int imagecolorallocatealpha( resource image, int red, int green, int blue, int alpha )
复制代码
imagecolordeallocate()
imagecolordeallocate() 函数用于取消先前由 imagecolorallocate() 和imagecolorallocatealpha() 函数为图像分配的颜色。
语法:
  1. bool imagecolordeallocate( resource image, int color )
复制代码
例子:
  1. <?
  2. $im = @imagecreate(200, 50) or die("创建图像资源失败");
  3. $bg = imagecolorallocate($im, 255, 0, 0);
  4. imagecolordeallocate($im, $bg);
  5. ?>
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|知识学习 ( 鲁ICP备20015483号 )

GMT+8, 2026-5-13 21:46 , Processed in 0.065339 second(s), 24 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表