PHP imagecolorat从相同的png和gif图像返回不同的值


PHP imagecolorat return different value from same png and gif image

我正在制作PHP脚本,它将从图像中读取所有像素的颜色,我从一些在线web服务中获得GIF格式,并将颜色值插入MySQL数据库。我创建了用于开发的测试图像,并注意到我从PNG和GIF图像中获得了不同的值。有人知道这是为什么吗?怎么解决这个问题?

图片:https://i.stack.imgur.com/MutB2.jpg

png的演示:http://tinyurl.com/dxxltzm

Demo for gif: http://tinyurl.com/c9a57xs

PNG代码:

<?php
include 'mysql.php';
$img = imagecreatefrompng("test1.png");
$hash = hash_file('md5', "test1.png");
$day = date("j");
$month = date("n");
$year = date("Y");
$hour = date("G");
$minute = date("i");
mysql_query("INSERT INTO pictures (year, month, day, hour, minute, hash) VALUES ('$year', '$month', '$year', '$hour', '$minute', '$hash')");
$result = mysql_query("SELECT * FROM pictures WHERE hash = '$hash' LIMIT 1");
while ($data = mysql_fetch_assoc($result)) {
    $id = $data['id'];
}
mysql_query("START TRANSACTION");
if($img) {
    $width = imagesx($img);
    $height = imagesy($img);
    $w = 1;
    $h = 1;
    while ($h < $height) {
        while ($w < $width) {
            $rgb = imagecolorat($img, $w, $h);
            $r = dechex(($rgb >> 16) & 0xFF);
            $g = dechex(($rgb >> 8) & 0xFF);
            $b = dechex($rgb & 0xFF);
            if (strlen($r) == 1) {
                $r = "0" . $r;
            }
            if (strlen($g) == 1) {
                $g = "0" . $g;
            }
            if (strlen($b) == 1) {
                $b = "0" . $b;
            }
            echo "<span style='color: #" . $r . $g . $b . "'>" . $r . $g . $b . "</span>_";
            mysql_query("INSERT INTO points (id_picture, x, y, value) VALUES ('$id', '$w', '$h', '$value')");
            $w++;
        }
        echo "<br />";
        $h++;
        $w = 1;
    }
mysql_query("COMMIT");
}
?>

GIF代码:

<?php
include 'mysql.php';
$img = imagecreatefromgif("test1.gif");
$hash = hash_file('md5', "test1.gif");
$day = date("j");
$month = date("n");
$year = date("Y");
$hour = date("G");
$minute = date("i");
mysql_query("INSERT INTO pictures (year, month, day, hour, minute, hash) VALUES ('$year', '$month', '$year', '$hour', '$minute', '$hash')");
$result = mysql_query("SELECT * FROM pictures WHERE hash = '$hash' LIMIT 1");
while ($data = mysql_fetch_assoc($result)) {
    $id = $data['id'];
}
mysql_query("START TRANSACTION");
if($img) {
    $width = imagesx($img);
    $height = imagesy($img);
    $w = 1;
    $h = 1;
    while ($h < $height) {
        while ($w < $width) {
            $rgb = imagecolorat($img, $w, $h);
            $r = dechex(($rgb >> 16) & 0xFF);
            $g = dechex(($rgb >> 8) & 0xFF);
            $b = dechex($rgb & 0xFF);
            if (strlen($r) == 1) {
                $r = "0" . $r;
            }
            if (strlen($g) == 1) {
                $g = "0" . $g;
            }
            if (strlen($b) == 1) {
                $b = "0" . $b;
            }
            echo "<span style='color: #" . $r . $g . $b . "'>" . $r . $g . $b . "</span>_";
            mysql_query("INSERT INTO points (id_picture, x, y, value) VALUES ('$id', '$w', '$h', '$value')");
            $w++;
        }
        echo "<br />";
        $h++;
        $w = 1;
    }
mysql_query("COMMIT");
}

?>

修复。

代替:

$rgb = imagecolorat($img, $w, $h);
$r = dechex(($rgb >> 16) & 0xFF);
$g = dechex(($rgb >> 8) & 0xFF);
$b = dechex($rgb & 0xFF);

我用的是:

$pixelrgb = imagecolorat($img,$w,$h);
$cols = imagecolorsforindex($img, $pixelrgb);
$r = dechex($cols['red']);
$g = dechex($cols['green']);
$b = dechex($cols['blue']);

https://bugs.php.net/bug.php?id=40801&编辑= 3