473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Creating Dynamic Images

K
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.php
// 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_image.php?text=this%20is%20the%20text' 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.ttf" ); // Important! This MUST be a real TTF or
PostScript font file for the script to work!
define("TEMP_IMAGE_DIR", "image_temp" );

define("image_width", 100 );
define("image_height", 23 );
define("lineheight", 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_IMAGE_DIR) ) mkdir(TEMP_IMAGE_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_contents($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_DELIM_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 = 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);

$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($image_width, $image_height);
$im_shadow = imagecreate($image_width, $image_height);

if (is_file($bgimage)){
$suffix = array_pop(split("\.", $bgimage));
$func = "imagecreatefrom" . $filetypes[$suffix];
$im2 = $func($bgimage);
imagecopyresized($im, $im2, 0, 0, 0, 0, 1, 1, 1, 1);
}
$background = imagecolorallocate($im, $bg_r, $bg_g, $bg_b);
$black = imagecolorallocate($im, 0, 0, 0);
$textcolor = imagecolorallocate($im, $fg_r, $fg_g, $fg_b);

if ($_REQUEST['drop_shadow'] == 1 and (defined("GDTOPNG") and
defined("MOGRIFY"))){
$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_offset_x,
$y+$shadow_offset_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 = imagecreatefrompng("/tmp/$tempimage.mgk");
unlink("/tmp/$tempimage.png");
unlink("/tmp/$tempimage.gd");
unlink("/tmp/$tempimage.mgk");

$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;
}
} 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($im);
}
?>
Jul 17 '05 #1
5 2304
*** K escribió/wrote (Fri, 11 Feb 2005 21:16:01 GMT):
However, no matter how I try, I can't get anything other than a blank white
image. define("GDTOPNG", "/usr/local/bin/gdtopng");
define("MOGRIFY", "/usr/bin/mogrify");


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
--
Jul 17 '05 #2
K
"Alvaro G. Vicario" <kA*****************@terra.es> wrote in message
news:6d*****************************@40tude.net...
*** K escribió/wrote (Fri, 11 Feb 2005 21:16:01 GMT):
However, no matter how I try, I can't get anything other than a blank white image.

define("GDTOPNG", "/usr/local/bin/gdtopng");
define("MOGRIFY", "/usr/bin/mogrify");


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.

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.
Jul 17 '05 #3
I noticed that Message-ID:
<gC******************@fe3.news.blueyonder.co.uk> from "K" <@.> contained
the following:
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.

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.


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/
Jul 17 '05 #4
*** K escribió/wrote (Fri, 11 Feb 2005 22:47:40 GMT):
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.


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
--
Jul 17 '05 #5
"K" <@.> wrote in news:lg******************@fe2.news.blueyonder.co.u k:
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.
Add this line:
error_reporting(E_ALL);
and then run the script to see if there are
any errors/warnings/notices.
define("fontdir", "fonts/" ); // Important! This MUST be a real
directory for the script to work!
define("font", "verdana.ttf" ); // Important! This MUST be a real
TTF or PostScript font file for the script to work!


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
http://www.confluence.org/
My website: http://members.shaw.ca/davepatton/
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JOSEPHINE ALVAREZ | last post by:
I have this code that I want to use to do a rollover. However, because the company I am doing it for is continually changing the text, I want to be able to use dynamic text to change the text on...
2
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is...
3
by: EnglishMan69 | last post by:
Hello All, I am using VB2005 Beta 2 in VS 2005 and am running into a small problem. I need to be able to add a picture box to the main form from within a thread. The program goes to a web...
15
by: David Thielen | last post by:
Hi; My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a subdirectory to display as part of the created page. However, the bitmap will not display due to a security violation. ...
0
by: Hemanth | last post by:
Hello, I'm generating a dynamic image on a website (using php.net/image library) and I want add hyperlinks to certain locations on the image - similar to HTML image map. Could someone pls...
16
by: pukivruki | last post by:
hi, I wish to create a temporary table who's name is dynamic based on the argument. ALTER PROCEDURE . @PID1 VARCHAR(50), @PID2 VARCHAR(50), @TICKET VARCHAR(20)
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
1
by: richardgroen | last post by:
Hi all, I got one brainteaser (well...for me). I have a database with, lets say 100 images, these images are 'dynamic' e.g. it can be 100 but also 151 images. My question is: How can i...
3
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
Hi! This discussion may help other programmers get a better idea of how to save uploaded images through a website. Why? Well currently, I save 3 versions of every uploaded image on my own...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.