简体   繁体   English

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

[英]PHP imagecolorat return different value from same png and gif image

I'm making PHP script which would read color of all pixels from the image I get from some online web service in GIF format and insert color values to MySQL database. 我正在制作PHP脚本,该脚本将从GIF格式的某些在线Web服务获取的图像中读取所有像素的颜色,并将颜色值插入MySQL数据库。 I created testing image for developing and noticed that I get different values from PNG and GIF image. 我创建了用于开发的测试图像,并注意到从PNG和GIF图像获得了不同的值。 Does anyone know why is that and how to solve this problem? 有谁知道这是为什么,以及如何解决这个问题?

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

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

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

PNG code: 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 code: 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");
}

?> ?>

Fixed. 固定。

Instead of: 代替:

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

I used this: 我用这个:

$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 https://bugs.php.net/bug.php?id=40801&edit=3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM