1<?php
2
3namespace dokuwiki\plugin\gallery\classes;
4
5use dokuwiki\Utf8\PhpString;
6
7class Image
8{
9    public const IMG_REGEX = '/\.(jpe?g|gif|png|svg|webp)$/i';
10
11    protected $isExternal = false;
12    protected $src;
13    protected $filename;
14    protected $localfile;
15    protected $title;
16    protected $description;
17    protected $width;
18    protected $height;
19    protected $created = 0;
20    protected $modified = 0;
21    protected $detaillink;
22
23
24    /**
25     * @param string $src local ID or external URL to image
26     * @throws \Exception
27     */
28    public function __construct($src)
29    {
30        $this->src = $src;
31        if (preg_match('/^https:\/\//i', $src)) {
32            $this->isExternal = true;
33            $path = parse_url($src, PHP_URL_PATH);
34            $this->filename = basename($path);
35        } else {
36            $this->localfile = mediaFN($src);
37            if (!file_exists($this->localfile)) throw new \Exception('File not found: ' . $this->localfile);
38            $this->filename = basename($this->localfile);
39            $this->modified = filemtime($this->localfile);
40
41            $jpegMeta = new \JpegMeta($this->localfile);
42            $this->title = $jpegMeta->getField('Simple.Title');
43            $this->description = $jpegMeta->getField('Iptc.Caption');
44            $this->created = $jpegMeta->getField('Date.EarliestTime');
45            $this->width = $jpegMeta->getField('File.Width');
46            $this->height = $jpegMeta->getField('File.Height');
47        }
48    }
49
50    public function isExternal()
51    {
52        return $this->isExternal;
53    }
54
55    public function getSrc()
56    {
57        return $this->src;
58    }
59
60    public function getFilename()
61    {
62        return $this->filename;
63    }
64
65    public function setFilename(string $filename)
66    {
67        $this->filename = $filename;
68    }
69
70    public function getLocalfile()
71    {
72        return $this->localfile;
73    }
74
75    public function setLocalfile(string $localfile)
76    {
77        $this->localfile = $localfile;
78    }
79
80    /**
81     * @return string
82     */
83    public function getTitle()
84    {
85        if (empty($this->title) || $this->title == $this->filename) {
86            $title = str_replace('_', ' ', $this->filename);
87            $title = preg_replace(self::IMG_REGEX, '', $title);
88            $title = PhpString::ucwords($title);
89            return $title;
90        }
91        return $this->title;
92    }
93
94    /**
95     * @param string $title
96     */
97    public function setTitle($title)
98    {
99        $this->title = $title;
100    }
101
102    /**
103     * @return string
104     */
105    public function getDescription()
106    {
107        return trim(str_replace("\n", ' ', $this->description));
108    }
109
110    /**
111     * @param string $description
112     */
113    public function setDescription($description)
114    {
115        $this->description = $description;
116    }
117
118    /**
119     * @return int
120     */
121    public function getWidth()
122    {
123        return $this->width;
124    }
125
126    /**
127     * @param int $width
128     */
129    public function setWidth($width)
130    {
131        $this->width = $width;
132    }
133
134    /**
135     * @return int
136     */
137    public function getHeight()
138    {
139        return $this->height;
140    }
141
142    /**
143     * @param int $height
144     */
145    public function setHeight($height)
146    {
147        $this->height = $height;
148    }
149
150    /**
151     * @return int
152     */
153    public function getCreated()
154    {
155        return $this->created;
156    }
157
158    /**
159     * @param int $created
160     */
161    public function setCreated($created)
162    {
163        $this->created = $created;
164    }
165
166    /**
167     * @return int
168     */
169    public function getModified()
170    {
171        return $this->modified;
172    }
173
174    /**
175     * @param int $modified
176     */
177    public function setModified($modified)
178    {
179        $this->modified = $modified;
180    }
181
182    public function getDetaillink()
183    {
184        return $this->detaillink;
185    }
186
187    public function setDetaillink($detaillink)
188    {
189        $this->detaillink = $detaillink;
190    }
191}
192