Testing PHP in entries

Just testing MT to see if it will allow me to do code in entries.

Inspired by a thread on the MovableType support forum, I want to see if I can display a text string as an image inside a blog entry.

It works! Well, it used to work until something broke on the server…

Nothing terribly complicated, but the ability to use PHP inside a blog entry does have some interesting potential.

For those of you interested in seeing what I did, I just embedded the following in my entry

$text = date("F j, Y, g:i a");
echo "<p>Today's date: $text</p>\n";
// Set the image size based on the font size and text
$font = 8;
$width  = ImageFontWidth($font) * strlen($text);
$height = ImageFontHeight($font);
// Create the image object
$im = imagecreate($width,$height);
// white background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// write the string at the top left
imagestring($im, 5, 0, 0, $text, $textcolor);
// output the image
// write the image to a file
// Change /path/to/image/dir/ to whatever path you're using
if (is_writable("/path/to/image/dir/")) {  // Check to see if the directory is writable
imagepng($im,"test.png",75);
echo "<p>Today's date as an image: <img src=\"test.png\"  height=\"$height\" width=\"$width\" /></p>\n";
}
else echo "<p>Don't have permission to write to the current directory</p>\n";
imagedestroy($im);

Discover more from Imablog

Subscribe to get the latest posts sent to your email.

One Reply to “Testing PHP in entries”

  1. Example 1 at http://www.php.net/manual/en/ref.image.php provides a better way of doing this, making the image generation dynamic and saving the image before displaying it unecessary. I’ll have to remember this trick.
    button.php

    header("Content-type: image/png");
    $string = $_GET['text'];
    $im    = imagecreatefrompng("images/button1.png");
    $orange = imagecolorallocate($im, 220, 210, 60);
    $px    = (imagesx($im) - 7.5 * strlen($string)) / 2;
    imagestring($im, 3, $px, 9, $string, $orange);
    imagepng($im);
    imagedestroy($im);
    

    and call with button.php?text=text

Comments are closed.