1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** Created by Gorlum 08.01.2024 19:14 */ |
4
|
|
|
|
5
|
|
|
use Tools\ImageContainer; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @property int $height |
9
|
|
|
* @property int $width |
10
|
|
|
*/ |
11
|
|
|
class ImageFile { |
12
|
|
|
public $dir = ''; |
13
|
|
|
public $fileName = ''; |
14
|
|
|
public $fullPath = ''; |
15
|
|
|
|
16
|
|
|
private $image = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param $fileName |
20
|
|
|
* @param $dir |
21
|
|
|
* |
22
|
|
|
* @return static|null |
23
|
|
|
*/ |
24
|
|
|
public static function read($fileName, $dir = '') { |
25
|
|
|
$that = new static($fileName, $dir); |
26
|
|
|
|
27
|
|
|
if ($that->getImageContainer() === null) { |
28
|
|
|
unset($that); |
29
|
|
|
$that = null; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $that; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $fileName Name of image file. Can be full path to file or just filename. In latter case $dir will be used |
37
|
|
|
* @param string $dir If present and filename is just name of file will we added to make full path. If empty - tools root folder will be assumed |
38
|
|
|
*/ |
39
|
|
|
public function __construct($fileName, $dir = '') { |
40
|
|
|
if (dirname($fileName) !== '.') { |
41
|
|
|
$this->dir = realpath(dirname($fileName)); |
42
|
|
|
$this->fileName = basename($fileName); |
43
|
|
|
} else { |
44
|
|
|
$this->dir = realpath($dir ?: __DIR__ . '/../'); |
45
|
|
|
$this->fileName = $fileName; |
46
|
|
|
} |
47
|
|
|
$this->dir = str_replace('\\', '/', $this->dir) . '/'; |
48
|
|
|
|
49
|
|
|
$this->fullPath = $this->dir . $this->fileName; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function __get($property) { |
53
|
|
|
if (in_array($property, ['height', 'width',])) { |
54
|
|
|
return $this->getImageContainer() ? $this->getImageContainer()->$property : 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return property_exists($this, $property) ? $this->$property : null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// public function __set($property, $value) { |
61
|
|
|
// if (property_exists($this, $property)) { |
62
|
|
|
// $this->$property = $value; |
63
|
|
|
// } |
64
|
|
|
// |
65
|
|
|
// return $this; |
66
|
|
|
// } |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return ImageContainer |
70
|
|
|
*/ |
71
|
|
|
public function getImageContainer() { |
72
|
|
|
if (empty($this->image)) { |
73
|
|
|
$this->image = ImageContainer::load($this->fullPath); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->image; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// public function load() { |
80
|
|
|
// $this->image = ImageContainer::load($this->fullPath); |
81
|
|
|
// } |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|