2017-11-14 6 views
0

간단히 말해서 데이터베이스의 데이터를 기반으로 사용자의 아바타를 만들어야하는 프로젝트가 있습니다. 아바타는 imagepng() 및 imagecopy() 함수를 사용하여 생성됩니다.GD 라이브러리 이미지 생성이 mySQL 쿼리와 작동하지 않습니다

사용자의 아바타는 남성 또는 여성이 될 수 있습니다 그리고 선호 여기서 "0"= 여성 "1"열 "user_gender"로 SQL 데이터베이스에 저장됩니다 = 남성 : 그래서

Screenshot of table in phpmyadmin

우리가 데이터베이스에서 데이터를 가져 와서 변수에 값 (0 또는 1)을 할당 한 다음 해당 변수를 사용하여 이미지를 생성하는 것이 아이디어입니다. 아래 코드를 참조하십시오

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

은 브라우저에 검은 색 빈 페이지에 결과로이 코드는하지만, 을 작동하지 않습니다.

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

이 이미지 생성이 실제로 작동하는지 보여, 두 번째 코드로 출력하고 문제는 가능성이 높습니다 :

그러나,이 코드는 모든 데이터베이스에서 당겨없이 완벽하게 잘 작동 PHP는에 SQL에서 통과 :

Working image generation in browser

내가 잘못된 일을 오전 또는 코드가 변수가 데이터베이스에서 가져온 경우 작동이 중지 이유에 대한 암시되고있는 것을 알고 매우 감사하게 될 거라고.

감사합니다.

+0

두 번째 예에서는 두 경우 모두 여성 이미지를 사용하고 있습니다. –

+0

아직 브라우저에 표시되지 않는 문제가 해결되지 않았습니다. – Pinkernox

+0

본인의 테스트 사례가 유효하지 않음을 이해합니다. 1과 0의 정적 값이 암수 아바타를 예상대로 생성하는지 확인합니다. 관련성을 알 수 없기 때문에 문제 해결 단계를 건너 뛰지 마십시오. –

답변

0

나는 당신의 코드를 시험해 보았고 같은 문제가 발생했다. 그래서 나는 그것을 파고 들었고 아무 것도 데이터베이스에서 반환되지 않았다는 것을 알았다. 그래서 나는 tablename과 함께 데이터베이스 이름 앞에 접두사를 붙였다. 아래 코드를 참조하십시오.

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

시도해보고 어떻게 진행되는지 알려주세요.

관련 문제