Jump to content

GDlib PHP Help


freaksville

Recommended Posts

Hey! I am scripting a PHP page using GD-lib to create an image that tells you how many days left until Christmas. I have written most of the script, I just cant seem to get the countdown text on it. Can someone help me? Thanks

 

you can view the current status http://www.freaksville.org/christmas/img.php if you could help me that would be great. Thanks again in advance

 

Brad

 

PS. If you can solve this for me quick it is much appreciated.

 

BTW the current code is as follows:

<?php
function LoadPNG($imgname)
{
/* Attempt to open */
$im = @imagecreatefrompng($imgname);

/* See if it failed */
if(!$im)
{
/* Create a blank image */
$im = imagecreatetruecolor(177, 134);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

/* Output an error message */
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}

return $im;
}

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

$img = LoadPNG('crimbo_back.png');
imagealphablending($img, true);
imagesavealpha($img, true); // save alphablending setting (important)

imagepng($img);
imagedestroy($img);
?>

Link to comment
https://forums.phpfreaks.com/topic/137335-gdlib-php-help/
Share on other sites

You might try this... I didn't test it

 

<?php
function LoadPNG($imgname, &$im)
{
/* Attempt to open */
$im = @imagecreatefrompng($imgname);

/* See if it failed */
if(!$im)
{
	/* Create a blank image */
	$im = imagecreatetruecolor(177, 134);
	$bgc = imagecolorallocate($im, 255, 255, 255);
	$tc = imagecolorallocate($im, 0, 0, 0);

	/* Output an error message */
	imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
	imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);

	return false;
}

return true;
}


if(LoadPNG('crimbo_back.png', $img)) {
$daysTilChristmas = date('j', 1230184800) - date('j');
$textColor = imagecolorallocate($im, 0, 0, 0);
imagestring($img, 1, 5, 5, $daysTilChristmas . ' Days Until Christmas', $textColor);
}

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

header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>

 

I changed the way you returned in the function because A) it let me say whether the loading of the background image was successful and B) because copying a full image as a string is expensive - references are better for large things like images.

 

This will hopefully add the number to the top left - try to move the number around on the background for funsies, try to figure out why I did things the way I did.And most importantly, fix any bugs I made! I totally didn't test this code.

 

I assumed in reading your code that the background image to which you wished to add the countdown was "crimbo_back.png".

 

edit: it would help if I remembered to put back in the header after I removed it.

Link to comment
https://forums.phpfreaks.com/topic/137335-gdlib-php-help/#findComment-717550
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.