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