fix CS
[phancap.git] / src / phancap / Image.php
1 <?php
2 /**
3  * Part of phancap
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Image
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  * Image meta data and methods to get info about the file
18  *
19  * @category  Tools
20  * @package   Image
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 Image
28 {
29
30     /**
31      * Description for protected
32      * @var object
33      */
34     protected $config;
35
36     /**
37      * Description for public
38      * @var string
39      */
40     public $name;
41
42
43     /**
44      * Set the file name
45      *
46      * @param string $name Image file name
47      */
48     public function __construct($name)
49     {
50         $this->name = $name;
51     }
52
53     /**
54      * Get the expiration date for the image, depending on
55      * the maximum age the user requested.
56      *
57      * @param Options $options Image rendering options
58      *
59      * @return integer Unix timestamp
60      */
61     public function getExpiryDate(Options $options)
62     {
63         $mtime = filemtime($this->getPath());
64
65         return $mtime + $options->values['smaxage'];
66     }
67
68     /**
69      * Get the MIME type for the file
70      *
71      * @return string MIME type string ("image/png")
72      */
73     public function getMimeType()
74     {
75         $ext = substr($this->name, -4);
76         if ($ext == '.jpg') {
77             return 'image/jpeg';
78         } else if ($ext == '.png') {
79             return 'image/png';
80         } else if ($ext == '.png') {
81             return 'application/pdf';
82         }
83         return 'application/octet-stream';
84     }
85
86     /**
87      * Get the full path to the cached image on disk
88      *
89      * @return string Full path to image
90      */
91     public function getPath()
92     {
93         return $this->config->cacheDir . $this->name;
94     }
95
96     /**
97      * Get the public URL for the image
98      *
99      * @return string Public image URL
100      */
101     public function getUrl()
102     {
103         return $this->config->cacheDirUrl . $this->name;
104     }
105
106     /**
107      * Set phancap configuration
108      *
109      * @param Config $config Phancap configuration
110      *
111      * @return void
112      */
113     public function setConfig(Config $config)
114     {
115         $this->config = $config;
116     }
117 }
118 ?>