从数据库中的十六进制值创建PHP映像 [英] PHP image creation from hex values in database

查看:116
本文介绍了从数据库中的十六进制值创建PHP映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码从数据库中提取十六进制值并创建该颜色的图像。有超过一千个值,所以它循环为它们创建一个图像。它似乎工作得很好,除了它只是覆盖第一个图像(0.jpg)而不是创建新的0.jpg,1.jpg 2.jpg等任何想法,我出错了?

I've got the code below to pull hex values from a database and create an image of that colour. There's over a thousand values, so it's looping to create an image for them all. It seems to work fine except it just keeps overwriting the first image (0.jpg) instead of creating new ones 0.jpg, 1.jpg 2.jpg etc. Any idea where I'm going wrong?

哦是的,我正在将十六进制转换为rgb,这样可以正常工作。

Oh yeah, I'm converting the hex to rgb in there too, that works fine.

<?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++;

        }
    ?>


推荐答案

$ x = 0; 在while循环的每次迭代中执行。您需要在循环前面移动初始化。

$x = 0; is executed in each iteration of the while loop. You need to move the initialization in front the loop.

这篇关于从数据库中的十六进制值创建PHP映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆