...

View Full Version : Help with GD



moos3
08-22-2007, 05:38 PM
Help with gd and php. I have this div set that is a calendar Icon on my site. I want to make it so that I a actual image instead of being a set of divs. any Ideas on how to do this. I have doing a lot of googling and reading. to see what I mean look at labs [dot] guthnur [dot] net / blog

mlseim
08-22-2007, 07:09 PM
I know what you mean ... like a watermark on an image, but the watermark
is actually text. These will get you close:

http://us.php.net/manual/en/function.imagettftext.php

Scroll down and look at specific examples to see if anyone has
an example that is close to what you're trying to do.

Also look at the functions for imagecopymerge

mlseim
08-23-2007, 05:01 PM
I thought that the challenge was so interesting that I actually
put together a real working example:

http://catpin.com/calendar/

That's my first shot at it. You can copy the files and PHP script.
Improve on it and repost so others can utilize it.

I also realize that a blog with many of these would create a lot of
processing time generating the PNG's. Maybe once a graphic has
been created, it can be cached. So the script might look for the
cached image first, if not found, then generate it.

I don't know ... maybe the script is so small it wouldn't make a difference?


.

Fumigator
08-23-2007, 05:29 PM
http://catpin.com/calendar/

That is so slick. Very nice.

Inigoesdr
08-26-2007, 07:35 PM
Maybe once a graphic has
been created, it can be cached. So the script might look for the
cached image first, if not found, then generate it.

You should be able to generate the image for a new post when it is made, delete images for older posts to save space(if you want.. these days it's probably not even necessary) and generate them for archived posts(if you delete old ones) without using too many resources.

Also, here is my version of your script. It uses the same files as yours, I just made a few changes.

<?php
// Calendar Day Cell - Generator

// Get Date from link in the form of mm/dd/yyyy
$date = $_GET['d'];

// If a date is provided, convert to the proper format
if($date)
{
list($month, $day, $year) = explode('/', $date);
if(checkdate($month, $day, $year))
list($month, $day, $year) = explode('/', date('F/jS/Y', mktime(0, 0, 0, $month, $day, $year)));
else
list($month, $day, $year) = explode('/', date('F/jS/Y'));
}
else
{
list($month, $day, $year) = explode('/', date('F/jS/Y'));
}

// Generate Image & set width
$image = imagecreatefrompng("calendar.png");
$imageWidth = imagesx($image);

// Generate Month
$color = imagecolorallocate($image, 255, 255, 255);
$font = "arialbd.ttf";
$fontSize = "8";
$ret = imagettfbbox($fontSize, 0, $font, $month);
imagettftext($image, $fontSize, 0, ($imageWidth - $ret[2]) / 2, 12, $color, $font, $month);

// Generate Day
$color = imagecolorallocate($image, 0, 0, 0);
$font = "arial.ttf";
$fontSize = "14";
$ret = imagettfbbox($fontSize, 0, $font, $day);
imagettftext($image, $fontSize, 0, ($imageWidth - $ret[2]) / 2, 38, $color, $font, $day);

// Generate Year
$color = imagecolorallocate($image, 0, 0, 0);
$font = "arial.ttf";
$fontSize = "10";
$ret = imagettfbbox($fontSize, 0, $font, $year);
imagettftext($image, $fontSize, 0, ($imageWidth - $ret[2]) / 2, 56, $color, $font, $year);

// Output & Free Memory
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
Changes:

Uses explode() instead of split()
Runs the date through checkdate() and if it's invalid uses current date instead
Centers the text for each line by getting the width of the image, and subtracting the width of the generated text(using it's bounding box), and dividing it in half. This should, in theory, allow you to change the image, change the font, or change the text size and everything will still be centered

mlseim
08-26-2007, 11:51 PM
Nice changes.

In my version, September is the widest image, so it almost doesn't fit.
I'm not sure how it looks with your version. Maybe a different font is
all that's needed to make it fit.

anyhow, it was fun to make it all work.

Inigoesdr
08-26-2007, 11:59 PM
Yeah, I just tested it, and you're right it's really close to the border. It looks like 1 or 2 pixels away, but it fits. :thumbsup:

moos3
08-27-2007, 01:52 AM
thanks for all the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum