Projet

Général

Profil

Paste
Télécharger (2,08 ko) Statistiques
| Branche: | Révision:

root / class / TilesGenerator.php @ d0ee1e7f

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 {
31
      if (file_exists($pano_path)) {
32
        if ($this->panorama->has_tiles()) {
33
                            $err = sprintf("\"%s\" contient déjà un découpage de panorama.",
34
                         $pano_path);
35
        }
36
                  } else {
37
                          mkdir($pano_path);
38
                  }
39
    }
40
                if ($err) {
41
                        throw (new TilesGeneratorRightsException($err));
42
                }
43
        }
44

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

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

    
78
                if ($err) {
79
                        throw (new TilesGeneratorScriptException($err));
80
                }
81
        }
82
}
83

    
84
?>