-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTools.php
More file actions
executable file
·305 lines (265 loc) · 10.1 KB
/
Copy pathTools.php
File metadata and controls
executable file
·305 lines (265 loc) · 10.1 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
namespace Pet\Tools;
use Exception;
class Tools {
/**
* jsonDe
*
* @param mixed $value
* @param mixed $bool
* @return array
*/
static function jsonDe(string $value, bool $bool = true): array|object {
try {
return json_decode($value, $bool);
} catch (Exception $e) {
return [];
}
}
/**
* strRep
*
* @param mixed $i
* @param mixed $seporator
* @param mixed $string
* @param mixed $if
* @return string
*/
static function strRep($i, $seporator, &$string, $if = null): string {
$string = str_split($string);
if ($if && $string[$i] === $if) $string[$i] = $seporator;
if (!$if) $string[$i] = $seporator;
return implode("", $string);
}
/**
* array_implode
*
* @param mixed $seporator
* @param mixed $arrKeyValue
* @param mixed $between [key] [val]
* @return string
*
*/
static function array_implode(string $seporator, array $arrKeyValue, string $between = '[key]=[val]', $callback = null): string {
return implode($seporator, array_map(
fn($v, $k)=> $callback?$callback($v, $k, $between):str_replace(['[val]', '[key]'],[$v, $k], $between),
$arrKeyValue,
array_keys($arrKeyValue)
));
}
/**
* is_assos
*
* @param array $array
* @return string "index"|"gibrid"|"assos"
*/
static function is_assos(array $array): string
{
$keys = array_keys($array);
$str = implode("", $keys);
if (is_numeric($str)) return 'index';
$hasString = false;
$hasInt = false;
foreach ($keys as $key) {
if (is_string($key)) {
$hasString = true;
} else {
$hasInt = true;
}
}
if ($hasInt && $hasString) {
return 'gibrid';
}
return 'assos';
}
/**
* strRepalceFile
*
* @param array $search
* @param array|string $replace
* @param string $path
* @param string $save
* @return string
*/
static function strRepalceFile(array|string $search, array|string $replace, string $path, ?string $save = null): string|false {
if (!file_exists($path)) return false;
$file = file_get_contents($path);
$file = str_replace($search, $replace, $file);
if ($save) file_put_contents($save, $file);
return $file;
}
public static function filter(array $data, callable $callback): array
{
foreach ($data as $k => $v){
$data[$k] = $callback($k, $v);
}
return $data;
}
public static function scan(string $path, callable $callback, $isPath = false):void
{
foreach (scandir($path) as $file) {
if (in_array($file, ['..', '.'])) continue;
$name = $path . DS . $file;
if ($isPath) {
if (is_dir($name)) $callback($name, false);
if (file_exists($name) && is_readable($name)) $callback(false, $name);
} else {
if (is_dir($name)) $callback($file, false);
if (file_exists($name) && is_readable($name)) $callback(false, $file);
}
}
}
/**
* Создание миниатюры (thumb) изображения.
*
* Поддерживает JPEG, PNG, GIF, WebP, ICO.
* При crop=true вырезает центральную область нужного размера.
* При crop=false ресайзит с сохранением пропорций, вписывая в targetWidth x targetHeight.
*
* @param string $src Путь к исходному файлу
* @param string $dest Путь для сохранения результата
* @param int $targetWidth Целевая ширина
* @param int|null $targetHeight Целевая высота (если null — высчитывается по пропорции)
* @param int $x Смещение по X от исходного изображения (при crop)
* @param int $y Смещение по Y от исходного изображения (при crop)
* @param bool $crop Вырезать центральную область (true) или ресайзить с пропорциями (false)
* @param int $dst_x Смещение по X на целевом изображении
* @param int $dst_y Смещение по Y на целевом изображении
* @param int $quality Качество для JPEG/WebP (0-100)
* @param string|null $outputFormat Принудительный формат вывода ('webp' или null)
* @return bool|null true при успехе, null при ошибке
*/
static function thumb(
string $src,
string $dest,
int $targetWidth,
?int $targetHeight = null,
int $x = 0,
int $y = 0,
bool $crop = false,
int $dst_x = 0,
int $dst_y = 0,
int $quality = 100,
?string $outputFormat = null
) {
$image_handlers = [
2 => [
'load' => 'imagecreatefromjpeg',
'save' => 'imagejpeg',
'quality' => $quality
],
3 => [
'load' => 'imagecreatefrompng',
'save' => 'imagepng',
'quality' => 0
],
4 => [
'load' => 'imagecreatefromgif',
'save' => 'imagegif'
],
18 => [
'load' => 'imagecreatefromwebp',
'save' => 'imagewebp',
'quality' => $quality
]
];
$type = @exif_imagetype($src);
// ICO — просто копируем
if ($type === 17) {
return copy($src, $dest);
}
if (!$type || !isset($image_handlers[$type])) {
return null;
}
$image = call_user_func($image_handlers[$type]['load'], $src);
if (!$image) {
return null;
}
$width = imagesx($image);
$height = imagesy($image);
// Если targetWidth = 0 — конвертация без изменения размера
if ($targetWidth === 0) {
$targetWidth = $width;
$targetHeight = $height;
$srcWidth = $width;
$srcHeight = $height;
$crop = false;
} else {
// Если исходник меньше или равен target — не увеличиваем
$targetWidth = min($targetWidth, $width);
$targetHeight = $targetHeight !== null ? min($targetHeight, $height) : null;
}
if ($crop) {
// Режим кропа: вырезаем область размером targetWidth x targetHeight
// Если targetHeight не указан — делаем квадрат по targetWidth
if ($targetHeight === null) {
$targetHeight = $targetWidth;
}
// Центрируем область вырезания, если x/y не заданы явно
if ($x === 0 && $y === 0) {
$x = (int)(($width - $targetWidth) / 2);
$y = (int)(($height - $targetHeight) / 2);
}
$srcWidth = $targetWidth;
$srcHeight = $targetHeight;
} else {
// Режим ресайза с сохранением пропорций
$ratio = $width / $height;
if ($targetHeight === null) {
// Только ширина задана — высота по пропорции
$targetHeight = (int)round($targetWidth / $ratio);
} elseif ($targetWidth === null || $targetWidth === 0) {
// Только высота задана — ширина по пропорции
$targetWidth = (int)round($targetHeight * $ratio);
} else {
// Заданы оба — вписываем в контейнер
$ratioW = $width / $targetWidth;
$ratioH = $height / $targetHeight;
if ($ratioW > $ratioH) {
$targetHeight = (int)round($targetWidth / $ratio);
} else {
$targetWidth = (int)round($targetHeight * $ratio);
}
}
$srcWidth = $width;
$srcHeight = $height;
}
$thumbnail = imagecreatetruecolor($targetWidth, $targetHeight);
// Сохраняем прозрачность для PNG, GIF и WebP
if (in_array($type, [3, 4, 18], true)) {
imagecolortransparent(
$thumbnail,
imagecolorallocate($thumbnail, 0, 0, 0)
);
if (in_array($type, [3, 18], true)) {
imagealphablending($thumbnail, false);
imagesavealpha($thumbnail, true);
}
}
imagecopyresampled(
$thumbnail,
$image,
$dst_x,
$dst_y,
$x,
$y,
$targetWidth,
$targetHeight,
$srcWidth,
$srcHeight
);
// Принудительный экспорт в WebP
if ($outputFormat === 'webp' && function_exists('imagewebp')) {
if (!preg_match('/\.webp$/i', $dest)) {
$dest = preg_replace('/\.[^.]+$/', '.webp', $dest) ?? ($dest . '.webp');
}
return imagewebp($thumbnail, $dest, $quality);
}
return call_user_func(
$image_handlers[$type]['save'],
$thumbnail,
$dest,
$image_handlers[$type]['quality'] ?? null
);
}
}