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