foreach ($images as $image) {
$name = explode('.', $image);
$webp = new Imagick($image);
$webp->setImageFormat('webp');
$webp->writeImage($name[0] . '.webp');
}
$dir = '/var/www/html/uploads';
$exts = ['png', 'gif', 'jpg', 'jpeg'];
$images = new DirectoryIterator($dir);
foreach ($images as $image) {
if ($image->isFile() && in_array($image->getExtension(), $exts, true)) {
$name = explode('.', $image->getFilename());
$webp = new Imagick($dir . DIRECTORY_SEPARATOR . $image);
$webp->setImageFormat('webp');
$webp->writeImage($dir . DIRECTORY_SEPARATOR . $name[0] . '.webp');
//unlink($dir . DIRECTORY_SEPARATOR . $image->getFilename()); // удаляем после обработки
}
}
if (isset($_FILES['file']['tmp_name'])) {
$image = imagecreatefromstring(file_get_contents($_FILES['file']['tmp_name']));
$name = time() . '_' . substr($_FILES['file']['name'], 0, -4);
imagejpeg($image, './img/' . $name . '.jpg');
for (
$initialQuantity = 50, $targetFileSize = 32 * 1024, $size = 2 * 1024 * 1024;
$targetFileSize < $size && $initialQuantity >= 10;
$initialQuantity -= 5
) {
ob_start();
imagewebp($image, null, $initialQuantity);
$image_data = ob_get_clean();
$size = strlen($image_data);
}
imagewebp($image, './img/mini/' . $name . '.webp', $initialQuantity);
}
// index.php
function download(string $filePath): void
{
if (!headers_sent()) {
ob_end_clean();
}
if (!file_exists($filePath)) {
throw new InvalidArgumentException('File not exists');
}
$filesize = filesize($filePath);
$fileObject = new SplFileObject($filePath, 'rb');
header('content-type: audio/mpeg');
$range = 0;
if (isset($_SERVER['HTTP_RANGE'])) {
$range = $_SERVER['HTTP_RANGE'];
$range = str_replace('bytes=', '', $range);
[$range] = explode('-', $range);
if (!empty($range)) {
$fileObject->fseek($range);
}
}
if ($range) {
header($_SERVER['SERVER_PROTOCOL'] . ' 206 Partial Content');
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
}
header('Accept-Ranges: bytes');
header('Content-Description: inline; File Transfer');
header('Content-Disposition: attachment; filename="' . basename($fileObject->getBasename()) . '";', false);
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . ($filesize - $range));
if ($range) {
header('Content-Range: bytes ' . $range . '-' . ($filesize - 1) . '/' . $filesize);
}
header('Connection: close');
$speed = 100;
while (!$fileObject->eof() && connection_status() === 0) {
echo $fileObject->fread(1024 * $speed);
flush();
#sleep(1); // разкоментирование приведет к ограничению скорости в 100 kb
}
}
download('./1.mp3');
<span class="green"><audio controls><source src="http://localhost/dnl/index.php"></audio></span>
Request URL: http://localhost/dnl/index.php
Request Method: GET
Status Code: 206 Partial Content
Remote Address: 127.0.0.1:80
Referrer Policy: strict-origin-when-cross-origin
Accept-Ranges: bytes
Connection: close
Content-Description: inline; File Transfer
Content-Disposition: attachment; filename="1.mp3";
Content-Length: 168977156
Content-Range: bytes 2097152-171074307/171074308
Content-Transfer-Encoding: binary
Content-Type: audio/mpeg
Date: Sat, 15 Apr 2023 19:03:29 GMT
Server: Apache
X-Content-Type-Options: nosniff
Accept: */*
Accept-Encoding: identity;q=1, *;q=0
Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,nb;q=0.6
Connection: keep-alive
DNT: 1
Host: localhost
Range: bytes=2097152-
Referer: http://localhost/dnl/index.html
sec-ch-ua: "Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Sec-Fetch-Dest: video
Sec-Fetch-Mode: no-cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36