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