]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CacheTest.php
added SubPages support: see SUBPAGE_SEPERATOR in index.php
[SourceForge/phpwiki.git] / lib / plugin / CacheTest.php
1 <?php // -*-php-*-
2 // +---------------------------------------------------------------------+
3 // | CacheTest.php                                                       |
4 // +---------------------------------------------------------------------+
5 // | simple test of the WikiPluginCached class which provides a          |
6 // | text to image conversion.                                           |
7 // | This is a usage example of WikiPluginCached.                        |
8 // |                                                                     |
9 // | Copyright (C) 2002 Johannes Große (Johannes Gro&szlig;e)            |
10 // | You may copy this code freely under the conditions of the GPL       |
11 // +---------------------------------------------------------------------+
12
13 /*------------------------------------------------------------------------
14  | CacheTest            
15  *------------------------------------------------------------------------
16  |
17  | You may call this plugin as follows:
18  |
19  |        <?plugin CacheTest text="What a wonderful test!" ?>
20  |
21
22 /*-----------------------------------------------------------------------
23  |
24  |  Source
25  |
26  *----------------------------------------------------------------------*/
27
28 /*-----------------------------------------------------------------------
29  | WikiPlugin_CacheTest
30  *----------------------------------------------------------------------*/
31
32 require_once "lib/WikiPluginCached.php";
33
34 class WikiPlugin_CacheTest extends WikiPluginCached
35 {   
36     /* --------- overwrite virtual or abstract methods ---------------- */
37
38     function getPluginType() {
39         return PLUGIN_CACHED_IMG_ONDEMAND;
40     }
41
42     function getName() {
43         return "CacheTest";
44     }
45
46     function getDescription() {
47         return 'This is a simple example using WikiPluginCached.';
48     }
49
50     function getDefaultArguments() {
51         return array('text' => $this->getDescription(),
52                      'font' => '3',
53                      'type' => 'png' );
54     }
55    
56     // should return image handle
57     // if an error occurs you MUST call $this->complain('aboutwhichidocomplain')
58     // you may produce an image handle to an error image
59     // if you do not, WikiPluginImageCache will do so. 
60
61     function getImage($dbi, $argarray, $request) {
62         extract($argarray);
63         return $this->produceGraphics($text,$font);
64         
65         // This should also work
66         // return $this->lazy_produceGraphics($text,$font);
67     } // getImage
68    
69
70     function getImageType($dbi, $argarray, $request) {
71         extract($argarray);
72         if (in_array($type,array('png','gif','jpg'))) {
73             return $type; 
74         }
75         return 'png';
76     }
77
78     function getAlt($dbi, $argarray, $request) {
79         // ALT-text for <img> tag
80         extract($argarray); 
81         return $text; 
82     }
83
84     function getExpire($dbi, $argarray, $request) {
85         return '+600'; // 600 seconds life time
86     }
87     
88
89     /* -------------------- extremely simple converter -------------------- */
90
91
92     function produceGraphics($text, $font ) { 
93         // The idea (and some code) is stolen from the text2png plugin
94         // but I did not want to use TTF. ImageString is quite ugly
95         // and quite compatible. It's only a usage example. 
96
97         if ($font<1 || $font>5) {
98             $text = "Fontnr. (font=\"$font\") should be in range 1-5";
99             $this->complain($text);
100             $font = 3;         
101         } 
102
103         $ok = ($im = @ImageCreate(400, 40)); 
104         $bg_color    = ImageColorAllocate($im, 240, 240, 240);
105         $text_color1 = ImageColorAllocate($im, 120, 120, 120);
106         $text_color2 = ImageColorAllocate($im, 0, 0, 0);
107
108         ImageFilledRectangle($im, 0, 0, 149, 49, $bg_color);
109         ImageString($im, $font, 11, 12, $text, $text_color1);
110         ImageString($im, $font, 10, 10, $text, $text_color2); 
111
112         if (!$ok) {
113             // simple error handling by WikiPluginImageCache
114             $this->complain("Could not create image");
115             return false;
116         } 
117
118         // image creation takes really _much_ time :-)
119         // so caching is very useful!
120         sleep(4);
121
122         return $im;
123     } // produce_Graphics
124
125     /* -------------------------------------------------------------------- */
126
127     // we could have used the simple built-in text2img function
128     // instead of writing our own:
129
130     function lazy_produceGraphics( $text, $font ) {
131         if ($font<1 || $font>5) {
132             $text = "Fontnr. (font=\"$font\") should be in range 1-5";
133             $this->complain($text);
134             $font = 3;         
135         } 
136  
137         return $this->text2img($text,$font,array(0,0,0),array(255,255,255));
138     } // lazy_produceGraphics
139
140 } // WikiPlugin_CacheTest
141
142
143
144 // For emacs users
145 // Local Variables:
146 // mode: php
147 // tab-width: 4
148 // c-basic-offset: 4
149 // c-hanging-comment-ender-p: nil
150 // indent-tabs-mode: nil
151 // End:
152 ?>