]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CacheTest.php
Replace tabs by spaces; remove EOL spaces
[SourceForge/phpwiki.git] / lib / plugin / CacheTest.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
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
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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  |        <?plugin 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         return PLUGIN_CACHED_IMG_ONDEMAND;
61     }
62
63     function getName() {
64         return "CacheTest";
65     }
66
67     function getDescription() {
68         return 'This is a simple example using WikiPluginCached.';
69     }
70
71     function getVersion() {
72         return preg_replace("/[Revision: $]/", '',
73                             "\$Revision$");
74     }
75
76     function getDefaultArguments() {
77         return array('text' => $this->getDescription(),
78                      'font' => '3',
79                      'type' => 'png' );
80     }
81
82     // should return image handle
83     //
84     // if an error occurs you MUST call
85     // $this->complain('aboutwhichidocomplain') you may produce an
86     // image handle to an error image if you do not,
87     // WikiPluginImageCache will do so.
88
89     function getImage($dbi, $argarray, $request) {
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
98     function getImageType($dbi, $argarray, $request) {
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         // ALT-text for <img> tag
108         extract($argarray);
109         return $text;
110     }
111
112     function getExpire($dbi, $argarray, $request) {
113         return '+600'; // 600 seconds life time
114     }
115
116
117     /* -------------------- extremely simple converter -------------------- */
118
119
120     function produceGraphics($text, $font ) {
121         // The idea (and some code) is stolen from the text2png plugin
122         // but I did not want to use TTF. ImageString is quite ugly
123         // and quite compatible. It's only a usage example.
124
125         if ($font<1 || $font>5) {
126             $text = "Fontnr. (font=\"$font\") should be in range 1-5";
127             $this->complain($text);
128             $font = 3;
129         }
130
131         $ok = ($im = @ImageCreate(400, 40));
132         $bg_color    = ImageColorAllocate($im, 240, 240, 240);
133         $text_color1 = ImageColorAllocate($im, 120, 120, 120);
134         $text_color2 = ImageColorAllocate($im, 0, 0, 0);
135
136         ImageFilledRectangle($im, 0, 0, 149, 49, $bg_color);
137         ImageString($im, $font, 11, 12, $text, $text_color1);
138         ImageString($im, $font, 10, 10, $text, $text_color2);
139
140         if (!$ok) {
141             // simple error handling by WikiPluginImageCache
142             $this->complain("Could not create image");
143             return false;
144         }
145
146         // image creation takes really _much_ time :-)
147         // so caching is very useful!
148         sleep(4);
149
150         return $im;
151     } // produce_Graphics
152
153     /* -------------------------------------------------------------------- */
154
155     // we could have used the simple built-in text2img function
156     // instead of writing our own:
157
158     function lazy_produceGraphics( $text, $font ) {
159         if ($font<1 || $font>5) {
160             $text = "Fontnr. (font=\"$font\") should be in range 1-5";
161             $this->complain($text);
162             $font = 3;
163
164         }
165
166         return $this->text2img($text, $font, array(0, 0, 0),
167                                array(255, 255, 255));
168     } // lazy_produceGraphics
169
170 } // WikiPlugin_CacheTest
171
172 // For emacs users
173 // Local Variables:
174 // mode: php
175 // tab-width: 4
176 // c-basic-offset: 4
177 // c-hanging-comment-ender-p: nil
178 // indent-tabs-mode: nil
179 // End:
180 ?>