473,748 Members | 9,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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);
}
?>
Jul 17 '05 #1
5 2374
*** 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.n et...
*** 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.uk:
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.tt f" ); // 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
2721
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 the fly, and still have the rollover work. I have taken the text off of the buttons, but cannot figure out how to do it with dynamic text using javascript and html. For example, in the columns of this row, I want to put "About...
2
4571
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 easy, but 1) How do I set about dynamically creating user controls (like TextBox, TextArea) --- simply Declare and initialised (new) the user controls?? How do I "place" it graphically on the form. Ideally, I want them to lay out in a table, one...
3
3695
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 site, performs a search based on POST variables, retrieves the code, parses the information (including a url for a thumbnail image) and displays the results. I am using threads, since the program will appear to freeze while
15
2835
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. Everything is the default settings I believe. IIS is running under Local System. In IIS the DefaultAppPool is running under Network Service. Annonymous access uses the account IUSR_JASMINE (machine name is Jasmine).
0
1335
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 suggest me a good way of doing this - with/without having to store the dynamic image. TIA, Hemanth
16
10329
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
3628
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 to create the dynamic controls in the OnInit stage of the lifecycle. Since data from static controls is not yet available in the OnInit stage I can't know what dynamic control I have to create.
1
2702
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 make a dynamic table where I can set that the maximum columns is 5. So when parsing the images, then CF fills up a row of 5 images (all in a different column) and the 6th is at a new row etc. etc. I just cant figure it out.
3
3581
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 little website: 1. Small: DOWNsize of original image to be used as a thumbnail. 2. Medium: DOWNsize of original image to be used as user avatars/icons in forums or profiles.
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.