]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CacheTest.php
private --> protected
[SourceForge/phpwiki.git] / lib / plugin / CacheTest.php
1 <?php
2
3 /*
4  * Copyright (C) 2002 Johannes Große
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 // +---------------------------------------------------------------------+
24 // | CacheTest.php                                                       |
25 // +---------------------------------------------------------------------+
26 // | simple test of the WikiPluginCached class which provides a          |
27 // | text to image conversion.                                           |
28 // | This is a usage example of WikiPluginCached.                        |
29 // |                                                                     |
30 // | You may copy this code freely under the conditions of the GPL       |
31 // +---------------------------------------------------------------------+
32
33 /*------------------------------------------------------------------------
34  | CacheTest
35  *------------------------------------------------------------------------
36  |
37  | You may call this plugin as follows:
38  |
39  |        <<CacheTest text="What a wonderful test!" >>
40  |
41
42 /*-----------------------------------------------------------------------
43  |
44  |  Source
45  |
46  *----------------------------------------------------------------------*/
47
48 /*-----------------------------------------------------------------------
49  | WikiPlugin_CacheTest
50  *----------------------------------------------------------------------*/
51
52 require_once 'lib/WikiPluginCached.php';
53
54 class WikiPlugin_CacheTest
55     extends WikiPluginCached
56 {
57     /* --------- overwrite virtual or abstract methods ---------------- */
58
59     function getPluginType()
60     {
61         return PLUGIN_CACHED_IMG_ONDEMAND;
62     }
63
64     function getName()
65     {
66         return "CacheTest";
67     }
68
69     function getDescription()
70     {
71         return 'This is a simple example using WikiPluginCached.';
72     }
73
74     function getDefaultArguments()
75     {
76         return array('text' => $this->getDescription(),
77             'font' => '3',
78             'type' => 'png');
79     }
80
81     // should return image handle
82     //
83     // if an error occurs you MUST call
84     // $this->complain('aboutwhichidocomplain') you may produce an
85     // image handle to an error image if you do not,
86     // WikiPluginImageCache will do so.
87
88     function getImage($dbi, $argarray, $request)
89     {
90         extract($argarray);
91         return $this->produceGraphics($text, $font);
92
93         // This should also work
94         // return $this->lazy_produceGraphics($text,$font);
95     } // getImage
96
97     function getImageType($dbi, $argarray, $request)
98     {
99         extract($argarray);
100         if (in_array($type, array('png', 'gif', 'jpg'))) {
101             return $type;
102         }
103         return 'png';
104     }
105
106     function getAlt($dbi, $argarray, $request)
107     {
108         // ALT-text for <img> tag
109         extract($argarray);
110         return $text;
111     }
112
113     function getExpire($dbi, $argarray, $request)
114     {
115         return '+600'; // 600 seconds life time
116     }
117
118     /* -------------------- extremely simple converter -------------------- */
119
120     function produceGraphics($text, $font)
121     {
122         // The idea (and some code) is stolen from the text2png plugin
123         // but I did not want to use TTF. ImageString is quite ugly
124         // and quite compatible. It's only a usage example.
125
126         if ($font < 1 || $font > 5) {
127             $text = "Fontnr. (font=\"$font\") should be in range 1-5";
128             $this->complain($text);
129             $font = 3;
130         }
131
132         $ok = ($im = @ImageCreate(400, 40));
133         $bg_color = ImageColorAllocate($im, 240, 240, 240);
134         $text_color1 = ImageColorAllocate($im, 120, 120, 120);
135         $text_color2 = ImageColorAllocate($im, 0, 0, 0);
136
137         ImageFilledRectangle($im, 0, 0, 149, 49, $bg_color);
138         ImageString($im, $font, 11, 12, $text, $text_color1);
139         ImageString($im, $font, 10, 10, $text, $text_color2);
140
141         if (!$ok) {
142             // simple error handling by WikiPluginImageCache
143             $this->complain("Could not create image");
144             return false;
145         }
146
147         // image creation takes really _much_ time :-)
148         // so caching is very useful!
149         sleep(4);
150
151         return $im;
152     } // produce_Graphics
153
154     /* -------------------------------------------------------------------- */
155
156     // we could have used the simple built-in text2img function
157     // instead of writing our own:
158
159     function lazy_produceGraphics($text, $font)
160     {
161         if ($font < 1 || $font > 5) {
162             $text = "Fontnr. (font=\"$font\") should be in range 1-5";
163             $this->complain($text);
164             $font = 3;
165
166         }
167
168         return $this->text2img($text, $font, array(0, 0, 0),
169             array(255, 255, 255));
170     } // lazy_produceGraphics
171
172 }
173
174 // Local Variables:
175 // mode: php
176 // tab-width: 8
177 // c-basic-offset: 4
178 // c-hanging-comment-ender-p: nil
179 // indent-tabs-mode: nil
180 // End: