我试图在PHP中创建一个图像,但是在imagecolorallocate()函数中使用十六进制颜色而不是RGB,但是我得到了以下错误消息:
警告:Imagecolor配()需要精确的4个参数,其中2个给定
你能告诉我怎么解决这个问题吗?
<?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);
?>发布于 2017-05-10 17:40:29
图像颜色分配需要四个参数:图像、Red值、Green值和Blue值。您只传递一个表示这些颜色的图像和数组。
尝试:
imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);阅读有关图像颜色分配的更多信息
https://stackoverflow.com/questions/43899210
复制相似问题