0

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!

Pinkernox
  • 23
  • 3
  • In your second example, you are using the female image for both cases. – Michael - sqlbot Nov 14 '17 at 03:14
  • Still doesn't solve the problem that it just...doesn't show up in the browser. – Pinkernox Nov 14 '17 at 04:01
  • I understand that, but your test case is not valid. Verify that static values of both 1 and 0 produce female and male avatars as expected. Don't skip over a troubleshooting step just because you don't see how it's related. – Michael - sqlbot Nov 14 '17 at 04:51
  • You should not render images with PHP if not absolutely necessary. Why not use a direct link to the image? You could even do that in pure SQL, although you could also just use an if statement in PHP to link to the appropriate image. `SELECT CONCAT('path/to/folder/', IF(gender = 1, 'male.png', 'female.png'))` – twicejr Nov 14 '17 at 09:27
  • @Michael, I tested that already . They both produce the male and female avatars as intended. – Pinkernox Nov 14 '17 at 16:55
  • @twicejr I can't use a direct link for the purpose that the avatar will be customizable. The avatar will be generated with g the several clothing, hairtsyles, etc. Options. Those are all seperate pictures layered over each other, thus the imagecopy() function. There are infinite numbers of possibilities so direct links are inneficient – Pinkernox Nov 14 '17 at 16:57
  • Personally, I'd go with a HTML/CSS solution, which would also display the same result albeit requiring more requests. Use position: absolute and z-index to position / "render" your avatars. This is much less resource-intensive. – twicejr Nov 27 '17 at 09:33

1 Answers1

0

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.

terdia07
  • 493
  • 6
  • 10