Dynamic Image Creation

CannonBallGuyCannonBallGuy ModeratorShared Hoster
I have a wordpress plugin that will pick a line randomly from a text file and display it wherever I place the code: witty ().

I also have an image creation script.

[php]<?php
// image_header.php
// Generate dynamic text banner images using TTF fonts and PHP.
// Copyright 2003, 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_image.php?text=this is the text' width='300' height='23'>
//
// CONFIGURATION - Change these items manually here or pass in values via the URL
define("fontdir", "/path/to/images/ttf" );
define("font", "hlmc____.pfb" );
define("image_width", 300 );
define("image_height", 23 );
define("lineheight", 14 );
define("x", 1 );
define("y", lineheight );
define("cols", 1 );

// Text color, RGB values 0-255
define("fg_r", 0 );
define("fg_g", 0 );
define("fg_b", 51 );
// background color, RGB values 0-255
define("bg_r", 255 );
define("bg_g", 255 );
define("bg_b", 255 );

// DO NOT CHANGE BELOW THIS LINE
$fontdir = $_REQUEST ? $_REQUEST : fontdir;
$font = $_REQUEST ? $_REQUEST : font;
$image_width = $_REQUEST ? $_REQUEST : image_width;
$image_height = $_REQUEST ? $_REQUEST : image_height;
$lineheight = $_REQUEST ? $_REQUEST : lineheight;
$x = $_REQUEST ? $_REQUEST : x;
$y = $_REQUEST ? $_REQUEST : $_REQUEST ? $_REQUEST : lineheight;
$cols = $_REQUEST ? $_REQUEST : cols;

// Text color, RGB values 0-255
$fg_r = $_REQUEST ? $_REQUEST : fg_r;
$fg_g = $_REQUEST ? $_REQUEST : fg_g;
$fg_b = $_REQUEST ? $_REQUEST : fg_b;
// background color, RGB values 0-255
$bg_r = $_REQUEST ? $_REQUEST : bg_r;
$bg_g = $_REQUEST ? $_REQUEST : bg_g;
$bg_b = $_REQUEST ? $_REQUEST : bg_b;
// Sample colors...
// $white = imagecolorallocate($im, 255, 255, 255);
// $gray1 = imagecolorallocate($im, 204, 204, 204);
// $white = imagecolorallocate($im, 255, 255, 255);
// $orange = imagecolorallocate($im, 220, 210, 60);
// $dark_red = imagecolorallocate($im, 130, 28, 28);
// $black = imagecolorallocate($im, 0, 0, 0);
header("Content-type: image/png");
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) ));
}
$result = $_REQUEST;
$lines = preg_split("/n/", $result);
$result = preg_replace("/n/", "rn", $result);
$im = imagecreate($image_width, $image_height);
$background = imagecolorallocate($im, $bg_r, $bg_g, $bg_b);
$textcolor = imagecolorallocate($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;
}
imagepng($im, '', 85);
imagedestroy($im);

?>[/php]

[php]<?php
/*
Plugin Name: Witty Text
Plugin URI: http://www.w-a-s-a-b-i.com/
Description: Outputs a random quote, predefined in text file. Click <a href="../wp-admin/templates.php?file=%2Fwp-content%2Fwitty.txt">here</a> to define your own quotes (each quote in it's own line). <em>Usage: <code>witty('$before', '$after');</code></em>.
Version: 1.1
Author: Alexander Malov
Author URI: http://www.w-a-s-a-b-i.com/
*/

function witty ($before = '', $after = '') {
$file_path = "witty.txt";
$witty_file = (ABSPATH . "wp-content/" . $file_path);
if (!file_exists($witty_file)) {
touch($witty_file);
chmod($witty_file, 0644);}
else {
$sniplets = file("$witty_file");
$sniplet = rand(0, sizeof($sniplets)-1);
echo $before, $sniplets[$sniplet], $after;
}
}
?>[/php]

I want to combine the two so that I can have a page where it makes an image which shows one of the lines from the text file, which I can then add to the signatures at the forums I visit using the tags...  How can I do this?

Comments

  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Bump.
    Can't anyone help?
    I know most people here are good at PHP... Please help!
  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    Try this (my PHP skills with GD aren't brilliant, but it may work):
    [php]
    <?php
    // image_header.php
    // Generate dynamic text banner images using TTF fonts and PHP.
    // Copyright 2003, The Johns Hopkins University, All rights reserved.
    // Author: Jeffrey D. Silverman
    // Date: 14-January-2004
    // Description:
    // Use this script to generate graphic headers.
    // Edited to get data from a file and output that instead :D
    // Should get data from the file and print it :/
    // Example usage:
    // <img src='header_image.php' width='300' height='23'>
    //
    // CONFIGURATION - Change these items manually here or pass in values via the URL
    define("fontdir", "/path/to/images/ttf" );
    define("font", "hlmc____.pfb" );
    define("image_width", 300 );
    define("image_height", 23 );
    define("lineheight", 14 );
    define("x", 1 );
    define("y", lineheight );
    define("cols", 1 );

    // function from your "witty" code :]
    function witty ($before = '', $after = '') {
    $file_path = "witty.txt";
    $witty_file = (ABSPATH . "wp-content/" . $file_path);
    if (!file_exists($witty_file)) {
    touch($witty_file);
    chmod($witty_file, 0644);}
    else {
    $sniplets = file("$witty_file");
    $sniplet = rand(0, sizeof($sniplets)-1);
    // bit of poking about here, just changed it so it doesn't echo the result, which would suck.
    $output = $before. $sniplets[$sniplet]. $after;
    return $output;
    }
    }

    // Text color, RGB values 0-255
    define("fg_r", 0 );
    define("fg_g", 0 );
    define("fg_b", 51 );
    // background color, RGB values 0-255
    define("bg_r", 255 );
    define("bg_g", 255 );
    define("bg_b", 255 );

    // DO NOT CHANGE BELOW THIS LINE... SORRY, i ALREADY DID :D
    $fontdir = $_REQUEST ? $_REQUEST : fontdir;
    $font = $_REQUEST ? $_REQUEST : font;
    $image_width = $_REQUEST ? $_REQUEST : image_width;
    $image_height = $_REQUEST ? $_REQUEST : image_height;
    $lineheight = $_REQUEST ? $_REQUEST : lineheight;
    $x = $_REQUEST ? $_REQUEST : x;
    $y = $_REQUEST ? $_REQUEST : $_REQUEST ? $_REQUEST : lineheight;
    $cols = $_REQUEST ? $_REQUEST : cols;

    // Text color, RGB values 0-255
    $fg_r = $_REQUEST ? $_REQUEST : fg_r;
    $fg_g = $_REQUEST ? $_REQUEST : fg_g;
    $fg_b = $_REQUEST ? $_REQUEST : fg_b;
    // background color, RGB values 0-255
    $bg_r = $_REQUEST ? $_REQUEST : bg_r;
    $bg_g = $_REQUEST ? $_REQUEST : bg_g;
    $bg_b = $_REQUEST ? $_REQUEST : bg_b;
    // Sample colors...
    // $white = imagecolorallocate($im, 255, 255, 255);
    // $gray1 = imagecolorallocate($im, 204, 204, 204);
    // $white = imagecolorallocate($im, 255, 255, 255);
    // $orange = imagecolorallocate($im, 220, 210, 60);
    // $dark_red = imagecolorallocate($im, 130, 28, 28);
    // $black = imagecolorallocate($im, 0, 0, 0);
    header("Content-type: image/png");
    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) ));
    }
    // Now we're not using a query string, we are using the function, so this gets changed to a function call, meaning the output of the function is inserted into the variable.
    $result = witty("args");
    $lines = preg_split("/n/", $result);
    $result = preg_replace("/n/", "rn", $result);
    $im = imagecreate($image_width, $image_height);
    $background = imagecolorallocate($im, $bg_r, $bg_g, $bg_b);
    $textcolor = imagecolorallocate($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;
    }
    imagepng($im, '', 85);
    imagedestroy($im);

    ?>
    [/php]
    Like I said, it might not work right as my PHP skills aren't perfect when it comes to things like GD.
    If you're going to use PHP for images like this, I'd reccomend setting up Apache to run your file with the .png extension rather than .php (use a .htaccess file to set this up) since some sites don't like .php files in image tags.
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!
  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    Hmm...
    The font error might be caused by you not having the TTF font uploaded and the directory set up in the script.
    I'm not sure about the first error since I hac only see one place that sends headers in the code I did.
    The second one is caused by a problem with the origional code... I think.
    It can't make the file ABSPATHhttp://www.cannonballguy.com/wp-content/plugins/witty.txt because you're not making a file at ABSPATHhttp://www.cannonballguy.com/wp-content/plugins/ and this should, if the origional code was correct, have tried to create the file at http://www.cannonballguy.com/wp-content/plugins/
    The chmod error seems to be another case of not finding the directory.
    I'd reccomend looking through the code and seeing if you can fixe the errors.
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    I have a dfont in the right place... Does it Have to be a ttf?
    And as for the others, I don't know how to fix them.
  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    I have a dfont in the right place... Does it Have to be a ttf?
    And as for the others, I don't know how to fix them.
    I think so...
    Did you make sure the following lines point to the right places:
    define("fontdir", "/path/to/images/ttf" );
        define("font",   "hlmc____.pfb" );
    
    $witty_file = (ABSPATH . "wp-content/" . $file_path);
    
    This should be "http://www.cannonballguy.com/wp-content/plugins/witty.txt"; or "http://www.cannonballguy.com/wp-content/plugins/".$file_path
    Fixing that will also sort out the chmod error...
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Yep, all that looks correct to me. :(
Sign In or Register to comment.