[GD] Image just turns black

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Vessla
Forum Newbie
Posts: 1
Joined: Wed Aug 13, 2008 9:13 am

[GD] Image just turns black

Post by Vessla »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi guys, I'm trying to create a picture with GD and PHP. Frankly, the result is so-so. The image is in fact created with specified height and width, BUT it's completely black! Anybody has any idea what the problem might be? Thanks. :)
And yes, all functions I'm using have been declared prior to the code below.

Code: Select all

  <?php
$im = imagecreatetruecolor(100, 100);
 header('Content-type: image/png');
    $split_hex_bg = str_split(str_replace('#','', $_POST['hexbg']),2);
    $split_hex_txt = str_split(str_replace('#','', $_POST['hexbg]),2);
 
    $bg_color = imagecolorallocate($im, str_pad($split_hex_bg[0], 4, '0x', STR_PAD_LEFT), str_pad($split_hex_bg[1], 4, '0x', STR_PAD_LEFT), str_pad($split_hex_bg[2], 4, '0x', STR_PAD_LEFT));
    $txt_color = imagecolorallocate($im, str_pad($split_hex_txt[0], 4, '0x', STR_PAD_LEFT), str_pad($split_hex_txt[1], 4, '0x', STR_PAD_LEFT), str_pad($split_hex_txt[2], 4, '0x', STR_PAD_LEFT));
    imagefill($im, 0, 0, $bg_color);
 
    $font = 'fonts/arial.TTF';
    $txt_artist = str_cut(wordwrap($_POST['artist'], 10), 20);
    $txt_album = str_cut(wordwrap($_POST['album'], 10), 17);
 
    imagettftext($im, 11, 0, 10, 22, $txt_color, $font, $txt_artist);
    imagettftext($im, 11, 0, 10, 70, $txt_color, $font, $txt_album);
 
    imagepng($im, '../images/infoimgs/info-'.filename($_POST['artist']).'-'.filename($_POST['album']).'.png');
    imagedestroy($im);
?>
 

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: [GD] Image just turns black

Post by pickle »

Check that the colour you're calculating is what you expect.

Know that I've made the code show syntax highlighting, you can see a parse error in your code.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply