]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/text2png.php
text2png plug now really works! (accepts a text= argument, and does cache the image...
[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         //to show debug string, whether image was saved or loaded from cache
28                 //and what the path is
29         
30                 $filename = $text . ".png";
31         if ($l == "C") { $l = "en"; } //FIXME: hack for english, C=en
32                 $filepath = getcwd() . "/images/$l";
33  
34         if (!file_exists($filepath ."/". $filename)) {
35             
36             if (!file_exists($filepath)) {
37                             $oldumask = umask(0);
38                 mkdir($filepath, 0777);    //permissions affected by user the www server is running as
39                 umask($oldumask);
40             }
41             
42                         // add trailing slash to save some keystrokes later
43             $filepath .= "/";
44             
45                         // prepare a new image
46             $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.");
47
48             // get ready to draw
49                     $bg_color   = ImageColorAllocate($im, 255, 255, 255);
50                     $ttfont     = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/LucidaSansRegular.ttf";
51
52             // http://download.php.net/manual/en/function.imagettftext.php
53                         // array imagettftext (int im, int size, int angle, int x, int y, int col, string fontfile, string text)
54
55             //draw shadow
56                     $text_color = ImageColorAllocate($im, 175, 175, 175);
57             //shadow is 1 pixel down and 2 pixels right
58                     ImageTTFText($im, 10, 0, 12, 31, $text_color, $ttfont, $text);
59             //draw text
60                     $text_color = ImageColorAllocate($im, 0, 0, 0);
61                     ImageTTFText($im, 10, 0, 10, 30, $text_color, $ttfont, $text);
62                     //ImageString($im, 2, 10, 40, $text, $text_color);
63
64                     // to dump directly to browser:
65                     //header("Content-type: image/png");
66                     //ImagePng($im);
67
68                     // to save to file:
69             $success = ImagePng($im, $filepath . $filename);
70
71         } else {
72             $filepath .= "/";
73             $success = 2;
74         }
75
76         // create an <img src= tag to show the image!
77         // this could use some better error reporting
78                 $html = "";
79                 if ($success > 0) {
80             if (defined('text2png_debug')) {
81                    switch($success) { 
82                    case 1:
83                         $html .= Element('p', "Image saved to cache file: " . $filepath . $filename) . "\n" ;
84                    case 2:
85                         $html .= Element('p', "Image loaded from cache file: " . $filepath . $filename) . "\n" ;
86                      }
87             }
88             $urlpath = DATA_PATH . "/images/$l/";
89             $html .= Element('img', array('src' => $urlpath . $filename, 'alt' => $text));
90                 } else {
91                         $html .= Element('p', "Error writing png file: " . $filepath . $filename) . "\n";
92                 }
93                 return $html;
94         }
95 };
96
97 // For emacs users
98 // Local Variables:
99 // mode: php
100 // tab-width: 4
101 // c-basic-offset: 4
102 // c-hanging-comment-ender-p: nil
103 // indent-tabs-mode: nil
104 // End:
105 ?>