4 * Copyright 1999,2000,2001,2002,2007 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * File loading and saving diagnostic messages, to see whether an
25 * image was saved to or loaded from the cache and what the path is.
27 * Convert text into a png image using GD without using [WikiPluginCached|Help:WikiPlugin].
28 * The images are stored in a private <PHPWIKI_DIR>/images/<LANG> subdirectory instead,
29 * which are not timestamp checked at all. Delete the .png file(s) if you change anything.
31 * This is a really simple and stupid plugin, which needs some work.
32 * No size and color options, no change check.
34 * We'd need a ButtonCreator for the MacOSX theme buttons also.
35 * Via svg => png, or is gd2 good enough?
37 * PHP must be compiled with support for the GD library version 1.6 or
38 * later to create PNG image files:
40 * ./configure --with-gd
42 * See <http://www.php.net/manual/pl/ref.image.php> for more info.
44 if (!defined('text2png_debug'))
45 define('text2png_debug', DEBUG & _DEBUG_VERBOSE);
47 class WikiPlugin_text2png
50 function getDescription()
52 return _("Convert text into a PNG image using GD.");
55 function getDefaultArguments()
58 // TODO: add fixed size and center.
59 return array('text' => "text2png testtext",
62 'fontsize' => 18, // with GD1 it's the pixelsize, with GD2 the pointsize
64 'fontcolor' => '#000000',
65 'shadowcolor' => '#AFAFAF',
66 'backcolor' => '#ffffff');
69 function run($dbi, $argstr, &$request, $basepage)
71 if (ImageTypes() & IMG_PNG) {
72 // we have gd & png so go ahead.
73 $args = $this->getArgs($argstr, $request);
74 return $this->text2png($args);
76 // we don't have png and/or gd.
77 $error_html = _("Sorry, this version of PHP cannot create PNG image files.");
78 $link = "http://www.php.net/manual/pl/ref.image.php";
79 $error_html .= sprintf(_("See %s"), $link) . ".";
80 trigger_error($error_html, E_USER_NOTICE);
81 return HTML::p($error_html);
86 * Parse hexcolor into ordinal rgb array.
87 * '#000' => array(0,0,0)
88 * '#000000' => array(0,0,0)
90 function hexcolor($h, $default = false)
92 if ($h[0] != '#') return $default;
94 if (strlen($rgb) == 3)
95 return array(hexdec($rgb{0}), hexdec($rgb{1}), hexdec($rgb{2}));
96 elseif (strlen($rgb) == 6)
97 return array(hexdec(substr($rgb, 0, 2)), hexdec(substr($rgb, 2, 2)), hexdec(substr($rgb, 4, 2)));
101 function text2png($args)
105 * Basic image creation and caching
107 * You MUST delete the image cache yourself in /images if you
108 * change the drawing routines!
111 $filename = urlencode($text) . ".png"; // protect by urlencode!!!
114 * FIXME: need something more elegant, and a way to gettext a
115 * different language depending on any individual
116 * user's locale preferences.
120 $l = "en"; //english=C
122 $l = urlencode($l); // who on earth forgot his?
124 $basedir = "text2png-image";
125 $filepath = getUploadFilePath() . "$basedir/$l";
126 if ($_force or !file_exists($filepath . $filename)) {
127 if (!file_exists($filepath)) {
128 $oldumask = umask(0);
129 // permissions affected by user the www server is running as
130 mkdir(getUploadFilePath() . $basedir, 0777);
131 mkdir($filepath, 0777);
136 * prepare a new image
138 * FIXME: needs a dynamic image size depending on text
142 // got this logic from GraphViz
143 if (defined('TTFONT'))
145 elseif (PHP_OS == "Darwin") // Mac OS X
146 $ttfont = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/LucidaSansRegular.ttf"; elseif (isWindows()) {
147 $ttfont = $_ENV['windir'] . '\Fonts\Arial.ttf';
149 $ttfont = 'luximr'; // This is the only what sourceforge offered.
150 //$ttfont = 'Helvetica';
153 /* http://download.php.net/manual/en/function.imagettftext.php
154 * array imagettftext (int im, int size, int angle, int x, int y,
155 * int col, string fontfile, string text)
159 $s = ImageTTFBBox($fontsize, 0, $ttfont, $text);
160 $im = @ImageCreate(abs($s[4]) + 20, abs($s[7]) + 10);
162 $error_html = _("PHP was unable to create a new GD image stream. Read 'lib/plugin/text2png.php' for details.");
163 // FIXME: Error manager does not transform URLs passed
165 $link = "http://www.php.net/manual/en/function.imagecreate.php";
166 $error_html .= sprintf(_("See %s"), $link) . ".";
167 trigger_error($error_html, E_USER_NOTICE);
168 return HTML::p($error_html);
170 $rgb = $this->hexcolor($backcolor, array(255, 255, 255));
171 $bg_color = ImageColorAllocate($im, $rgb[0], $rgb[1], $rgb[2]);
173 $rgb = $this->hexcolor($shadowcolor, array(175, 175, 175));
174 $text_color = ImageColorAllocate($im, $rgb[0], $rgb[1], $rgb[2]);
175 // shadow is 1 pixel down and 2 pixels right
176 ImageTTFText($im, $fontsize, 0, 12, abs($s[7]) + 6, $text_color, $ttfont, $text);
179 $rgb = $this->hexcolor($fontcolor, array(0, 0, 0));
180 $text_color = ImageColorAllocate($im, $rgb[0], $rgb[1], $rgb[2]);
181 ImageTTFText($im, $fontsize, 0, 10, abs($s[7]) + 5, $text_color, $ttfont, $text);
184 * An alternate text drawing method in case ImageTTFText
187 //ImageString($im, 2, 10, 40, $text, $text_color);
189 // To dump directly to browser:
190 //header("Content-type: image/png");
194 $success = ImagePng($im, $filepath . $filename);
201 // create an <img src= tag to show the image!
204 if (defined('text2png_debug')) {
207 trigger_error(sprintf(_("Image saved to cache file: %s"),
208 $filepath . $filename),
211 trigger_error(sprintf(_("Image loaded from cache file: %s"),
212 $filepath . $filename),
216 $url = getUploadDataPath() . "$basedir/" . urlencode($l) . "/" . urlencode($filename);
217 $html->pushContent(HTML::img(array('src' => $url,
219 'title' => '"' . $text . '"' . _(" produced by ") . $this->getName())));
221 trigger_error(sprintf(_("couldn't open file ā%sā for writing"),
222 $filepath . $filename), E_USER_NOTICE);
232 // c-hanging-comment-ender-p: nil
233 // indent-tabs-mode: nil