使用PHP动态图像中的数据库选择数据

时间:2013-09-25 12:21:26

标签: php image dynamic

我一直在研究动态图像脚本。我搜索它的主要原因是我想从数据库中显示用户信息。这是我的问题:

<?php
    header("Content-type:image/png");
    $array=array("I am a monument to all your sins", "Currently making pizza","Best before 12/7/09", "Farming Onions");
            function imagettftext_cr(&$im, $size, $angle, $x, $y, $color, $fontfile, $text)
            {
                // retrieve boundingbox
                $bbox = imagettfbbox($size, $angle, $fontfile, $text);
                // calculate deviation
                $dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0;         // deviation left-right
                $dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0;        // deviation top-bottom
                // new pivotpoint
                $px = $x-$dx;
                $py = $y-$dy;
                return imagettftext($im, $size, $angle, $px, $y, $color, $fontfile, $text);
            }

    $image = imagecreate(500,90);
    $black = imagecolorallocate($image,0,0,0);
    $grey_shade = imagecolorallocate($image,40,40,40);
    $white = imagecolorallocate($image,255,255,255);


    $text = $array[rand(0,sizeof($array)-1)];

    $otherFont = 'open.ttf';
    $font = 'open.ttf';

    $name = "erlis";
    $name = substr($name, 0, 25);    


    //BG text for Name
    while($i<10){
    imagettftext_cr($image,rand(2,40),rand(0,50),rand(10,500),rand(0,200),$grey_shade,$font,$name);
    $i++;
    }
    //BG text for saying
    while($i<10){
    imagettftext_cr($image,rand(0,40),rand(90,180),rand(100,500),rand(200,500),$grey_shade,$otherFont,$text);
    $i++;
    }
    // Main Text
    imagettftext_cr($image,35,0,250,46,$white,$font,$name);
    imagettftext_cr($image,10,0,250,76,$white,$otherFont,$text);
    imagepng($image);

    ?>

这部分与简单文本完美配合,没有mysql查询。但是当我尝试启动一个mysql查询时,我说$query = mysql_query("SELECT * FROM serverplayers WHERE id=1");它只是打破了图像。我使用的是什么样的形式并不重要但它只是制动我的图像。我怎样才能实现一些“取自数据库“代码?也许$_GET['']或类似的东西。

我需要将$name="erlis";更改为用户信息。

1 个答案:

答案 0 :(得分:0)

这似乎是如何调试那个野兽的问题。

  1. 为自己构建一个打开图像的URL,并在任何浏览器中对其进行测试。像

    这样的东西

    http:// yourserver / images?id = 1(请根据您的需求调整 - 只是一个例子)

  2. 将mysql序列添加到代码

  3. 取消注释您的mimetype标头修改,如下所示:

    // header(“Content-type:image / png”);

  4. 运行浏览器中步骤1的链接 - &gt; php / mysql调试可能会提示您输入错误

  5. 如果您在浏览器上看不到任何内容,请尝试从浏览器菜单中查看“查看源代码”
相关问题