【问题标题】:PHP get color at last pixels of image errorPHP在图像错误的最后一个像素处获取颜色
【发布时间】:2016-11-15 09:23:04
【问题描述】:

我编写了这段代码来获取图像的第一个像素和最后一个像素的十六进制颜色。 第一个像素的代码有效,我得到了十六进制代码。 但是对于最后一个像素,我有一个错误:

PHP Notice:  imagecolorat(): 1,1024 is out of bounds in /var/playground/imghex.php on line 55

这是我的代码:

$gradientHeight = getimagesize($res["gradient"]);
// get Positions
$im  = imagecreatefrompng($res["gradient"]);
$rgb = imagecolorat($im, 0, 0);
$r   = ($rgb >> 16) & 0xFF;
$g   = ($rgb >> 8) & 0xFF;
$b   = $rgb & 0xFF;
// store
$res["Gradient1"] = rgb2hex([$r, $g, $b]);
// get positions
print_r($gradientHeight);
$rgb2 = imagecolorat($im, $gradientHeight[0], $gradientHeight[1]);
$r2   = ($rgb2 >> 16) & 0xFF;
$g2   = ($rgb2 >> 8) & 0xFF;
$b2   = $rgb2 & 0xFF;
// store
$res["Gradient2"] = rgb2hex([$r2, $g2, $b2]);
// print
print_r($res);

怎么了?我没有看到任何错误

【问题讨论】:

  • @FirstOne 你的意思是$rgb2 = imagecolorat($im, $gradientHeight[0] - 1, $gradientHeight[1]); 吗?因为PHP Notice: imagecolorat(): 0,1024 is out of bounds
  • 已解决,谢谢:) 请给出答案,以便我为您投票并选择您的有效答案

标签: php hex gd getimagesize


【解决方案1】:

您看到该通知是因为您在基于 0 的 索引上使用 size。如果您的大小为1024,则您的职位从01023

这样,您需要从中减去1。替换

$rgb2 = imagecolorat($im, $gradientHeight[0], $gradientHeight[1]);

$rgb2 = imagecolorat($im, $gradientHeight[0] - 1, $gradientHeight[1] - 1);

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 2015-01-19
    • 1970-01-01
    • 2013-07-21
    • 2019-07-27
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多