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

时间:2013-03-07 19:16:53

标签: php image-processing png gif

我正在编写PHP脚本,它将从GIF格式的某些在线Web服务中获取的图像中读取所有像素的颜色,并将颜色值插入MySQL数据库。我创建了用于开发的测试图像,并注意到我从PNG和GIF图像中获得了不同的值。有谁知道为什么会这样,以及如何解决这个问题?

图片:http://imgur.com/B79LHdx

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

演示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");
}

&GT;

1 个答案:

答案 0 :(得分:2)

固定。

而不是:

$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&edit=3