2013-03-07 2 views
1

일부 온라인 웹 서비스에서 GIF 형식으로 가져온 이미지에서 모든 픽셀의 색상을 읽고, MySQL 데이터베이스에 색상 값을 삽입하는 PHP 스크립트를 만들고 있습니다. 개발을위한 테스트 이미지를 만들었고 PNG 및 GIF 이미지와 다른 값을 얻는 것으로 나타났습니다. 아무도 왜 그런지, 어떻게이 문제를 해결할 수 있는지 알고 있습니까?PHP imagecolorat가 동일한 png 및 gif 이미지에서 다른 값을 반환합니다.

이미지 : GIF에 대한 http://tinyurl.com/dxxltzm

데모 : 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"); 
} 
,691 PNG에 대한 http://imgur.com/B79LHdx

데모

?>

답변

2

수정 됨.

대신에 :

$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

:

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

나는 이것을 사용

관련 문제