]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/text2png.php
Minor reindenting & rewrapping.
[SourceForge/phpwiki.git] / lib / plugin / text2png.php
1 <?php // -*-php-*-
2 rcs_id('$Id: text2png.php,v 1.8 2002-01-09 18:06:49 carstenklapp Exp $');
3 /**
4  * File loading and saving diagnostic messages, to see whether an
5  * image was saved to or loaded from the cache and what the path is
6  */
7 define('text2png_debug', true);
8
9
10 class WikiPlugin_text2png
11 extends WikiPlugin
12 {
13     function getName () {
14         return "text2png";
15     }
16
17     function getDefaultArguments() {
18         global $LANG;
19         return array('text' => "Hello WikiWorld!",
20                      'l'    => $LANG );
21         }
22
23     function run($dbi, $argstr, $request) {
24         extract($this->getArgs($argstr, $request));
25         return $this->text2png($text,$l);
26     }
27
28     function text2png($text,$l) {
29
30         /**
31          * Basic image creation and caching
32          *
33          * You MUST delete the image cache yourself in /images if you
34          * change the drawing routines!
35          */
36
37         $filename = $text . ".png";
38
39         /**
40          * FIXME: need something more elegant, and a way to gettext a
41          *        different language depending on any individual
42          *        user's locale preferences.
43          */
44
45         if ($l == "C") { $l = "en"; } //english=C
46         $filepath = getcwd() . "/images/$l";
47
48         if (!file_exists($filepath ."/". $filename)) {
49
50             if (!file_exists($filepath)) {
51                 $oldumask = umask(0);
52                 // permissions affected by user the www server is running as
53                 mkdir($filepath, 0777);
54                 umask($oldumask);
55             }
56
57             // add trailing slash to save some keystrokes later
58             $filepath .= "/";
59
60             /**
61              * prepare a new image
62              *
63              * FIXME: needs a dynamic image size depending on text
64              *        width and height $im = @ImageCreate(150, 50);
65              */
66
67             if (empty($im)) {
68                 $error_text = _("Unable to create a new GD image stream. PHP must be compiled with support for the GD library version 1.6 or later to create PNG image files.");
69                 // FIXME: Error manager does not transform URLs passed
70                 //        through it.
71                 $error_text .= QElement('a',
72                                         array('href'  => "http://www.php.net/manual/en/function.imagecreate.php",
73                                               'class' => 'rawurl'),
74                                         "PHP web page");
75                 trigger_error( $error_text, E_USER_NOTICE );
76             }
77             // get ready to draw
78             $bg_color = ImageColorAllocate($im, 255, 255, 255);
79             $ttfont   = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/LucidaSansRegular.ttf";
80
81             /* http://download.php.net/manual/en/function.imagettftext.php
82              * array imagettftext (int im, int size, int angle, int x, int y,
83              *                      int col, string fontfile, string text)
84              */
85
86             // draw shadow
87             $text_color = ImageColorAllocate($im, 175, 175, 175);
88             // shadow is 1 pixel down and 2 pixels right
89             ImageTTFText($im, 10, 0, 12, 31, $text_color, $ttfont, $text);
90
91             // draw text
92             $text_color = ImageColorAllocate($im, 0, 0, 0);
93             ImageTTFText($im, 10, 0, 10, 30, $text_color, $ttfont, $text);
94
95             /**
96              * An alternate text drawing method in case ImageTTFText
97              * doesn't work.
98              **/
99             //ImageString($im, 2, 10, 40, $text, $text_color);
100
101             // To dump directly to browser:
102             //header("Content-type: image/png");
103             //ImagePng($im);
104
105             // to save to file:
106             $success = ImagePng($im, $filepath . $filename);
107
108         } else {
109             $filepath .= "/";
110             $success = 2;
111         }
112
113         // create an <img src= tag to show the image!
114         $html = "";
115         if ($success > 0) {
116             if (defined('text2png_debug')) {
117                 switch($success) {
118                 case 1:
119                     trigger_error(sprintf(_("Image saved to cache file: %s"),
120                                           $filepath . $filename),
121                                   E_USER_NOTICE);
122                 case 2:
123                     trigger_error(sprintf(_("Image loaded from cache file: %s"),
124                                           $filepath . $filename),
125                                   E_USER_NOTICE);
126                 }
127             }
128             $urlpath = DATA_PATH . "/images/$l/";
129             $html .= Element('img', array('src' => $urlpath . $filename,
130                                           'alt' => $text));
131         } else {
132             trigger_error(sprintf(_("couldn't open file '%s' for writing"),
133                                   $filepath . $filename), E_USER_NOTICE);
134         }
135         return $html;
136     }
137 };
138
139 // For emacs users
140 // Local Variables:
141 // mode: php
142 // tab-width: 4
143 // c-basic-offset: 4
144 // c-hanging-comment-ender-p: nil
145 // indent-tabs-mode: nil
146 // End:
147 ?>