]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CacheTest.php
getName should not translate
[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 getDescription()
65     {
66         return _('This is a simple example using WikiPluginCached.');
67     }
68
69     function getDefaultArguments()
70     {
71         return array('text' => $this->getDescription(),
72             'font' => '3',
73             'type' => 'png');
74     }
75
76     // should return image handle
77     //
78     // if an error occurs you MUST call
79     // $this->complain('aboutwhichidocomplain') you may produce an
80     // image handle to an error image if you do not,
81     // WikiPluginImageCache will do so.
82
83     function getImage($dbi, $argarray, $request)
84     {
85         extract($argarray);
86         return $this->produceGraphics($text, $font);
87
88         // This should also work
89         // return $this->lazy_produceGraphics($text,$font);
90     } // getImage
91
92     function getImageType($dbi, $argarray, $request)
93     {
94         extract($argarray);
95         if (in_array($type, array('png', 'gif', 'jpg'))) {
96             return $type;
97         }
98         return 'png';
99     }
100
101     function getAlt($dbi, $argarray, $request)
102     {
103         // ALT-text for <img> tag
104         extract($argarray);
105         return $text;
106     }
107
108     function getExpire($dbi, $argarray, $request)
109     {
110         return '+600'; // 600 seconds life time
111     }
112
113     /* -------------------- extremely simple converter -------------------- */
114
115     function produceGraphics($text, $font)
116     {
117         // The idea (and some code) is stolen from the text2png plugin
118         // but I did not want to use TTF. ImageString is quite ugly
119         // and quite compatible. It's only a usage example.
120
121         if ($font < 1 || $font > 5) {
122             $text = "Fontnr. (font=\"$font\") should be in range 1-5";
123             $this->complain($text);
124             $font = 3;
125         }
126
127         $ok = ($im = @ImageCreate(400, 40));
128         $bg_color = ImageColorAllocate($im, 240, 240, 240);
129         $text_color1 = ImageColorAllocate($im, 120, 120, 120);
130         $text_color2 = ImageColorAllocate($im, 0, 0, 0);
131
132         ImageFilledRectangle($im, 0, 0, 149, 49, $bg_color);
133         ImageString($im, $font, 11, 12, $text, $text_color1);
134         ImageString($im, $font, 10, 10, $text, $text_color2);
135
136         if (!$ok) {
137             // simple error handling by WikiPluginImageCache
138             $this->complain("Could not create image");
139             return false;
140         }
141
142         // image creation takes really _much_ time :-)
143         // so caching is very useful!
144         sleep(4);
145
146         return $im;
147     } // produce_Graphics
148
149     /* -------------------------------------------------------------------- */
150
151     // we could have used the simple built-in text2img function
152     // instead of writing our own:
153
154     function lazy_produceGraphics($text, $font)
155     {
156         if ($font < 1 || $font > 5) {
157             $text = "Fontnr. (font=\"$font\") should be in range 1-5";
158             $this->complain($text);
159             $font = 3;
160
161         }
162
163         return $this->text2img($text, $font, array(0, 0, 0),
164             array(255, 255, 255));
165     } // lazy_produceGraphics
166
167 }
168
169 // Local Variables:
170 // mode: php
171 // tab-width: 8
172 // c-basic-offset: 4
173 // c-hanging-comment-ender-p: nil
174 // indent-tabs-mode: nil
175 // End: