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