3 * Copyright © 2002 Johannes Große
5 * This file is part of PhpWiki.
7 * PhpWiki is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * PhpWiki is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * SPDX-License-Identifier: GPL-2.0-or-later
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 // +---------------------------------------------------------------------+
31 /*------------------------------------------------------------------------
33 | You may call this plugin as follows:
35 | <<CacheTest text="What a wonderful test!" >>
37 *----------------------------------------------------------------------*/
39 require_once 'lib/WikiPluginCached.php';
41 class WikiPlugin_CacheTest
42 extends WikiPluginCached
44 /* --------- overwrite abstract methods ---------------- */
46 function getPluginType()
48 return PLUGIN_CACHED_IMG_ONDEMAND;
51 function getDescription()
53 return _('This is a simple example using WikiPluginCached.');
56 function getDefaultArguments()
58 return array('text' => $this->getDescription(),
63 // should return image handle
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.
70 protected function getImage($dbi, $argarray, $request)
73 return $this->produceGraphics($text, $font);
75 // This should also work
76 // return $this->lazy_produceGraphics($text,$font);
79 protected function getMap($dbi, $argarray, $request)
81 trigger_error('pure virtual', E_USER_ERROR);
84 protected function getHtml($dbi, $argarray, $request, $basepage)
86 trigger_error('pure virtual', E_USER_ERROR);
89 function getImageType($dbi, $argarray, $request)
92 if (in_array($type, array('png', 'gif', 'jpg'))) {
98 function getAlt($dbi, $argarray, $request)
100 // ALT-text for <img> tag
105 function getExpire($dbi, $argarray, $request)
107 return '+600'; // 600 seconds life time
110 /* -------------------- extremely simple converter -------------------- */
112 function produceGraphics($text, $font)
114 // The idea (and some code) is stolen from the text2png plugin
115 // but I did not want to use TTF. Imagestring is quite ugly
116 // and quite compatible. It's only a usage example.
118 if ($font < 1 || $font > 5) {
119 $text = "Fontnr. (font=\"$font\") should be in range 1-5";
120 $this->complain($text);
124 $ok = ($im = @imagecreate(400, 40));
125 $bg_color = imagecolorallocate($im, 240, 240, 240);
126 $text_color1 = imagecolorallocate($im, 120, 120, 120);
127 $text_color2 = imagecolorallocate($im, 0, 0, 0);
129 imagefilledrectangle($im, 0, 0, 149, 49, $bg_color);
130 imagestring($im, $font, 11, 12, $text, $text_color1);
131 imagestring($im, $font, 10, 10, $text, $text_color2);
134 // simple error handling by WikiPluginImageCache
135 $this->complain("Could not create image");
139 // image creation takes really _much_ time :-)
140 // so caching is very useful!
144 } // produce_Graphics
146 /* -------------------------------------------------------------------- */
148 // we could have used the simple built-in text2img function
149 // instead of writing our own:
151 function lazy_produceGraphics($text, $font)
153 if ($font < 1 || $font > 5) {
154 $text = "Fontnr. (font=\"$font\") should be in range 1-5";
155 $this->complain($text);
160 return $this->text2img($text, $font, array(0, 0, 0),
161 array(255, 255, 255));
162 } // lazy_produceGraphics