2009-12-03 43 views
0

我有下面的代碼從數據庫中提取十六進制值並創建該顏色的圖像。有超過一千個值,所以它是循環爲他們創建一個圖像。它似乎工作得很好,除了它只是覆蓋第一個圖像(0.jpg)而不是創建新的0.jpg,1.jpg 2.jpg等任何想法,我哪裏出錯了?從數據庫中的十六進制值創建PHP圖像

哦,是的,我把那裏的十六進制轉換爲rgb,工作正常。

<?php 

    require ('connect.php'); 

    $sql = mysql_query("SELECT * FROM hex") 
    or die(mysql_error()); 

    while($colors = mysql_fetch_array($sql)) 
     { 

     $x = 0; 

     $imgname = $x.".jpg"; 

     $color = $colors['value']; 

      if (strlen($color) == 6) 
       list($r, $g, $b) = array($color[0].$color[1], 
             $color[2].$color[3], 
             $color[4].$color[5]); 

      $r = hexdec($r); $g = hexdec($g); $b = hexdec($b); 

     header("Content-type: image/jpeg"); 
     $image = imagecreate(720, 576); 
     imagecolorallocate($image,$r, $g, $b); 
     imagejpeg($image, $imgname); 
     imagedestroy($image); 

     $x++; 

     } 
    ?> 
+0

媽的,這是真的很明顯,嘿,感謝您的幫助,現在完美的作品! – 2009-12-03 09:45:03

回答

3

$x = 0;在while循環的每次迭代中執行。您需要在循環前移動初始化。

3

您只需將$x = 0;移動到循環開始之前。

似乎有是一些其他的東西錯了,太

$x = 0; 

while($colors = mysql_fetch_array($sql)) 
{ 
    $imgname = $x.".jpg"; 

    $color = $colors['value']; 

    // Skip the whole lot if the colour is invalid 
    if (strlen($color) != 6) 
     continue; 

    // No need to create an array just to call list() 
    $r = hexdec($color[0].$color[1]); 
    $g = hexdec($color[2].$color[3]); 
    $b = hexdec($color[4].$color[5]); 

    // There's no need to header() if you're writing to a file 
    //header("Content-type: image/jpeg"); 
    $image = imagecreate(720, 576); 
    $colour = imagecolorallocate($image, $r, $g, $b); 

    // You don't actually fill the image with the colour 
    imagefilledrectangle($image, 0, 0, 719, 575, $colour); 

    imagejpeg($image, $imgname); 
    imagedestroy($image); 

    $x++; 
} 
+0

謝謝你,我還在學習和拼湊一些東西。我會把這些指針帶到船上...... – 2009-12-03 09:48:58