Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Help with GD

  1. #1
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post

    Help with GD

    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

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    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

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    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?


    .
    Last edited by mlseim; 08-23-2007 at 06:11 PM.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    That is so slick. Very nice.

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by mlseim View Post
    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 Code:
    <?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(000$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($image255255255); 
    $font "arialbd.ttf"
    $fontSize "8";
    $ret imagettfbbox($fontSize0$font$month);
    imagettftext($image$fontSize0, ($imageWidth $ret[2]) / 212$color$font$month);

    // Generate Day
    $color imagecolorallocate($image000); 
    $font "arial.ttf"
    $fontSize "14";
    $ret imagettfbbox($fontSize0$font$day);
    imagettftext($image$fontSize0, ($imageWidth $ret[2]) / 238$color$font$day);

    // Generate Year
    $color imagecolorallocate($image000); 
    $font "arial.ttf"
    $fontSize "10";
    $ret imagettfbbox($fontSize0$font$year);
    imagettftext($image$fontSize0, ($imageWidth $ret[2]) / 256$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

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    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.

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    thanks for all the help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •