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