gpt4 book ai didi

PHP在图像错误的最后一个像素处获取颜色

转载 作者:行者123 更新时间:2023-12-04 14:29:20 25 4
gpt4 key购买 nike

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

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);

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

最佳答案

您看到该通知是因为您在0-based 索引上使用大小。如果您的尺寸为 1024,您的位置将从 01023

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

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

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

关于PHP在图像错误的最后一个像素处获取颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38340662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com