]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/text2png.php
Updated to use ErrorManager
[SourceForge/phpwiki.git] / lib / plugin / text2png.php
1 <?php // -*-php-*-
2
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       * You MUST delete the image cache yourself in /images if you change the
33       * drawing routines!
34       */
35
36         $filename = $text . ".png";
37
38      /* FIXME: need something more elegant, and a way to gettext a different
39       * language depending on any individual user's locale preferences.
40       */
41         if ($l == "C") { $l = "en"; } //english=C
42                 $filepath = getcwd() . "/images/$l";
43  
44         if (!file_exists($filepath ."/". $filename)) {
45             
46             if (!file_exists($filepath)) {
47                             $oldumask = umask(0);
48              // permissions affected by user the www server is running as
49                 mkdir($filepath, 0777);
50                 umask($oldumask);
51             }
52             
53          // add trailing slash to save some keystrokes later
54             $filepath .= "/";
55             
56          /* prepare a new image
57           * FIXME: needs a dynamic image size depending on text width and height
58           * $im = @ImageCreate(150, 50);
59           */
60             if (empty($im)) {
61                 $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.");
62              // FIXME: Error manager does not transform URLs passed through it.
63                 $error_text .= QElement('a', array('href' => "http://www.php.net/manual/en/function.imagecreate.php",
64                                         'class' => 'rawurl'), "PHP web page");
65                 trigger_error( $error_text, E_USER_NOTICE );
66             }
67          // get ready to draw
68             $bg_color = ImageColorAllocate($im, 255, 255, 255);
69             $ttfont   = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/LucidaSansRegular.ttf";
70
71          /* http://download.php.net/manual/en/function.imagettftext.php
72           * array imagettftext (int im, int size, int angle, int x, int y, int col, 
73           *                     string fontfile, string text)
74           */
75
76          // draw shadow
77             $text_color = ImageColorAllocate($im, 175, 175, 175);
78          // shadow is 1 pixel down and 2 pixels right
79             ImageTTFText($im, 10, 0, 12, 31, $text_color, $ttfont, $text);
80
81          // draw text
82             $text_color = ImageColorAllocate($im, 0, 0, 0);
83             ImageTTFText($im, 10, 0, 10, 30, $text_color, $ttfont, $text);
84
85          /* An alternate text drawing method in case ImageTTFText
86           * doesn't work.
87           */
88             #ImageString($im, 2, 10, 40, $text, $text_color);
89
90          // To dump directly to browser:
91             #header("Content-type: image/png");
92             #ImagePng($im);
93
94          // to save to file:
95             $success = ImagePng($im, $filepath . $filename);
96
97         } else {
98             $filepath .= "/";
99             $success = 2;
100         }
101
102      // create an <img src= tag to show the image!
103         $html = "";
104         if ($success > 0) {
105             if (defined('text2png_debug')) {
106                 switch($success) { 
107                 case 1:
108                     trigger_error(sprintf(_("Image saved to cache file: %s"),
109                                         $filepath . $filename), E_USER_NOTICE);
110                 case 2:
111                     trigger_error(sprintf(_("Image loaded from cache file: %s"),
112                                         $filepath . $filename), E_USER_NOTICE);
113                 }
114             }
115             $urlpath = DATA_PATH . "/images/$l/";
116             $html .= Element('img', array('src' => $urlpath . $filename, 'alt' => $text));
117         } else {
118             trigger_error(sprintf(_("couldn't open file '%s' for writing"),
119                                         $filepath . $filename), E_USER_NOTICE);
120         }
121     return $html;
122     }
123 };
124
125 // For emacs users
126 // Local Variables:
127 // mode: php
128 // tab-width: 4
129 // c-basic-offset: 4
130 // c-hanging-comment-ender-p: nil
131 // indent-tabs-mode: nil
132 // End:
133 ?>