7 protected $lockFile = null;
10 * @return mixed TRUE if all is fine, array with error messages otherwise
12 public function isAvailable()
14 $old = error_reporting(error_reporting() & ~E_STRICT);
16 if (\System::which('xvfb-run') === false) {
17 $arErrors[] = '"xvfb-run" is not installed';
19 if (\System::which('cutycapt') === false) {
20 $arErrors[] = '"cutycapt" is not installed';
22 if (\System::which('convert') === false) {
23 $arErrors[] = '"convert" (imagemagick) is not installed';
26 error_reporting($old);
27 if (count($arErrors)) {
34 public function render(Image $img, Options $options)
36 $format = $options->values['sformat'];
37 if ($format == 'jpg') {
41 $serverNumber = $this->getServerNumber($options);
42 $tmpPath = $img->getPath() . '-tmp';
44 . ' --url=' . escapeshellarg($options->values['url'])
45 . ' --out-format=' . escapeshellarg($format)
46 . ' --out=' . escapeshellarg($tmpPath)
48 . ' --min-width=' . $options->values['bwidth'];
49 if ($options->values['bheight'] !== null) {
50 $cmd .= ' --min-height=' . $options->values['bheight'];
55 . ' --server-args="-screen 0, 1024x768x24"'
56 . ' --server-num=' . $serverNumber;
57 Executor::run($xvfbcmd . ' ' . $cmd);
59 $this->resize($tmpPath, $img, $options);
62 protected function getServerNumber()
64 //clean stale lock files
71 $f = $this->config->cacheDir . 'tmp-curlycapt-server-' . $num . '.lock';
72 $this->lockHdl = fopen($f, 'w');
73 if (flock($this->lockHdl, LOCK_EX | LOCK_NB)) {
78 fclose($this->lockHdl);
83 throw new \Exception('Too many requests running');
90 public function cleanup()
92 if ($this->lockFile !== null && $this->lockHdl) {
93 flock($this->lockHdl, LOCK_UN);
94 unlink($this->lockFile);
98 $this->config->cacheDir . 'tmp-curlycapt-server-*.lock'
102 foreach ($lockFiles as $file) {
103 if ($now - filemtime($file) > 120) {
104 //delete stale lock file; probably something crashed.
110 protected function resize($tmpPath, $img, $options)
112 if ($options->values['sformat'] == 'pdf') {
114 rename($tmpPath, $img->getPath());
119 if ($options->values['smode'] == 'screen') {
120 $crop = ' -crop ' . $options->values['swidth']
121 . 'x' . $options->values['sheight']
125 $convertcmd = 'convert'
126 . ' ' . escapeshellarg($tmpPath)
127 . ' -resize ' . $options->values['swidth']
129 . ' ' . escapeshellarg($img->getPath());
130 Executor::run($convertcmd);
131 //var_dump($convertcmd);die();
135 public function setConfig(Config $config)
137 $this->config = $config;