从数据库中的十六进制值创建 PHP 图像

标签 php mysql image gdlib

我有下面的代码可以从数据库中提取十六进制值并创建该颜色的图像。有超过一千个值,因此循环为它们创建一个图像。它似乎工作正常,除了它只是不断覆盖第一张图片 (0.jpg) 而不是创建新图片 0.jpg、1.jpg 2.jpg 等。知道我哪里出错了吗?

哦,是的,我也在其中将十六进制转换为 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++;

        }
    ?>

最佳答案

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

关于从数据库中的十六进制值创建 PHP 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1838794/

相关文章:

php - 相当于 php 中的 Curl -k 选项

php - 交响乐 2 : Upload file and save as blob

MySQL,检测排序索引上的行值更改

html - 不显示Base64/SVG HTML图像

css - 如何防止调整浏览器大小时内容跳起来?

html - 什么是 <img> 标签 css 属性来显示图像而不失真?

php - 网站上有现成的埃塞俄比亚日历吗?

php - AES-256-GCM 模式解密在 php 中失败

php - 简单的 PHP 登录表单不起作用

php - java.lang.String 无法转换为 JSONObject