GD库图像生成不适用于mySQL查询 [英] GD Library image generation does not work with mySQL query

查看:57
本文介绍了GD库图像生成不适用于mySQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说,我有一个项目,要求根据用户从数据库中的数据创建化身.化身使用imagepng()和imagecopy()函数生成.

Long story short, I have a project that requires creating a user's avatar based on their data from the database. The avatar is generated using the imagepng() and imagecopy() functions.

用户的化身可以是男性或女性,并且该首选项存储在SQL数据库中的"user_gender"列中,其中"0" =女性,"1" =男性:

The user's avatar can either be male or female and that preference is saved in an SQL database as column "user_gender" where "0" = female and "1" = male:

phpmyadmin中表的屏幕截图

因此,想法是我们从数据库中获取数据,将值(0或1)分配给变量,然后使用该变量生成图像.参见下面的代码:

So the idea is that we take the data from the database, assign the value (0 or 1) to a variable, then use that variable to generate the image. See code below:

<?php

    //Database connection script not included, but works fine

    $id = 1;

        $sqlQuery = "SELECT * FROM table WHERE id = :id";
        $statement = $db->prepare($sqlQuery);
        $statement->execute(array(':id' => $id));

        while($rs = $statement->fetch())
        {
            $gender = $rs['user_gender'];

        }

    if($gender == "0")
    {
        //Allocation of images, file paths
        $bodytype ="images/female/f_body.png";
    }   
    else
    {
        $bodytype ="images/male/f_body.png";
    }

    header('Content-Type: image/png');

    $destination = imagecreatefrompng($bodytype);

    imagealphablending($destination, true);
    imagesavealpha($destination, true);

    imagepng($destination); 
?>

但是,此代码不起作用,因为它会导致浏览器上出现空白的黑页.

This code however, does not work as it results in a blank black page on the browser.

但是,此代码无需从数据库中进行任何提取,就可以很好地工作:

HOWEVER, this code, without any pulling from the database, works perfectly fine:

<?php

//taking out the sql query and just creating a $gender variable for testing

$gender = "0";

if($gender === 0)
{
    $bodytype ="images/female/f_body.png";
}   
else
{
    $bodytype ="images/female/f_body.png";
}

header('Content-Type: image/png');

$destination = imagecreatefrompng($bodytype);

imagealphablending($destination, true);
imagesavealpha($destination, true);

imagepng($destination);
?>

这是带有第二个代码的输出,表明图像生成确实起作用,并且问题很可能是从sql传递到php:

This is the output with the second code, showing that the image generation is indeed functional and the problem is most likely the passing from sql to php:

在浏览器中生成图像

我将非常感谢您知道我在做错什么,或者被提示如果从数据库中提取变量,为什么代码会停止工作.

I'd be extremely grateful to know what I am doing wrong or being hinted as to why the code stops working if the variable is pulled from the database.

谢谢!

推荐答案

我尝试了您的代码,并遇到了相同的问题,所以我进行了一些挖掘,发现数据库没有返回任何内容,因此我在数据库名称前添加了前缀以及表名,它的工作原理.参见下面的代码

I tried your code and encountered the same problem so I did some digging it and found that nothing was returned from the database so what I did was prefix the database name along with the tablename and it worked. See code below

$gender = '';

$sqlQuery = "SELECT * FROM register.users WHERE id = :id";
$statement = $db->prepare($sqlQuery);
$statement->execute(array('id' => 1));

while($rs = $statement->fetch())
{
    $gender = $rs['gender'];
}

if($gender == 0)
{
    $bodytype ="images/female/f_body.png";
}
else if($gender == 1)
{
    $bodytype ="images/male/m_body.png";
}

$destination = imagecreatefrompng($bodytype);

imagealphablending($destination, true);
imagesavealpha($destination, true);

header('Content-Type: image/png');
imagepng($destination);

尝试一下,让我知道它的进展.

Try it and let me know how it goes.

这篇关于GD库图像生成不适用于mySQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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