1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** Created by Gorlum 08.01.2024 19:14 */ |
4
|
|
|
|
5
|
|
|
use GIFEndec\Decoder; |
6
|
|
|
use GIFEndec\Events\FrameDecodedEvent; |
7
|
|
|
use GIFEndec\IO\FileStream; |
8
|
|
|
use Tools\ImageContainer; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @property int $height |
12
|
|
|
* @property int $width |
13
|
|
|
*/ |
14
|
|
|
class ImageFile { |
15
|
|
|
public $dir = ''; |
16
|
|
|
public $fileName = ''; |
17
|
|
|
public $fullPath = ''; |
18
|
|
|
|
19
|
|
|
private $image = null; |
20
|
|
|
/** @var string|false $content Image file content */ |
21
|
|
|
protected $content; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $fileName |
25
|
|
|
* @param $dir |
26
|
|
|
* |
27
|
|
|
* @return static|null |
28
|
|
|
*/ |
29
|
|
|
public static function read($fileName, $dir = '') { |
30
|
|
|
$that = new static($fileName, $dir); |
31
|
|
|
|
32
|
|
|
if ($that->getImageContainer() === null) { |
33
|
|
|
unset($that); |
34
|
|
|
$that = null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $that; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $fileName Name of image file. Can be full path to file or just filename. In latter case $dir will be used |
42
|
|
|
* @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 |
43
|
|
|
*/ |
44
|
|
|
public function __construct($fileName, $dir = '') { |
45
|
|
|
if (dirname($fileName) !== '.') { |
46
|
|
|
$this->dir = realpath(dirname($fileName)); |
47
|
|
|
$this->fileName = basename($fileName); |
48
|
|
|
} else { |
49
|
|
|
$this->dir = realpath($dir ?: __DIR__ . '/../'); |
50
|
|
|
$this->fileName = $fileName; |
51
|
|
|
} |
52
|
|
|
$this->dir = str_replace('\\', '/', $this->dir) . '/'; |
53
|
|
|
|
54
|
|
|
$this->fullPath = $this->dir . $this->fileName; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function __get($property) { |
58
|
|
|
if (in_array($property, ['height', 'width',])) { |
59
|
|
|
return $this->getImageContainer() ? $this->getImageContainer()->$property : 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return property_exists($this, $property) ? $this->$property : null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// public function __set($property, $value) { |
66
|
|
|
// if (property_exists($this, $property)) { |
67
|
|
|
// $this->$property = $value; |
68
|
|
|
// } |
69
|
|
|
// |
70
|
|
|
// return $this; |
71
|
|
|
// } |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return ImageContainer |
75
|
|
|
*/ |
76
|
|
|
public function getImageContainer() { |
77
|
|
|
if (empty($this->image)) { |
78
|
|
|
$this->image = ImageContainer::load($this->fullPath); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->image; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isAnimatedGif() { |
85
|
|
|
// Checking file extension |
86
|
|
|
if (!strtolower(pathinfo($this->fullPath, PATHINFO_EXTENSION)) === 'gif') { |
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Counting frame(s) |
91
|
|
|
if (substr($content = $this->loadContent(), 0, 4) !== 'GIF8') { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
//an animated gif contains multiple "frames", with each frame having a |
95
|
|
|
//header made up of: |
96
|
|
|
// * a static 4-byte sequence (\x00\x21\xF9\x04) |
97
|
|
|
// * 4 variable bytes |
98
|
|
|
// * a static 2-byte sequence (\x00\x2C) |
99
|
|
|
if (preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', $content) <= 1) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return false|string |
108
|
|
|
*/ |
109
|
|
|
protected function loadContent() { |
110
|
|
|
if (empty($this->content)) { |
111
|
|
|
$this->content = file_get_contents($this->fullPath); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->content; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|