summaryrefslogtreecommitdiffstats
path: root/examples/lib/FilterTranscode.php
blob: 30cf1ca04deb2df19b25f8aaf2dda715df2cf252 (plain)
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
<?php

namespace lib;

class FilterTranscode extends php_user_filter
{
    private static $name = 'convert.transcode.';

    private $encoding_from = 'auto';

    private $encoding_to;

    public function onCreate()
    {
        if (strpos($this->filtername, self::$name) !== 0) {
            return false;
        }

        $params = substr($this->filtername, strlen(self::$name));
        if (! preg_match('/^([-\w]+)(:([-\w]+))?$/', $params, $matches)) {
            return false;
        }

        if (isset($matches[1])) {
            $this->encoding_from = $matches[1];
        }

        $this->encoding_to = mb_internal_encoding();
        if (isset($matches[3])) {
            $this->encoding_to = $matches[3];
        }

        $this->params['locale'] = setlocale(LC_CTYPE, '0');
        if (stripos($this->params['locale'], 'UTF-8') === false) {
            setlocale(LC_CTYPE, 'en_US.UTF-8');
        }

        return true;
    }

    public function onClose()
    {
        setlocale(LC_CTYPE, $this->params['locale']);
    }

    public function filter($in, $out, &$consumed, $closing)
    {
        while ($res = stream_bucket_make_writeable($in)) {
            $res->data = @mb_convert_encoding($res->data, $this->encoding_to, $this->encoding_from);
            $consumed += $res->datalen;
            stream_bucket_append($out, $res);
        }

        return PSFS_PASS_ON;
    }
}