]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/text2png.php
pear --> PEAR
[SourceForge/phpwiki.git] / lib / plugin / text2png.php
1 <?php
2
3 /*
4  * Copyright 1999,2000,2001,2002,2007 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 /**
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.
26  *
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.
30  *
31  * This is a really simple and stupid plugin, which needs some work.
32  * No size and color options, no change check.
33  *
34  * We'd need a ButtonCreator for the MacOSX theme buttons also.
35  * Via svg => png, or is gd2 good enough?
36  *
37  * PHP must be compiled with support for the GD library version 1.6 or
38  * later to create PNG image files:
39  *
40  * ./configure --with-gd
41  *
42  * See <http://www.php.net/manual/pl/ref.image.php> for more info.
43  */
44 if (!defined('text2png_debug'))
45     define('text2png_debug', DEBUG & _DEBUG_VERBOSE);
46
47
48 class WikiPlugin_text2png
49     extends WikiPlugin
50 {
51     function getName()
52     {
53         return "text2png";
54     }
55
56     function getDescription()
57     {
58         return _("Convert text into a png image using GD.");
59     }
60
61     function getDefaultArguments()
62     {
63         global $LANG;
64         // TODO: add fixed size and center.
65         return array('text' => "text2png testtext",
66             'lang' => $LANG,
67             '_force' => 0,
68             'fontsize' => 18, // with GD1 it's the pixelsize, with GD2 the pointsize
69             'with_shadow' => 1,
70             'fontcolor' => '#000000',
71             'shadowcolor' => '#AFAFAF',
72             'backcolor' => '#ffffff');
73     }
74
75     function run($dbi, $argstr, &$request, $basepage)
76     {
77         if (ImageTypes() & IMG_PNG) {
78             // we have gd & png so go ahead.
79             $args = $this->getArgs($argstr, $request);
80             return $this->text2png($args);
81         } else {
82             // we don't have png and/or gd.
83             $error_html = _("Sorry, this version of PHP cannot create PNG image files.");
84             $link = "http://www.php.net/manual/pl/ref.image.php";
85             $error_html .= sprintf(_("See %s"), $link) . ".";
86             trigger_error($error_html, E_USER_NOTICE);
87             return;
88         }
89     }
90
91     /**
92      * Parse hexcolor into ordinal rgb array.
93      * '#000'    => array(0,0,0)
94      * '#000000' => array(0,0,0)
95      */
96     function hexcolor($h, $default = false)
97     {
98         if ($h[0] != '#') return $default;
99         $rgb = substr($h, 1);
100         if (strlen($rgb) == 3)
101             return array(hexdec($rgb{0}), hexdec($rgb{1}), hexdec($rgb{2}));
102         elseif (strlen($rgb) == 6)
103             return array(hexdec(substr($rgb, 0, 2)), hexdec(substr($rgb, 2, 2)), hexdec(substr($rgb, 4, 2)));
104         return $default;
105     }
106
107     function text2png($args)
108     {
109         extract($args);
110         /**
111          * Basic image creation and caching
112          *
113          * You MUST delete the image cache yourself in /images if you
114          * change the drawing routines!
115          */
116
117         $filename = urlencode($text) . ".png"; // protect by urlencode!!!
118
119         /**
120          * FIXME: need something more elegant, and a way to gettext a
121          *        different language depending on any individual
122          *        user's locale preferences.
123          */
124
125         if ($l == "C") {
126             $l = "en"; //english=C
127         } else {
128             $l = urlencode($l); // who on earth forgot his?
129         }
130         $basedir = "text2png-image";
131         $filepath = getUploadFilePath() . "$basedir/$l";
132         if ($_force or !file_exists($filepath . $filename)) {
133             if (!file_exists($filepath)) {
134                 $oldumask = umask(0);
135                 // permissions affected by user the www server is running as
136                 mkdir(getUploadFilePath() . $basedir, 0777);
137                 mkdir($filepath, 0777);
138                 umask($oldumask);
139             }
140             $filepath .= "/";
141             /**
142              * prepare a new image
143              *
144              * FIXME: needs a dynamic image size depending on text
145              *        width and height
146              */
147
148             // got this logic from GraphViz
149             if (defined('TTFONT'))
150                 $ttfont = TTFONT;
151             elseif (PHP_OS == "Darwin") // Mac OS X
152                 $ttfont = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/LucidaSansRegular.ttf"; elseif (isWindows()) {
153                 $ttfont = $_ENV['windir'] . '\Fonts\Arial.ttf';
154             } else {
155                 $ttfont = 'luximr'; // This is the only what sourceforge offered.
156                 //$ttfont = 'Helvetica';
157             }
158
159             /* http://download.php.net/manual/en/function.imagettftext.php
160              * array imagettftext (int im, int size, int angle, int x, int y,
161              *                      int col, string fontfile, string text)
162              */
163
164             // get ready to draw
165             $s = ImageTTFBBox($fontsize, 0, $ttfont, $text);
166             $im = @ImageCreate(abs($s[4]) + 20, abs($s[7]) + 10);
167             if (empty($im)) {
168                 $error_html = _("PHP was unable to create a new GD image stream. Read 'lib/plugin/text2png.php' for details.");
169                 // FIXME: Error manager does not transform URLs passed
170                 //        through it.
171                 $link = "http://www.php.net/manual/en/function.imagecreate.php";
172                 $error_html .= sprintf(_("See %s"), $link) . ".";
173                 trigger_error($error_html, E_USER_NOTICE);
174                 return;
175             }
176             $rgb = $this->hexcolor($backcolor, array(255, 255, 255));
177             $bg_color = ImageColorAllocate($im, $rgb[0], $rgb[1], $rgb[2]);
178             if ($with_shadow) {
179                 $rgb = $this->hexcolor($shadowcolor, array(175, 175, 175));
180                 $text_color = ImageColorAllocate($im, $rgb[0], $rgb[1], $rgb[2]);
181                 // shadow is 1 pixel down and 2 pixels right
182                 ImageTTFText($im, $fontsize, 0, 12, abs($s[7]) + 6, $text_color, $ttfont, $text);
183             }
184             // draw text
185             $rgb = $this->hexcolor($fontcolor, array(0, 0, 0));
186             $text_color = ImageColorAllocate($im, $rgb[0], $rgb[1], $rgb[2]);
187             ImageTTFText($im, $fontsize, 0, 10, abs($s[7]) + 5, $text_color, $ttfont, $text);
188
189             /**
190              * An alternate text drawing method in case ImageTTFText
191              * doesn't work.
192              **/
193             //ImageString($im, 2, 10, 40, $text, $text_color);
194
195             // To dump directly to browser:
196             //header("Content-type: image/png");
197             //ImagePng($im);
198
199             // to save to file:
200             $success = ImagePng($im, $filepath . $filename);
201
202         } else {
203             $filepath .= "/";
204             $success = 2;
205         }
206
207         // create an <img src= tag to show the image!
208         $html = HTML();
209         if ($success > 0) {
210             if (defined('text2png_debug')) {
211                 switch ($success) {
212                     case 1:
213                         trigger_error(sprintf(_("Image saved to cache file: %s"),
214                                 $filepath . $filename),
215                             E_USER_NOTICE);
216                     case 2:
217                         trigger_error(sprintf(_("Image loaded from cache file: %s"),
218                                 $filepath . $filename),
219                             E_USER_NOTICE);
220                 }
221             }
222             $url = getUploadDataPath() . "$basedir/" . urlencode($l) . "/" . urlencode($filename);
223             $html->pushContent(HTML::img(array('src' => $url,
224                 'alt' => $text,
225                 'title' => '"' . $text . '"' . _(" produced by ") . $this->getName())));
226         } else {
227             trigger_error(sprintf(_("couldn't open file '%s' for writing"),
228                 $filepath . $filename), E_USER_NOTICE);
229         }
230         return $html;
231     }
232 }
233
234
235
236 // Local Variables:
237 // mode: php
238 // tab-width: 8
239 // c-basic-offset: 4
240 // c-hanging-comment-ender-p: nil
241 // indent-tabs-mode: nil
242 // End: