7 protected $lockFile = null;
9 public function isAvailable()
11 //FIXME: setup check for xvfbrun, cutycapt, convert
14 public function render(Image $img, Options $options)
16 $format = $options->values['sformat'];
17 if ($format == 'jpg') {
21 $serverNumber = $this->getServerNumber($options);
22 $tmpPath = $img->getPath() . '-tmp';
24 . ' --url=' . escapeshellarg($options->values['url'])
25 . ' --out-format=' . escapeshellarg($format)
26 . ' --out=' . escapeshellarg($tmpPath)
28 . ' --min-width=' . $options->values['bwidth'];
29 if ($options->values['bheight'] !== null) {
30 $cmd .= ' --min-height=' . $options->values['bheight'];
35 . ' --server-args="-screen 0, 1024x768x24"'
36 . ' --server-num=' . $serverNumber;
37 Executor::run($xvfbcmd . ' ' . $cmd);
39 $this->resize($tmpPath, $img, $options);
42 protected function getServerNumber()
44 //clean stale lock files
51 $f = $this->config->cacheDir . 'tmp-curlycapt-server-' . $num . '.lock';
52 $this->lockHdl = fopen($f, 'w');
53 if (flock($this->lockHdl, LOCK_EX | LOCK_NB)) {
58 fclose($this->lockHdl);
63 throw new \Exception('Too many requests running');
70 public function cleanup()
72 if ($this->lockFile !== null && $this->lockHdl) {
73 flock($this->lockHdl, LOCK_UN);
74 unlink($this->lockFile);
78 $this->config->cacheDir . 'tmp-curlycapt-server-*.lock'
82 foreach ($lockFiles as $file) {
83 if ($now - filemtime($file) > 120) {
84 //delete stale lock file; probably something crashed.
90 protected function resize($tmpPath, $img, $options)
92 if ($options->values['sformat'] == 'pdf') {
94 rename($tmpPath, $img->getPath());
99 if ($options->values['smode'] == 'screen') {
100 $crop = ' -crop ' . $options->values['swidth']
101 . 'x' . $options->values['sheight']
105 $convertcmd = 'convert'
106 . ' ' . escapeshellarg($tmpPath)
107 . ' -resize ' . $options->values['swidth']
109 . ' ' . escapeshellarg($img->getPath());
110 Executor::run($convertcmd);
111 //var_dump($convertcmd);die();
115 public function setConfig(Config $config)
117 $this->config = $config;