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