Embed meta data into generated screenshot file
[phancap.git] / src / phancap / Repository.php
1 <?php
2 /**
3  * Part of phancap
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Repository
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
13  */
14 namespace phancap;
15
16 /**
17  * Repository of existing screenshots
18  *
19  * @category  Tools
20  * @package   Repository
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
26  */
27 class Repository
28 {
29     /**
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.
34      *
35      * @param Options $options Image rendering options
36      *
37      * @return Image Image object
38      */
39     public function getImage(Options $options)
40     {
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);
46         }
47         return $img;
48     }
49
50     /**
51      * Get the cache image filename for the given rendering options
52      *
53      * @param Options $options Image rendering options
54      *
55      * @return string relative file name for the image
56      */
57     public function getFilename(Options $options)
58     {
59         $optValues = $options->values;
60         unset($optValues['smaxage']);
61         unset($optValues['atimestamp']);
62         unset($optValues['asignature']);
63         unset($optValues['atoken']);
64
65         return parse_url($optValues['url'], PHP_URL_HOST)
66             . '-' . md5(\serialize($optValues))
67             . '.' . $optValues['sformat'];
68     }
69
70     /**
71      * Check if the image is available locally.
72      *
73      * @param Image   $img     Image object to render (contains name)
74      * @param Options $options Image rendering options
75      *
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
78      *                 exist.
79      */
80     public function isAvailable(Image $img, Options $options)
81     {
82         $path = $img->getPath();
83         if (!file_exists($path)) {
84             return false;
85         }
86
87         if (filemtime($path) < time() - $options->values['smaxage']) {
88             return false;
89         }
90
91         return true;
92     }
93
94     /**
95      * Render a website screenshot
96      *
97      * @param Image   $img     Image object to render (contains name)
98      * @param Options $options Image rendering options
99      *
100      * @return void
101      * @throws \Exception When something goes wrong
102      */
103     protected function render(Image $img, Options $options)
104     {
105         $adapter = new Adapter_Cutycapt();
106         $adapter->setConfig($this->config);
107         try {
108             $adapter->render($img, $options);
109             $adapter->cleanup();
110         } catch (\Exception $e) {
111             $adapter->cleanup();
112             throw $e;
113         }
114
115         $meta = new MetaData();
116         $meta->embed($img, $options);
117     }
118
119     /**
120      * Set phancap configuration
121      *
122      * @param Config $config Phancap configuration
123      *
124      * @return void
125      */
126     public function setConfig(Config $config)
127     {
128         $this->config = $config;
129     }
130 }
131 ?>