Creating Dynamic Images

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K

    Creating Dynamic Images

    I have found a script online that I want to use (I am new to PHP).

    It creates dynamic images based on the text that you pass it.

    However, no matter how I try, I can't get anything other than a blank white
    image.

    Can one of you knowledgeable people please have a look at the code below and
    help please.

    ---

    <?php
    // image_header.ph p
    // Generate dynamic text banner images using TTF fonts and PHP.
    // Copyright 2003,2004 The Johns Hopkins University, All rights reserved.
    // Author: Jeffrey D. Silverman
    // Date: 14-January-2004
    // Description:
    // Use this script to generate graphic headers.
    // Pass in a value for "$text" to print.
    // Example usage:
    // <img src='header_ima ge.php?text=thi s%20is%20the%20 text' width='300'
    height='23'>
    //
    // CONFIGURATION - Change these items manually here or pass in values via
    the URL
    // ITEMS that can be passed in (optionally) via the URL are:
    // fontdir
    // bg_b
    // bgcolor
    // bg_g
    // bgimage
    // bg_r
    // center_x
    // center_y
    // cols
    // drop_shadow
    // fg_b
    // fgcolor
    // fg_g
    // fg_r
    // font
    // fontdir
    // image_height
    // image_width
    // lineheight
    // linewidth
    // shadow_offset_x
    // shadow_offset_y
    // text
    // x
    // y
    define("fontdir ", "fonts/" ); // Important! This MUST be a real directory
    for the script to work!
    define("font", "verdana.tt f" ); // Important! This MUST be a real TTF or
    PostScript font file for the script to work!
    define("TEMP_IM AGE_DIR", "image_temp " );

    define("image_w idth", 100 );
    define("image_h eight", 23 );
    define("linehei ght", 12 );
    define("x", 10 );
    define("y", lineheight );
    define("cols", 1 );
    // Text (foreground) color, RGB values 0-255
    define("fg_r", 255 );
    define("fg_g", 255 );
    define("fg_b", 255 );
    // Background color, RGB values 0-255
    define("bg_r", 0 );
    define("bg_g", 0 );
    define("bg_b", 0 );

    # Configuration of external programs
    # You MUST put the full path to the proper programs here. Not doing so will
    disable
    # the drop-shadow functionality
    define("GDTOPNG ", "/usr/local/bin/gdtopng");
    define("MOGRIFY ", "/usr/bin/mogrify");
    // END OF CONFIGURATION

    // DO NOT CHANGE BELOW THIS LINE //////////////////////////

    if (! is_dir(TEMP_IMA GE_DIR) ) mkdir(TEMP_IMAG E_DIR);

    // Check for saved images based on MD5 Hash of the $_SERVER['QUERY_STRING']
    $saved_image = TEMP_IMAGE_DIR . "/" . md5($_SERVER['QUERY_STRING']) .
    ".png";
    if (is_file( $saved_image)){
    header("Content-type: image/png");
    echo file_get_conten ts($saved_image );
    exit;
    } else {
    $fontdir = $_REQUEST['fontdir'] ? $_REQUEST['fontdir'] : fontdir;
    $font = $_REQUEST['font'] ? $_REQUEST['font'] : font;
    $image_width = $_REQUEST['image_width'] ? $_REQUEST['image_width'] :
    image_width;
    $image_height = $_REQUEST['image_height'] ? $_REQUEST['image_height'] :
    image_height;
    $lineheight = $_REQUEST['lineheight'] ? $_REQUEST['lineheight'] :
    lineheight;
    $x = $_REQUEST['x'] ? $_REQUEST['x'] : x;
    // $y = $_REQUEST['y'] ? $_REQUEST['y'] : $_REQUEST['lineheight'] ?
    $_REQUEST['lineheight'] : lineheight;
    $y = $_REQUEST['y'] ? $_REQUEST['y'] : y;
    if ($_REQUEST['center_x']){
    // x-offset = ((width of box) - (width of string)) / 2
    $x = ( $image_width / 2 ) + ($image_height / 2 );
    }
    // Need to make this dynamic. Not all header images should break on
    multiple lines! (Notably the page_title)
    if ($_REQUEST['linewidth']){
    // linewidth MUST be passed in through the URL to split long lines.
    // Otherwise the text will be all on one line.
    $split_lines = preg_split("/(.{" . $_REQUEST['linewidth'] .
    "}\S*)\s/", $_REQUEST['text'], -1, PREG_SPLIT_DELI M_CAPTURE);
    } else {
    $split_lines = array($_REQUEST['text']);
    }

    $cols = $_REQUEST['cols'] ? $_REQUEST['cols'] : cols;
    // Text color, RGB values 0-255
    $fg_r = $_REQUEST['fg_r'] ? $_REQUEST['fg_r'] : fg_r;
    $fg_g = $_REQUEST['fg_g'] ? $_REQUEST['fg_g'] : fg_g;
    $fg_b = $_REQUEST['fg_b'] ? $_REQUEST['fg_b'] : fg_b;
    // background color, RGB values 0-255
    $bg_r = $_REQUEST['bg_r'] ? $_REQUEST['bg_r'] : bg_r;
    $bg_g = $_REQUEST['bg_g'] ? $_REQUEST['bg_g'] : bg_g;
    $bg_b = $_REQUEST['bg_b'] ? $_REQUEST['bg_b'] : bg_b;
    // Background Image
    $fgcolor = $_REQUEST['fgcolor'] ? $_REQUEST['fgcolor'] : false;
    $bgcolor = $_REQUEST['bgcolor'] ? $_REQUEST['bgcolor'] : false;
    $bgimage = $_REQUEST['bgimage'] ? $_REQUEST['bgimage'] : false;
    // Sample colors...
    // $white = imagecoloralloc ate($im, 255, 255, 255);
    // $gray1 = imagecoloralloc ate($im, 204, 204, 204);
    // $white = imagecoloralloc ate($im, 255, 255, 255);
    // $orange = imagecoloralloc ate($im, 220, 210, 60);
    // $dark_red = imagecoloralloc ate($im, 130, 28, 28);
    // $black = imagecoloralloc ate($im, 0, 0, 0);

    $filetypes = array(
    "gif" => "gif"
    , "jpg" => "jpeg"
    , "jpeg" => "jpeg"
    , "jpe" => "jpeg"
    , "png" => "png"
    );

    // TO DEBUG. Set Content-tpye to text/plain
    // header("Content-type: text/html");
    header("Content-type: image/png");
    if ($fgcolor) {
    $fgcolor = preg_replace("/[^0-9A-Za-z]/", "", $fgcolor);
    list($fg_r, $fg_g, $fg_b) = array(hexdec( substr($fgcolor , 0, 2) ),
    hexdec( substr($fgcolor , 2, 2) ),
    hexdec( substr($fgcolor , 4, 2) ));
    }
    if ($bgcolor) {
    $bgcolor = preg_replace("/[^0-9A-Za-z]/", "", $bgcolor);
    list($bg_r, $bg_g, $bg_b) = array(hexdec( substr($bgcolor , 0, 2) ),
    hexdec( substr($bgcolor , 2, 2) ),
    hexdec( substr($bgcolor , 4, 2) ));
    }
    // Hack/Bugfix:
    $result = preg_replace("/&amp;/", "&", $_REQUEST['text']); // fixes
    problem with &amp; showing up inside images!

    foreach ($split_lines as $line){
    if (! preg_match("/^\s*$/", $line)) $lines[] = $line;
    }
    $no_lines = count($lines);
    if ($_REQUEST['center_y']){
    $center_offset = ($no_lines + 1);
    $y = ($image_height / 2 ) - ( (($no_lines / 2) * $lineheight) -
    $lineheight );
    }


    // $lines = preg_split("/\n/", $result);
    $result = preg_replace("/\n/", "\r\n", $result);
    $im = imagecreate($im age_width, $image_height);
    $im_shadow = imagecreate($im age_width, $image_height);

    if (is_file($bgima ge)){
    $suffix = array_pop(split ("\.", $bgimage));
    $func = "imagecreatefro m" . $filetypes[$suffix];
    $im2 = $func($bgimage) ;
    imagecopyresize d($im, $im2, 0, 0, 0, 0, 1, 1, 1, 1);
    }


    $background = imagecoloralloc ate($im, $bg_r, $bg_g, $bg_b);
    $black = imagecoloralloc ate($im, 0, 0, 0);
    $textcolor = imagecoloralloc ate($im, $fg_r, $fg_g, $fg_b);

    if ($_REQUEST['drop_shadow'] == 1 and (defined("GDTOP NG") and
    defined("MOGRIF Y"))){
    $y_orig = $y;
    $shadow_offset_ x = isset($_REQUEST['shadow_offset_ x']) ?
    $_REQUEST['shadow_offset_ x'] : 2;
    $shadow_offset_ y = isset($_REQUEST['shadow_offset_ y']) ?
    $_REQUEST['shadow_offset_ y'] : 2;
    foreach ($lines as $string) {
    $string = preg_replace("/^\s*/", "", $string);
    imagefttext($im , $lineheight - 1, 0, $x+$shadow_offs et_x,
    $y+$shadow_offs et_y, $black, "$fontdir/$font", $string, array() );
    $y += $lineheight;
    }
    $y = $y_orig;
    $tempimage = md5(time() . "" . rand(0,10000000 ));

    imagegd($im, "/tmp/$tempimage.gd") ;
    system(GDTOPNG . " /tmp/$tempimage.gd /tmp/$tempimage.png" );
    system(MOGRIFY . " -blur 2 /tmp/$tempimage.png" );
    $im = imagecreatefrom png("/tmp/$tempimage.mgk" );
    unlink("/tmp/$tempimage.png" );
    unlink("/tmp/$tempimage.gd") ;
    unlink("/tmp/$tempimage.mgk" );

    $textcolor = imagecoloralloc ate($im, $fg_r, $fg_g, $fg_b);
    foreach ($lines as $string) {
    $string = preg_replace("/^\s*/", "", $string);
    imagefttext($im , $lineheight - 1, 0, $x, $y, $textcolor,
    "$fontdir/$font", $string, array() );
    $y += $lineheight;
    }
    } else {
    foreach ($lines as $string) {
    $string = preg_replace("/^\s*/", "", $string);
    imagefttext($im , $lineheight - 1, 0, $x, $y, $textcolor,
    "$fontdir/$font", $string, array() );
    $y += $lineheight;
    }
    }
    imagepng($im);
    imagepng($im, $saved_image);
    imagedestroy($i m);
    }
    ?>


  • Alvaro G. Vicario

    #2
    Re: Creating Dynamic Images

    *** K escribió/wrote (Fri, 11 Feb 2005 21:16:01 GMT):[color=blue]
    > However, no matter how I try, I can't get anything other than a blank white
    > image.[/color]
    [color=blue]
    > define("GDTOPNG ", "/usr/local/bin/gdtopng");
    > define("MOGRIFY ", "/usr/bin/mogrify");[/color]

    It seems you need to install some external software. It it's a Unix server
    it may be already there but in a Windows server it won't. Check that first.



    --
    -+ Álvaro G. Vicario - Burgos, Spain
    +- http://www.demogracia.com (la web de humor barnizada para la intemperie)
    ++ Manda tus dudas al grupo, no a mi buzón
    -+ Send your questions to the group, not to my mailbox
    --

    Comment

    • K

      #3
      Re: Creating Dynamic Images

      "Alvaro G. Vicario" <kAlvaroNOSPAMT HANKS@terra.es> wrote in message
      news:6dfkf8ws4s np.1f2j8pmq0vse r$.dlg@40tude.n et...[color=blue]
      > *** K escribió/wrote (Fri, 11 Feb 2005 21:16:01 GMT):[color=green]
      > > However, no matter how I try, I can't get anything other than a blank[/color][/color]
      white[color=blue][color=green]
      > > image.[/color]
      >[color=green]
      > > define("GDTOPNG ", "/usr/local/bin/gdtopng");
      > > define("MOGRIFY ", "/usr/bin/mogrify");[/color]
      >
      > It seems you need to install some external software. It it's a Unix server
      > it may be already there but in a Windows server it won't. Check that[/color]
      first.

      It is a Linux server. The comments in the code indicate that these two
      pieces of software are only needed for drop shadows which I don't require.
      That said though I don't know PHP.


      Comment

      • Geoff Berrow

        #4
        Re: Creating Dynamic Images

        I noticed that Message-ID:
        <gCaPd.62940$B8 .37681@fe3.news .blueyonder.co. uk> from "K" <@.> contained
        the following:
        [color=blue][color=green]
        >> It seems you need to install some external software. It it's a Unix server
        >> it may be already there but in a Windows server it won't. Check that[/color]
        >first.
        >
        >It is a Linux server. The comments in the code indicate that these two
        >pieces of software are only needed for drop shadows which I don't require.
        >That said though I don't know PHP.[/color]

        Well Jeffrey Silverman posts here so with any luck he'll be along soon
        to explain. :-)

        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        • Alvaro G. Vicario

          #5
          Re: Creating Dynamic Images

          *** K escribió/wrote (Fri, 11 Feb 2005 22:47:40 GMT):[color=blue]
          > It is a Linux server. The comments in the code indicate that these two
          > pieces of software are only needed for drop shadows which I don't require.
          > That said though I don't know PHP.[/color]

          Yes, you are right, I didn't actually go further in the code.

          So you do get an image, only that it's blank... What parameters do you pass
          to the script? You need at least a text:

          http://......../image_header.php?tex...s%20the%20text

          This should return a white text on a black image.


          Also, just in case: add these lines to the beginning of the file:

          <?
          error_reporting (E_ALL & ~E_NOTICE);
          ?>

          Then find where it says:

          // TO DEBUG. Set Content-tpye to text/plain
          // header("Content-type: text/html");
          header("Content-type: image/png");

          and let it look like this:

          // TO DEBUG. Set Content-tpye to text/plain
          header("Content-type: text/html");
          // header("Content-type: image/png");

          That way, if there're error messages you'll be able to see them.


          --
          -+ Álvaro G. Vicario - Burgos, Spain
          +- http://www.demogracia.com (la web de humor barnizada para la intemperie)
          ++ Manda tus dudas al grupo, no a mi buzón
          -+ Send your questions to the group, not to my mailbox
          --

          Comment

          • Dave Patton

            #6
            Re: Creating Dynamic Images

            "K" <@.> wrote in news:lg9Pd.8759 5$K7.22981@fe2. news.blueyonder .co.uk:
            [color=blue]
            > I have found a script online that I want to use (I am new to PHP).
            >
            > It creates dynamic images based on the text that you pass it.
            >
            > However, no matter how I try, I can't get anything other than a blank
            > white image.[/color]

            Add this line:
            error_reporting (E_ALL);
            and then run the script to see if there are
            any errors/warnings/notices.
            [color=blue]
            > define("fontdir ", "fonts/" ); // Important! This MUST be a real
            > directory for the script to work!
            > define("font", "verdana.tt f" ); // Important! This MUST be a real
            > TTF or PostScript font file for the script to work![/color]

            Have you changed these to properly reference your
            fonts directory, and confirmed that you have
            verdana installed as a font?

            --
            Dave Patton
            Canadian Coordinator, Degree Confluence Project
            The Degree Confluence Project contains photographs of the intersections of integer latitude and longitude degree lines.

            My website: http://members.shaw.ca/davepatton/

            Comment

            Working...