【问题标题】:PHP imagecolorat return different value from same png and gif imagePHP imagecolorat 从相同的 png 和 gif 图像返回不同的值
【发布时间】:2013-02-23 03:51:25
【问题描述】:

我正在制作 PHP 脚本,该脚本将从我从某些在线 Web 服务获得的 GIF 格式图像中读取所有像素的颜色,并将颜色值插入 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");
}

?>

【问题讨论】:

    标签: php image-processing png gif


    【解决方案1】:

    固定。

    代替:

    $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

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2019-04-26
      • 2016-02-08
      相关资源
      最近更新 更多