]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/text2png.php
Moved description from html template into plugins.
[SourceForge/phpwiki.git] / lib / plugin / text2png.php
1 <?php // -*-php-*-
2
3 define('text2png_debug', true);
4
5
6 class WikiPlugin_text2png
7 extends WikiPlugin
8 {
9         var $name = 'text2png';
10         var $description = 'text2png';
11         
12         function getDefaultArguments() {
13     global $LANG;
14         return array('text'     => "Hello WikiWorld!",
15                      'l'    => $LANG );
16         }
17
18         function run($dbi, $argstr, $request) {
19                 extract($this->getArgs($argstr, $request));
20                 return $this->text2png($text,$l);
21         }
22
23         function text2png($text,$l) {
24
25         //basic image creation and caching
26                 //you MUST delete the image cache yourself if you change the drawing routines!
27                 
28         //uncomment debug string above to see whether image was saved to or loaded from cache
29                 //and what the path is.
30                 
31         //locale test
32                 //http://download.php.net/manual/en/function.dcgettext.php
33                 //dcgettext and dgettext aren't available functions on my system.?? -carsten
34                 //this doesn't seem to work anyway, always get english. ??
35         //$oldlang=$LANG;
36                 //putenv("LANG=$l");
37         //$LANG=$l;
38         //if (!$l == "C") {include("locale/$l/LC_MESSAGES/phpwiki.php");}
39                 //$text = gettext($text);
40         //putenv("LANG=$oldlang");
41                 
42                 $filename = $text . ".png";
43                 
44         if ($l == "C") { $l = "en"; } //FIXME: hack for english, C=en ??
45                 $filepath = getcwd() . "/images/$l";
46  
47         if (!file_exists($filepath ."/". $filename)) {
48             
49             if (!file_exists($filepath)) {
50                             $oldumask = umask(0);
51                 mkdir($filepath, 0777);    //permissions affected by user the www server is running as
52                 umask($oldumask);
53             }
54             
55                         // add trailing slash to save some keystrokes later
56             $filepath .= "/";
57             
58                         // prepare a new image
59             $im = @ImageCreate(150, 50) or die ("Cannot Initialize new GD image stream. PHP must be compiled with support for GD 1.6 or later to create png files.");
60
61             // get ready to draw
62                     $bg_color   = ImageColorAllocate($im, 255, 255, 255);
63                     $ttfont     = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/LucidaSansRegular.ttf";
64
65             // http://download.php.net/manual/en/function.imagettftext.php
66                         // array imagettftext (int im, int size, int angle, int x, int y, int col, string fontfile, string text)
67
68             //draw shadow
69                     $text_color = ImageColorAllocate($im, 175, 175, 175);
70             //shadow is 1 pixel down and 2 pixels right
71                     ImageTTFText($im, 10, 0, 12, 31, $text_color, $ttfont, $text);
72             //draw text
73                     $text_color = ImageColorAllocate($im, 0, 0, 0);
74                     ImageTTFText($im, 10, 0, 10, 30, $text_color, $ttfont, $text);
75
76             //maybe an alternate text drawing method in case ImageTTFText doesn't work
77                     //ImageString($im, 2, 10, 40, $text, $text_color);
78
79                     // to dump directly to browser:
80                     //header("Content-type: image/png");
81                     //ImagePng($im);
82
83                     // to save to file:
84             $success = ImagePng($im, $filepath . $filename);
85
86         } else {
87             $filepath .= "/";
88             $success = 2;
89         }
90
91         // create an <img src= tag to show the image!
92         // this could use some better error reporting
93                 $html = "";
94                 if ($success > 0) {
95             if (defined('text2png_debug')) {
96                    switch($success) { 
97                    case 1:
98                         $html .= Element('p', "Image saved to cache file: " . $filepath . $filename) . "\n" ;
99                    case 2:
100                         $html .= Element('p', "Image loaded from cache file: " . $filepath . $filename) . "\n" ;
101                      }
102             }
103             $urlpath = DATA_PATH . "/images/$l/";
104             $html .= Element('img', array('src' => $urlpath . $filename, 'alt' => $text));
105                 } else {
106                         $html .= Element('p', "Error writing png file: " . $filepath . $filename) . "\n";
107                 }
108                 return $html;
109         }
110 };
111
112 // For emacs users
113 // Local Variables:
114 // mode: php
115 // tab-width: 4
116 // c-basic-offset: 4
117 // c-hanging-comment-ender-p: nil
118 // indent-tabs-mode: nil
119 // End:
120 ?>