Projet

Général

Profil

Paste
Télécharger (1,99 ko) Statistiques
| Branche: | Révision:

root / class / TilesGenerator.php @ e68abb6a

1
<?php
2

    
3
require_once(dirname(__FILE__).'/../constants.inc.php');
4

    
5
class TilesGeneratorException extends Exception {}
6
class TilesGeneratorRightsException extends TilesGeneratorException {}
7
class TilesGeneratorScriptException extends TilesGeneratorException {}
8

    
9
class TilesGenerator {
10
        private $panorama;
11

    
12
        const SCRIPT_RELATIVE_PATH = 'to_tiles/gen_tiles.sh';
13

    
14
        public function __construct($img_path, $panorama) {
15
                /** @param $img_path : path of the source image
16
                 *  @param $panorama : a site_point instance
17
                 */
18
                $this->panorama = $panorama;
19
                $this->img_path = $img_path;
20
        }
21

    
22
        public function prepare() {
23
                $err = false;
24
                $pano_path = $this->panorama->tiles_path();
25

    
26
                if (! is_dir(PANORAMA_PATH)) {
27
                        if (! mkdir(PANORAMA_PATH)) {
28
                                $err = "le répertoire \"PANORAMA_PATH\" n'est pas accessible et ne peut être créé";
29
                        }
30
                } else if (file_exists($pano_path)) {
31
                        $err = sprintf("le nom de répertoire \"%s\" est déjà pris",
32
                                       $pano_path);
33
                } else {
34
                        mkdir($pano_path);
35
                }
36
                if ($err) {
37
                        throw (new TilesGeneratorRightsException($err));
38
                }
39

    
40
        }
41

    
42
        public function mk_command() {
43
                /** Returns the command to execute
44
                 */
45
                $c = sprintf('%s/%s -p "%s" "%s"',
46
                             CELUTZ_PATH, $this::SCRIPT_RELATIVE_PATH,
47
                             $this->panorama->tiles_prefix(), $this->img_path);
48
                return escapeshellcmd($c);
49
        }
50

    
51
        public function process() {
52
                $err = false;
53
                if ($fp = popen($this->mk_command(), 'r')) {
54
                        while (!feof($fp)) {
55
                                $results = fgets($fp, 4096);
56
                                if (strlen($results) == 0) {
57
                                        // stop the browser timing out
58
                                        flush();
59
                                } else {
60
                                        $tok = strtok($results, "\n");
61
                                        while ($tok !== false) {
62
                                                echo htmlspecialchars(sprintf("%s\n",$tok))."<br/>";
63
                                                flush();
64
                                                $tok = strtok("\n");
65
                                        }
66
                                }
67
                        }
68
                        if (pclose($fp) !== 0) {
69
                                $err = "Opération en échec durant l'exécution du script";
70
                        }
71
                } else {
72
                        $err =  'Opération en échec à l\'ouverture du script !';
73
                }
74

    
75
                if ($err) {
76
                        throw (new TilesGeneratorScriptException($err));
77
                }
78
        }
79
}
80

    
81
?>