9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @copyright 2014 Christian Weiske
11 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12 * @link http://cweiske.de/phancap.htm
17 * Repository of existing screenshots
21 * @author Christian Weiske <cweiske@cweiske.de>
22 * @copyright 2014 Christian Weiske
23 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24 * @version Release: @package_version@
25 * @link http://cweiske.de/phancap.htm
30 * Return image object for the given rendering options.
31 * Loads it from cache if possible.
32 * If cache is expired or image does not yet exist,
33 * it will be rendered.
35 * @param Options $options Image rendering options
37 * @return Image Image object
39 public function getImage(Options $options)
41 $name = $this->getFilename($options);
42 $img = new Image($name);
43 $img->setConfig($this->config);
44 if (!$this->isAvailable($img, $options)) {
45 $this->render($img, $options);
51 * Get the cache image filename for the given rendering options
53 * @param Options $options Image rendering options
55 * @return string relative file name for the image
57 public function getFilename(Options $options)
59 $optValues = $options->values;
60 unset($optValues['smaxage']);
61 unset($optValues['atimestamp']);
62 unset($optValues['asignature']);
63 unset($optValues['atoken']);
65 return parse_url($optValues['url'], PHP_URL_HOST)
66 . '-' . md5(\serialize($optValues))
67 . '.' . $optValues['sformat'];
71 * Check if the image is available locally.
73 * @param Image $img Image object to render (contains name)
74 * @param Options $options Image rendering options
76 * @return boolean True if we have it and it's within the cache lifetime,
77 * false if the cache expired or the screenshot does not
80 public function isAvailable(Image $img, Options $options)
82 $path = $img->getPath();
83 if (!file_exists($path)) {
87 if (filemtime($path) < time() - $options->values['smaxage']) {
95 * Render a website screenshot
97 * @param Image $img Image object to render (contains name)
98 * @param Options $options Image rendering options
101 * @throws \Exception When something goes wrong
103 protected function render(Image $img, Options $options)
105 $adapter = new Adapter_Cutycapt();
106 $adapter->setConfig($this->config);
108 $adapter->render($img, $options);
110 } catch (\Exception $e) {
117 * Set phancap configuration
119 * @param Config $config Phancap configuration
123 public function setConfig(Config $config)
125 $this->config = $config;