-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.php
More file actions
216 lines (179 loc) · 5 KB
/
Copy pathImage.php
File metadata and controls
216 lines (179 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
declare(strict_types=1);
namespace ADT\Utils\Filters;
use ADT\Utils\FileSystem;
use Exception;
use Nette\Utils\ImageException;
class Image
{
private string $path;
private string $dir;
private int $multiplier;
public function __construct(string $path, string $dir = 'thumbnails', int $multiplier = 2)
{
$this->path = $path;
$this->dir = $dir;
$this->multiplier = $multiplier;
}
const FormatToExtensions = [
IMAGETYPE_WEBP => 'webp'
];
const ExtensionsToFormat = [
'webp' => IMAGETYPE_WEBP
];
/**
* @throws Exception
*/
public function format(string $url, int $width, int $height, int $mode = \Nette\Utils\Image::OrSmaller, int $format = IMAGETYPE_WEBP): string
{
$originalUrl = $url;
$isRemoteUrl = $this->isRemoteUrl($url);
// original file does not exist
if ($isRemoteUrl) {
$contents = @file_get_contents($url);
if (!$contents) {
return $originalUrl;
}
list($urlWithoutExtension,) = $this->splitUrlOnLastDot($this->removeProtocol($url));
} else {
$url = trim($url, '/');
if (!file_exists($this->path . '/' . $url)) {
return $originalUrl;
}
$contents = file_get_contents($this->path . '/' . $url);
if ($this->isAnimatedGif($contents)) {
return $originalUrl;
}
list($urlWithoutExtension,) = $this->splitUrlOnLastDot($url);
}
$width = $width * $this->multiplier;
$height = $height * $this->multiplier;
$ext = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
if ($ext === 'svg') {
return $originalUrl;
}
$newFile = $this->dir . '/' . $urlWithoutExtension . '_' . $width . '_' . $height . '_' . $mode . '_' . $ext . '.' . self::FormatToExtensions[$format];
$this->createImage($contents, $width, $height, $mode, $format, $this->path . '/' . $newFile);
return '/' . $newFile;
}
private function isRemoteUrl(string $url): bool
{
$parsedUrl = parse_url($url);
if (isset($parsedUrl['scheme'])) {
return in_array($parsedUrl['scheme'], ['http', 'https']);
}
return false;
}
/**
* Thanks to ZeBadger for original example, and Davide Gualano for pointing me to it
* Original at http://it.php.net/manual/en/function.imagecreatefromgif.php#59787
**/
private function isAnimatedGif($fileContents): bool
{
$raw = $fileContents;
$offset = 0;
$frames = 0;
while ($frames < 2)
{
$where1 = strpos($raw, "\x00\x21\xF9\x04", $offset);
if ( $where1 === false )
{
break;
}
else
{
$offset = $where1 + 1;
$where2 = strpos( $raw, "\x00\x2C", $offset );
if ( $where2 === false )
{
break;
}
else
{
if ( $where1 + 8 == $where2 )
{
$frames ++;
}
$offset = $where2 + 1;
}
}
}
return $frames > 1;
}
private function splitUrlOnLastDot(string $url): array
{
$lastDotPos = strrpos($url, '.');
if ($lastDotPos !== false) {
$part1 = substr($url, 0, $lastDotPos);
$part2 = substr($url, $lastDotPos + 1);
return [$part1, $part2];
}
return [$url, ''];
}
private function removeProtocol(string $url): string
{
return preg_replace("/^https?:\/\//", "", $url);
}
/**
* @throws ImageException
*/
public function createImageFromThumbnailUrl(string $url): bool
{
$info = pathinfo($url);
if (empty($info['extension'])) {
return false;
}
$format = $info['extension'];
if (!array_key_exists($format, static::ExtensionsToFormat)) {
return false;
}
if (empty($info['filename'])) {
return false;
}
$fileInfo = pathinfo($info['filename']);
if (empty($fileInfo['filename'])) {
return false;
}
$segments = explode('_', $fileInfo['filename']);
$extension = array_pop($segments);
$mode = (int)array_pop($segments);
$height = (int)array_pop($segments);
$width = (int)array_pop($segments);
$originalFile = $info['dirname'] . '/' . implode('_', $segments) . '.' . $extension;
if (!file_exists($this->path . '/' . $originalFile)) {
return false;
}
$this->createImage(file_get_contents($this->path . '/' . $originalFile), $width, $height, $mode, static::ExtensionsToFormat[$format], $this->path . '/' . $this->dir . '/' . $url);
return true;
}
/**
* @throws ImageException
* @throws Exception
*/
protected function createImage(string $contents, int $width, int $height, int $mode, int $format, string $newFile): void
{
// thumbnail already exists
if (file_exists($newFile)) {
return;
}
FileSystem::createDirAtomically(dirname($newFile));
$prevErrorHandler = set_error_handler(function ($errno, $errstr) use (&$prevErrorHandler) {
if ($errno === E_USER_WARNING && $errstr === 'Nette\Utils\Image::fromString(): gd-png: libpng warning: iCCP: known incorrect sRGB profile') {
return true;
}
return $prevErrorHandler ? $prevErrorHandler(...func_get_args()) : false;
});
$image = \Nette\Utils\Image::fromString($contents);
set_error_handler($prevErrorHandler);
$image->resize($width, $height, $mode);
$image->save($newFile, 100, $format);
}
public function getPath(): string
{
return $this->path;
}
public function getDir(): string
{
return $this->dir;
}
}