首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在PHP上传递十六进制颜色面临的问题

在PHP上传递十六进制颜色面临的问题
EN

Stack Overflow用户
提问于 2017-05-10 17:31:38
回答 1查看 278关注 0票数 2

我试图在PHP中创建一个图像,但是在imagecolorallocate()函数中使用十六进制颜色而不是RGB,但是我得到了以下错误消息:

警告:Imagecolor配()需要精确的4个参数,其中2个给定

你能告诉我怎么解决这个问题吗?

代码语言:javascript
运行
复制
<?php
$dir = "maps/";

function hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }
   $rgb = array($r, $g, $b);
   //return implode(",", $rgb);
   return $rgb; 
}
$rgb = hex2rgb("#cc0");
$im = imagecreatetruecolor(400, 400);
$red = imagecolorallocate($im, $rgb);
imagefill($im, 0, 0, $red);
// Save the image as 'simpletext.jpg'
imagejpeg($im, $dir.'image_demo.jpg');

imagedestroy($im);
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-10 17:40:29

图像颜色分配需要四个参数:图像Red值、Green值和Blue值。您只传递一个表示这些颜色的图像和数组。

尝试:

代码语言:javascript
运行
复制
  imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);

阅读有关图像颜色分配的更多信息

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43899210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档