0

データベースから 16 進値を取得し、その色のイメージを作成するコードを以下に示します。千を超える値があるため、それらすべてのイメージを作成するためにループしています。新しい画像 0.jpg、1.jpg 2.jpg などを作成するのではなく、最初の画像 (0.jpg) を上書きし続けることを除けば、問題なく動作しているようです。

そうそう、私はそこでも16進数を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++;

        }
    ?>
4

2 に答える 2

3

$x = 0;while ループの各反復で実行されます。初期化をループの前に移動する必要があります。

于 2009-12-03T09:42:05.163 に答える
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++;
}
于 2009-12-03T09:42:07.943 に答える