Projet

Général

Profil

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

root / class / TilesGenerator.php @ c621f296

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
         $pano_files = scandir($pano_path);
33
        foreach($pano_files as $filename) {
34
          if (preg_match('/.*\.jpg/', $filename)) {
35
                              $err = sprintf("\"%s\" contient déjà un découpage de panorama.",
36
                           $pano_path);
37
            break;
38
          }
39
        }
40
                  } else {
41
                          mkdir($pano_path);
42
                  }
43
    }
44
                if ($err) {
45
                        throw (new TilesGeneratorRightsException($err));
46
                }
47

    
48
        }
49

    
50
        public function mk_command() {
51
                /** Returns the command to execute
52
                 */
53
                $c = sprintf('%s/%s -p "%s" "%s"',
54
                             CELUTZ_PATH, $this::SCRIPT_RELATIVE_PATH,
55
                             $this->panorama->tiles_prefix(), $this->img_path);
56
                return escapeshellcmd($c);
57
        }
58

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

    
83
                if ($err) {
84
                        throw (new TilesGeneratorScriptException($err));
85
                }
86
        }
87
}
88

    
89
?>