1<?php
2/**
3 * The following example demonstrates custom transcoders to show have you can
4 * encoder specialized kinds of objects, or store data in specialized formats
5 * to the server, rather than using the default methods as defined by the SDK.
6 *
7 * To see a more thorough but complex example of transcoder functions, please
8 * see the Couchbase.class.php at the root of the SDK source tree, which
9 * contains the default transcoders.
10 *
11 * NOTE: When using custom transcoders, it is unlikely that the default
12 * transcoders will work against the resulting document.  Thus, if you use a
13 * custom encoder to store data, you must use the respective decoder for correct
14 * behaviour.
15 */
16
17/*
18 * Create a new Cluster object to represent the connection to our
19 * cluster and specify any needed options such as SSL.
20 */
21$cb = new \Couchbase\Cluster('couchbase://localhost');
22
23/*
24 * We open the default bucket to store our cached data in.
25 */
26$db = $cb->openBucket('default');
27
28/*
29 * Some flags for differentiating what kind of data is stored so our
30 * decoder knows what to do with it.
31 */
32define('CBTE_FLAG_IMG', 1);
33define('CBTE_FLAG_JSON', 2);
34
35/*
36 * Lets define some custom transcoding functions.  For this example, any
37 * image types that are stored will be serialized as a PNG, and all other
38 * object types will be encoded as JSON.
39 */
40function example_encoder($value) {
41    if (gettype($value) == 'resource' && get_resource_type($value) == 'gd') {
42        // This is am image, lets capture the PNG data!
43        ob_start();
44        imagepng($value);
45        $png_data = ob_get_contents();
46        ob_end_clean();
47
48        // Return our bytes and flags
49        return array($png_data, CBTE_FLAG_IMG, 0);
50    } else {
51        // This is an arbitrary type, lets JSON encode it
52        $json_data = json_encode($value);
53
54        // Return our bytes and flags
55        return array($json_data, CBTE_FLAG_JSON, 0);
56    }
57}
58function example_decoder($bytes, $flags, $datatype) {
59    if ($flags == CBTE_FLAG_IMG) {
60        // Recreate our image object from the stored data
61        return imagecreatefromstring($bytes);
62    } else if ($flags == CBTE_FLAG_JSON) {
63        // Simply JSON decode
64        return json_decode($bytes);
65    } else {
66        // Ugh oh...
67        return NULL;
68    }
69}
70
71/*
72 * Here we simply set our transcoder functions above as the active transcoders
73 * for our bucket object.
74 */
75$db->setTranscoder('example_encoder', 'example_decoder');
76
77/*
78 * Create an image to test with
79 */
80$im = imagecreatetruecolor(300, 50);
81$text_color = imagecolorallocate($im, 233, 14, 91);
82imagestring($im, 6, 10, 10,  'Couchbase Rocks!', $text_color);
83
84/*
85 * Store it in Couchbase.  This should execute our custom encoder.
86 */
87$db->upsert('test_image', $im);
88
89
90/*
91 * Now lets retreive it back, it should still be an image thanks to our
92 * custom decoder.
93 */
94$image_doc = $db->get('test_image');
95
96/*
97 * Output our retrieved document to the browser with a image/png content-type.
98 */
99header('Content-Type: image/png');
100imagepng($image_doc->value);
101