]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/text2png.php
enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for...
[SourceForge/phpwiki.git] / lib / plugin / text2png.php
1 <?php // -*-php-*-
2 rcs_id('$Id: text2png.php,v 1.13 2004-02-17 12:11:36 rurban Exp $');
3 /*
4  Copyright 1999, 2000, 2001, 2002 $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  * PHP must be compiled with support for the GD library version 1.6 or
29  * later to create PNG image files:
30  *
31  * ./configure --with-gd
32  *
33  * See <http://www.php.net/manual/pl/ref.image.php> for more info.
34  */
35 define('text2png_debug', true);
36
37
38 class WikiPlugin_text2png
39 extends WikiPlugin
40 {
41     function getName () {
42         return "text2png";
43     }
44
45     function getDescription() {
46         return _("Convert text into a png image using GD.");
47     }
48
49     function getVersion() {
50         return preg_replace("/[Revision: $]/", '',
51                             "\$Revision: 1.13 $");
52     }
53
54     function getDefaultArguments() {
55         global $LANG;
56         return array('text' => "Hello WikiWorld!",
57                      'l'    => $LANG );
58         }
59
60     function run($dbi, $argstr, &$request, $basepage) {
61         if (ImageTypes() & IMG_PNG) {
62             // we have gd & png so go ahead.
63             extract($this->getArgs($argstr, $request));
64             return $this->text2png($text,$l);
65         } else {
66             // we don't have png and/or gd.
67             $error_html = _("Sorry, this version of PHP cannot create PNG image files.");
68             $link = "http://www.php.net/manual/pl/ref.image.php";
69             $error_html .= sprintf(_("See %s"), $link) .".";
70             trigger_error($error_html, E_USER_NOTICE);
71             return;
72         }
73     }
74
75     function text2png($text, $l) {
76
77         /**
78          * Basic image creation and caching
79          *
80          * You MUST delete the image cache yourself in /images if you
81          * change the drawing routines!
82          */
83
84         $filename = $text . ".png";
85
86         /**
87          * FIXME: need something more elegant, and a way to gettext a
88          *        different language depending on any individual
89          *        user's locale preferences.
90          */
91
92         if ($l == "C") {
93             $l = "en";
94         } //english=C
95         $filepath = getcwd() . "/images/$l";
96
97         if (!file_exists($filepath ."/". $filename)) {
98
99             if (!file_exists($filepath)) {
100                 $oldumask = umask(0);
101                 // permissions affected by user the www server is running as
102                 mkdir($filepath, 0777);
103                 umask($oldumask);
104             }
105
106             // add trailing slash to save some keystrokes later
107             $filepath .= "/";
108
109             /**
110              * prepare a new image
111              *
112              * FIXME: needs a dynamic image size depending on text
113              *        width and height
114              */
115             $im = @ImageCreate(150, 50);
116
117             if (empty($im)) {
118                 $error_html = _("PHP was unable to create a new GD image stream. Read 'lib/plugin/text2png.php' for details.");
119                 // FIXME: Error manager does not transform URLs passed
120                 //        through it.
121                 $link = "http://www.php.net/manual/en/function.imagecreate.php";
122                 $error_html .= sprintf(_("See %s"), $link) .".";
123                 trigger_error( $error_html, E_USER_NOTICE );
124                 return;
125             }
126             // get ready to draw
127             $bg_color = ImageColorAllocate($im, 255, 255, 255);
128             $ttfont   = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/LucidaSansRegular.ttf";
129
130             /* http://download.php.net/manual/en/function.imagettftext.php
131              * array imagettftext (int im, int size, int angle, int x, int y,
132              *                      int col, string fontfile, string text)
133              */
134
135             // draw shadow
136             $text_color = ImageColorAllocate($im, 175, 175, 175);
137             // shadow is 1 pixel down and 2 pixels right
138             ImageTTFText($im, 10, 0, 12, 31, $text_color, $ttfont, $text);
139
140             // draw text
141             $text_color = ImageColorAllocate($im, 0, 0, 0);
142             ImageTTFText($im, 10, 0, 10, 30, $text_color, $ttfont, $text);
143
144             /**
145              * An alternate text drawing method in case ImageTTFText
146              * doesn't work.
147              **/
148             //ImageString($im, 2, 10, 40, $text, $text_color);
149
150             // To dump directly to browser:
151             //header("Content-type: image/png");
152             //ImagePng($im);
153
154             // to save to file:
155             $success = ImagePng($im, $filepath . $filename);
156
157         } else {
158             $filepath .= "/";
159             $success = 2;
160         }
161
162         // create an <img src= tag to show the image!
163         $html = HTML();
164         if ($success > 0) {
165             if (defined('text2png_debug')) {
166                 switch($success) {
167                 case 1:
168                     trigger_error(sprintf(_("Image saved to cache file: %s"),
169                                           $filepath . $filename),
170                                   E_USER_NOTICE);
171                 case 2:
172                     trigger_error(sprintf(_("Image loaded from cache file: %s"),
173                                           $filepath . $filename),
174                                   E_USER_NOTICE);
175                 }
176             }
177             $url = "images/$l/$filename";
178             if (defined('DATA_PATH'))
179                 $url = DATA_PATH . "/$url";
180             $html->pushContent(HTML::img(array('src' => $url,
181                                                'alt' => $text)));
182         } else {
183             trigger_error(sprintf(_("couldn't open file '%s' for writing"),
184                                   $filepath . $filename), E_USER_NOTICE);
185         }
186         return $html;
187     }
188 };
189
190 // $Log: not supported by cvs2svn $
191 // Revision 1.12  2003/02/22 19:21:47  dairiki
192 // If DATA_PATH is not defined (by user in index.php), then use
193 // relative URLs to data.
194 //
195 // Revision 1.11  2003/01/18 22:08:01  carstenklapp
196 // Code cleanup:
197 // Reformatting & tabs to spaces;
198 // Added copyleft, getVersion, getDescription, rcs_id.
199 //
200
201 // For emacs users
202 // Local Variables:
203 // mode: php
204 // tab-width: 4
205 // c-basic-offset: 4
206 // c-hanging-comment-ender-p: nil
207 // indent-tabs-mode: nil
208 // End:
209 ?>