PHP在没有图像资源的情况下分配颜色

发布于 2024-09-18 00:31:59 字数 729 浏览 6 评论 0 原文

您可以在 PHP GD 中分配颜色吗? 图像资源?这应该是可能的,因为分配的颜色实际上是一个数字,对吗?

$im = imagecreatetruecolor(100, 100);
$col = imagecolorallocate($im, 255, 0, 0);
print $col."<br/>";
$col2 = imagecolorallocate($im, 255, 0, 0);
print $col2."<br/>";
$im2 = imagecreatetruecolor(600, 100);
$col3 = imagecolorallocate($im, 255, 0, 0);
print $col3;

打印出:

16711680

16711680

16711680

我猜真正的问题是 255、0 和 0 是如何变成 16711680 的。

Can you allocate a color in PHP GD without an image resource? It should be possible because really an allocated color is a number, right?

$im = imagecreatetruecolor(100, 100);
$col = imagecolorallocate($im, 255, 0, 0);
print $col."<br/>";
$col2 = imagecolorallocate($im, 255, 0, 0);
print $col2."<br/>";
$im2 = imagecreatetruecolor(600, 100);
$col3 = imagecolorallocate($im, 255, 0, 0);
print $col3;

This prints out:

16711680

16711680

16711680

I guess what the real question is how 255, 0, and 0 are made into 16711680.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒儿 2024-09-25 00:32:00

16711680(十进制)为 0x00FF0000(十六进制)

00 - Alpha 值(十进制 0)

FF - 红色(十进制 255)

00 - 绿色(十进制 0)

00 - 蓝色(十进制 0)

请参阅 http://www.php.net/manual/en/function.imagecolorallocatealpha.php 设置 alpha 字节

编辑:

另外,要回答您的第一个问题 - ,您可以在没有图像资源的情况下创建颜色(因此无需调用 imagecolorallocate):

$col1 = 0x00FF0000; // 红色

$col2 = 0x0000FF00; // 绿色

// 等...

16711680 (decimal) is 0x00FF0000 (hexadecimal)

00 - Alpha value (0 dec)

FF - Red (255 dec)

00 - Green (0 dec)

00 - Blue (0 dec)

See http://www.php.net/manual/en/function.imagecolorallocatealpha.php to set the alpha byte

Edit:

Also, to answer your first question -- yes, you can create a color without an image resource (and, consequently without a call to imagecolorallocate):

$col1 = 0x00FF0000; // Red

$col2 = 0x0000FF00; // Green

// etc...

后来的我们 2024-09-25 00:32:00

这应该是可能的,因为分配的颜色实际上是一个数字,对吗?

不,不是。 GD 可能还必须在图像的调色板中注册该颜色(想想非真彩色图像)。

所以你需要一个图像资源。

It should be possible because really an allocated color is a number, right?

No, it's not. GD may also have to register that color in the palette of the image (think non true color images).

So you need an image resource.

难如初 2024-09-25 00:32:00

颜色索引或颜色 ID 可以通过多种方式确定。这应该有助于发现之路。

更新***
我不知道为什么,但在看到这个问题后,我编写了以下函数将 类型的颜色值转换为 类型的颜色表达式。希望它对某人有用。 *添加 CMYK 只是因为。 ...

选项-->
'整数',
'rgba', 'rgb', 'cmyk',
'hex', 'rgbaCSS', 'rgbCSS', 'hexCSS', 'hexCSS4'

如果有人想变得非常认真,他们可以编写可选参数来将格式转换为任何格式;使用 sprintf。

<?php
function convertColorValue($colorValue, $toType = 'hex', $recurse = 0) {
    if ($recurse > 2) { return $colorValue; }
    if (!is_array($colorValue)) {
        // OUT --> {Mixed} :: Options --> rgba || rgb || cmyk
        // If not an array then we have some form of Number - Valid values int || hex<string>
        // Please note that hex numbers such as 0x000000 that are not strings '0x000000' are interpreted by php as their number representation.
        // Shortcode hex values.
        if (is_string($colorValue) && $colorValue{0} === '#') {
            $start = 0;
            if (strlen($colorValue) === 4) {
                $rgb['alpha'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
            } else {
                $rgb['alpha'] = 0;
            }
            $rgb['r'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
            $rgb['g'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
            $rgb['b'] = hexdec(str_repeat(substr($colorValue, $start, 1), 2));
            // We need to make sure this follows some rules before we send it back even if it is the type we are being asked for.
            return convertColorValue($rgb, $toType);
        }
        // Make sure we have a clean sensible string for conversion.
        if (preg_match("/[^0-9a-f]/i", $colorValue) !== false) {
            if (($colorValue{0} === 0 && $colorValue{1} !== 'x'))
                $colorValue = '0x' . $colorValue;
        }
        $colorValue = preg_replace("/[^0-9a-fx]/i", '', $colorValue); // 4294967295 === 0xFFFFFFFF, the maximum color value.
        $colorValue = strlen((string) $colorValue) >= 10 ? (intval(substr($colorValue, 0, 10), 0) > 4294967295 ? 0 : substr($colorValue, 0, 10)) : $colorValue;
        // CONVERT our number to base 10 from whatever base it is in.
        // **** TODO: Hopefully int or hex, not sure what happens in other bases. ¯\_(ツ)_/¯
        // Hex strings should always be prepended with 0x, otherwise confusion happens 11110000 !=? 0x11110000
        // If not prepended here with 0x, I can't fix/predict which you are trying to represent int or hex. Int is naturally assumed.
        $color = [];
        $colorValue = intval(intval($colorValue, 0), 10);
        $color['r'] = ($colorValue >> 16) & 0xFF;
        $color['g'] = ($colorValue >> 8) & 0xFF;
        $color['b'] = $colorValue & 0xFF;
        $color['alpha'] = ($colorValue >> 24) & 0x7F;
        if ($toType === 'cmyk') {
            $c = (255 - $color['r']) / 255.0 * 100;
            $m = (255 - $color['g']) / 255.0 * 100;
            $y = (255 - $color['b']) / 255.0 * 100;
            $b = min([$c,$m,$y]);
            $c = $c - $b; $m = $m - $b; $y = $y - $b;
            return ['c' => $c, 'm' => $m, 'y' => $y, 'k' => $b, 'alpha' => $color['alpha']];
        }
        if ($toType === 'rgba' || $toType === 'rgb') {
            return $color;
        }
        return convertColorValue($color, $toType, ++$recurse);
    }
    // OUT --> {Mixed} :: Options --> int || hex || hexCSS || rgbCSS || rgbaCSS || hexCSS4
    // If they've thrown us a c, we are going to assume they gave us CMYK.
    if (isset($colorValue['c'])) {
        $colorValue['c'] = empty($colorValue['c']) ? 0 : $colorValue['c'] / 100;
        $colorValue['m'] = empty($colorValue['m']) ? 0 : $colorValue['m'] / 100;
        $colorValue['y'] = empty($colorValue['y']) ? 0 : $colorValue['y'] / 100;
        $colorValue['k'] = empty($colorValue['k']) ? 0 : $colorValue['k'] / 100;
        $colorValue['r'] = round((1 - ($colorValue['c'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
        $colorValue['g'] = round((1 - ($colorValue['m'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
        $colorValue['b'] = round((1 - ($colorValue['y'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);

    } else {
        $colorValue['r'] = empty($colorValue['r']) ? 0 : $colorValue['r'];
        $colorValue['g'] = empty($colorValue['g']) ? 0 : $colorValue['g'];
        $colorValue['b'] = empty($colorValue['b']) ? 0 : $colorValue['b'];
    }
    $colorValue['alpha'] = (empty($colorValue['alpha']) || $colorValue['alpha'] > 0x7f) ? 0 : $colorValue['alpha'];
    if ($toType === 'rgbaCSS') {
        $color['alpha'] = empty($color['alpha']) ? 1 :
            (round(1 * $color['alpha'] / 127, 3, PHP_ROUND_HALF_DOWN));
        return "rgba({$colorValue['r']}, {$colorValue['g']}, {$colorValue['b']}, {$colorValue['alpha']})";
    }
    if ($toType === 'rgbCSS') {
        return "rgb({$colorValue['r']}, {$colorValue['g']}, {$colorValue['b']})";
    }
    // Just return the rgb value if opaque since '==' color-values.
    if (empty($colorValue['alpha'])) {
        $hex = sprintf("%02x%02x%02x", $colorValue['r'], $colorValue['g'], $colorValue['b']);
    } else {
        $hex = sprintf("%02x%02x%02x%02x", $colorValue['alpha'], $colorValue['r'], $colorValue['g'], $colorValue['b']);
    }
    $isCSS = ($toType === 'hexCSS4' || $toType === 'hexCSS');
    if ($toType === 'hex' || $isCSS) {
        return $isCSS ? ("#".$hex) : ("0x".$hex);
    }
    $hex = "0x".$hex;
    if ($toType === 'int') { return hexdec($hex); }
    // Is now a hex string. Going back UP^
    return convertColorValue($hex, $toType, ++$recurse);
}

The color index, or color id can be determined several ways. This should help on the road to discovery.

Update ***
I'm not sure why, but after seeing this question I wrote the following function to convert <many> types of color values to <many> types of color expressions. Hope it works for someone. *Added CMYK just because. ...

Options -->
'int',
'rgba', 'rgb', 'cmyk',
'hex', 'rgbaCSS', 'rgbCSS', 'hexCSS', 'hexCSS4'

If someone wanted to get really serious, they could write optional arguments for converting the format to any; using sprintf.

<?php
function convertColorValue($colorValue, $toType = 'hex', $recurse = 0) {
    if ($recurse > 2) { return $colorValue; }
    if (!is_array($colorValue)) {
        // OUT --> {Mixed} :: Options --> rgba || rgb || cmyk
        // If not an array then we have some form of Number - Valid values int || hex<string>
        // Please note that hex numbers such as 0x000000 that are not strings '0x000000' are interpreted by php as their number representation.
        // Shortcode hex values.
        if (is_string($colorValue) && $colorValue{0} === '#') {
            $start = 0;
            if (strlen($colorValue) === 4) {
                $rgb['alpha'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
            } else {
                $rgb['alpha'] = 0;
            }
            $rgb['r'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
            $rgb['g'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
            $rgb['b'] = hexdec(str_repeat(substr($colorValue, $start, 1), 2));
            // We need to make sure this follows some rules before we send it back even if it is the type we are being asked for.
            return convertColorValue($rgb, $toType);
        }
        // Make sure we have a clean sensible string for conversion.
        if (preg_match("/[^0-9a-f]/i", $colorValue) !== false) {
            if (($colorValue{0} === 0 && $colorValue{1} !== 'x'))
                $colorValue = '0x' . $colorValue;
        }
        $colorValue = preg_replace("/[^0-9a-fx]/i", '', $colorValue); // 4294967295 === 0xFFFFFFFF, the maximum color value.
        $colorValue = strlen((string) $colorValue) >= 10 ? (intval(substr($colorValue, 0, 10), 0) > 4294967295 ? 0 : substr($colorValue, 0, 10)) : $colorValue;
        // CONVERT our number to base 10 from whatever base it is in.
        // **** TODO: Hopefully int or hex, not sure what happens in other bases. ¯\_(ツ)_/¯
        // Hex strings should always be prepended with 0x, otherwise confusion happens 11110000 !=? 0x11110000
        // If not prepended here with 0x, I can't fix/predict which you are trying to represent int or hex. Int is naturally assumed.
        $color = [];
        $colorValue = intval(intval($colorValue, 0), 10);
        $color['r'] = ($colorValue >> 16) & 0xFF;
        $color['g'] = ($colorValue >> 8) & 0xFF;
        $color['b'] = $colorValue & 0xFF;
        $color['alpha'] = ($colorValue >> 24) & 0x7F;
        if ($toType === 'cmyk') {
            $c = (255 - $color['r']) / 255.0 * 100;
            $m = (255 - $color['g']) / 255.0 * 100;
            $y = (255 - $color['b']) / 255.0 * 100;
            $b = min([$c,$m,$y]);
            $c = $c - $b; $m = $m - $b; $y = $y - $b;
            return ['c' => $c, 'm' => $m, 'y' => $y, 'k' => $b, 'alpha' => $color['alpha']];
        }
        if ($toType === 'rgba' || $toType === 'rgb') {
            return $color;
        }
        return convertColorValue($color, $toType, ++$recurse);
    }
    // OUT --> {Mixed} :: Options --> int || hex || hexCSS || rgbCSS || rgbaCSS || hexCSS4
    // If they've thrown us a c, we are going to assume they gave us CMYK.
    if (isset($colorValue['c'])) {
        $colorValue['c'] = empty($colorValue['c']) ? 0 : $colorValue['c'] / 100;
        $colorValue['m'] = empty($colorValue['m']) ? 0 : $colorValue['m'] / 100;
        $colorValue['y'] = empty($colorValue['y']) ? 0 : $colorValue['y'] / 100;
        $colorValue['k'] = empty($colorValue['k']) ? 0 : $colorValue['k'] / 100;
        $colorValue['r'] = round((1 - ($colorValue['c'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
        $colorValue['g'] = round((1 - ($colorValue['m'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
        $colorValue['b'] = round((1 - ($colorValue['y'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);

    } else {
        $colorValue['r'] = empty($colorValue['r']) ? 0 : $colorValue['r'];
        $colorValue['g'] = empty($colorValue['g']) ? 0 : $colorValue['g'];
        $colorValue['b'] = empty($colorValue['b']) ? 0 : $colorValue['b'];
    }
    $colorValue['alpha'] = (empty($colorValue['alpha']) || $colorValue['alpha'] > 0x7f) ? 0 : $colorValue['alpha'];
    if ($toType === 'rgbaCSS') {
        $color['alpha'] = empty($color['alpha']) ? 1 :
            (round(1 * $color['alpha'] / 127, 3, PHP_ROUND_HALF_DOWN));
        return "rgba({$colorValue['r']}, {$colorValue['g']}, {$colorValue['b']}, {$colorValue['alpha']})";
    }
    if ($toType === 'rgbCSS') {
        return "rgb({$colorValue['r']}, {$colorValue['g']}, {$colorValue['b']})";
    }
    // Just return the rgb value if opaque since '==' color-values.
    if (empty($colorValue['alpha'])) {
        $hex = sprintf("%02x%02x%02x", $colorValue['r'], $colorValue['g'], $colorValue['b']);
    } else {
        $hex = sprintf("%02x%02x%02x%02x", $colorValue['alpha'], $colorValue['r'], $colorValue['g'], $colorValue['b']);
    }
    $isCSS = ($toType === 'hexCSS4' || $toType === 'hexCSS');
    if ($toType === 'hex' || $isCSS) {
        return $isCSS ? ("#".$hex) : ("0x".$hex);
    }
    $hex = "0x".$hex;
    if ($toType === 'int') { return hexdec($hex); }
    // Is now a hex string. Going back UP^
    return convertColorValue($hex, $toType, ++$recurse);
}
傲影 2024-09-25 00:32:00

使用此功能

function img_color($r, $g, $b, $a=0) {
  // prepare red color
  if (is_string($r)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $r)) return false;
    $r = hexdec($r);
  }
  elseif (is_int($r)) {if ($r<0 or $r>255) return false;}
  else return false;

  // prepare green color
  if (is_string($g)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $g)) return false;
    $g = hexdec($g);
  }
  elseif (is_int($g)) {if ($g<0 or $g>255) return false;}
  else return false;

  // prepare blue color
  if (is_string($b)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $b)) return false;
    $b = hexdec($b);
  }
  elseif (is_int($b)) {if ($b<0 or $b>255) return false;}
  else return false;

  // prepare alpha channel
  if (is_string($a)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $a)) return false;
    $a = hexdec($a);
  }
  elseif (!is_int($a)) return false;
  if ($a<0 or $a>127) return false;

  $result = unpack('I', chr($b) . chr($g) . chr($r) . chr($a));
  return $result[1];
}

Use this function

function img_color($r, $g, $b, $a=0) {
  // prepare red color
  if (is_string($r)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $r)) return false;
    $r = hexdec($r);
  }
  elseif (is_int($r)) {if ($r<0 or $r>255) return false;}
  else return false;

  // prepare green color
  if (is_string($g)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $g)) return false;
    $g = hexdec($g);
  }
  elseif (is_int($g)) {if ($g<0 or $g>255) return false;}
  else return false;

  // prepare blue color
  if (is_string($b)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $b)) return false;
    $b = hexdec($b);
  }
  elseif (is_int($b)) {if ($b<0 or $b>255) return false;}
  else return false;

  // prepare alpha channel
  if (is_string($a)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $a)) return false;
    $a = hexdec($a);
  }
  elseif (!is_int($a)) return false;
  if ($a<0 or $a>127) return false;

  $result = unpack('I', chr($b) . chr($g) . chr($r) . chr($a));
  return $result[1];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文