phpmysqlsqlgdgdlib

GD Library image generation does not work with mySQL query


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.

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:

Screenshot of table in phpmyadmin

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

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:

Working image generation in browser

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.

Thank you!


Solution

  • 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.