File: CacheTest.php

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (167 lines) | stat: -rw-r--r-- 5,282 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php // -*-php-*-
rcs_id('$Id: CacheTest.php,v 1.2 2003/01/18 21:19:25 carstenklapp Exp $');
// +---------------------------------------------------------------------+
// | CacheTest.php                                                       |
// +---------------------------------------------------------------------+
// | simple test of the WikiPluginCached class which provides a          |
// | text to image conversion.                                           |
// | This is a usage example of WikiPluginCached.                        |
// |                                                                     |
// | Copyright (C) 2002 Johannes Groe (Johannes Gro&szlig;e)            |
// | You may copy this code freely under the conditions of the GPL       |
// +---------------------------------------------------------------------+

/*------------------------------------------------------------------------
 | CacheTest
 *------------------------------------------------------------------------
 |
 | You may call this plugin as follows:
 |
 |        <?plugin CacheTest text="What a wonderful test!" ?>
 |

/*-----------------------------------------------------------------------
 |
 |  Source
 |
 *----------------------------------------------------------------------*/

/*-----------------------------------------------------------------------
 | WikiPlugin_CacheTest
 *----------------------------------------------------------------------*/

require_once "lib/WikiPluginCached.php";

class WikiPlugin_CacheTest
extends WikiPluginCached
{
    /* --------- overwrite virtual or abstract methods ---------------- */

    function getPluginType() {
        return PLUGIN_CACHED_IMG_ONDEMAND;
    }

    function getName() {
        return "CacheTest";
    }

    function getDescription() {
        return 'This is a simple example using WikiPluginCached.';
    }

    function getVersion() {
        return preg_replace("/[Revision: $]/", '',
                            "\$Revision: 1.2 $");
    }

    function getDefaultArguments() {
        return array('text' => $this->getDescription(),
                     'font' => '3',
                     'type' => 'png' );
    }

    // should return image handle
    //
    // if an error occurs you MUST call
    // $this->complain('aboutwhichidocomplain') you may produce an
    // image handle to an error image if you do not,
    // WikiPluginImageCache will do so.

    function getImage($dbi, $argarray, $request) {
        extract($argarray);
        return $this->produceGraphics($text,$font);

        // This should also work
        // return $this->lazy_produceGraphics($text,$font);
    } // getImage


    function getImageType($dbi, $argarray, $request) {
        extract($argarray);
        if (in_array($type, array('png', 'gif', 'jpg'))) {
            return $type;
        }
        return 'png';
    }

    function getAlt($dbi, $argarray, $request) {
        // ALT-text for <img> tag
        extract($argarray);
        return $text;
    }

    function getExpire($dbi, $argarray, $request) {
        return '+600'; // 600 seconds life time
    }


    /* -------------------- extremely simple converter -------------------- */


    function produceGraphics($text, $font ) {
        // The idea (and some code) is stolen from the text2png plugin
        // but I did not want to use TTF. ImageString is quite ugly
        // and quite compatible. It's only a usage example.

        if ($font<1 || $font>5) {
            $text = "Fontnr. (font=\"$font\") should be in range 1-5";
            $this->complain($text);
            $font = 3;
        }

        $ok = ($im = @ImageCreate(400, 40));
        $bg_color    = ImageColorAllocate($im, 240, 240, 240);
        $text_color1 = ImageColorAllocate($im, 120, 120, 120);
        $text_color2 = ImageColorAllocate($im, 0, 0, 0);

        ImageFilledRectangle($im, 0, 0, 149, 49, $bg_color);
        ImageString($im, $font, 11, 12, $text, $text_color1);
        ImageString($im, $font, 10, 10, $text, $text_color2);

        if (!$ok) {
            // simple error handling by WikiPluginImageCache
            $this->complain("Could not create image");
            return false;
        }

        // image creation takes really _much_ time :-)
        // so caching is very useful!
        sleep(4);

        return $im;
    } // produce_Graphics

    /* -------------------------------------------------------------------- */

    // we could have used the simple built-in text2img function
    // instead of writing our own:

    function lazy_produceGraphics( $text, $font ) {
        if ($font<1 || $font>5) {
            $text = "Fontnr. (font=\"$font\") should be in range 1-5";
            $this->complain($text);
            $font = 3;

        }

        return $this->text2img($text, $font, array(0, 0, 0),
                               array(255, 255, 255));
    } // lazy_produceGraphics

} // WikiPlugin_CacheTest

// $Log: CacheTest.php,v $
// Revision 1.2  2003/01/18 21:19:25  carstenklapp
// Code cleanup:
// Reformatting; added copyleft, getVersion, getDescription
//

// For emacs users
// Local Variables:
// mode: php
// tab-width: 4
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>