<?php
declare(strict_types=1);
namespace classes;
use Gumlet\ImageResize;
use Gumlet\ImageResizeException;
class Storage
{
private ?array $storageCnf;
public function __construct(
protected $database,
protected ImgConverter $imgConverter
)
{
$this->storageCnf = require ROOT . '/config/storage.php';
}
public function createDir(): string
{
$dir = ROOT . '/' . $this->storageCnf['uploadDir'];
if (!file_exists($dir)) {
mkdir($dir, 0775);
}
return $dir;
}
public function setContent(array $list, array $images): void
{
$dir = $this->createDir();
if (count($list)) {
foreach ($list AS $item) {
$this->copyMultiple($dir, $item['images']);
}
} else {
$this->copyMultiple($dir, $images);
}
}
/**
* @throws ImageResizeException
*/
public function copyMultiple(string $dir, array $urls): void
{
$mh = curl_multi_init();
$conn = [];
$fp = [];
$paths = [];
foreach ($urls AS $i => $url) {
$file = basename($url);
$path = "$dir/$file";
$conn[$i] = curl_init($url);
$fp[$i] = fopen($path, 'w');
$paths[$i] = $path;
curl_setopt($conn[$i], CURLOPT_FILE, $fp[$i]);
curl_setopt($conn[$i], CURLOPT_HEADER, false);
curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT, 60);
curl_multi_add_handle($mh, $conn[$i]);
}
do {
curl_multi_exec($mh, $active);
}
while ($active);
$this->database->insert("INSERT INTO photosets (title) VALUES (?)", ['Название фотосета']);
$photoset_id = $this->database->lastId();
foreach ($urls AS $i => $url) {
curl_multi_remove_handle($mh, $conn[$i]);
curl_close($conn[$i]);
fclose($fp[$i]);
//$paths[$i] = $this->changeExtension($paths[$i]);
$this->resize($paths[$i]);
$path = $this->storageCnf['uploadDir'] . '/' . basename($paths[$i]);
$this->database->insert("INSERT INTO photos (photoset_id, path, extension) VALUES (?, ?, ?)", [$photoset_id, $path, 'webp']);
}
curl_multi_close($mh);
}
public function changeExtension(string $path): bool|string
{
if (!file_exists($path)) {
return false;
}
$pathinfo = pathinfo($path);
extract($pathinfo);
$outPath = $dirname . '/' . $filename . '.' . $this->storageCnf['extension'];
$result = $this
->imgConverter
->setImg($path)
->out($outPath, $this->storageCnf['extension']);
if ($result) {
unlink($path);
}
return $outPath;
}
/**
* @throws ImageResizeException
*/
public function resize(string $path): bool
{
if (!file_exists($path)) {
echo filesize($path) . '<br/s>';
return false;
}
if ($this->storageCnf['resizeHeight'] OR $this->storageCnf['resizeWidth']) {
$thumbPath = str_replace($this->storageCnf['uploadDir'], $this->storageCnf['uploadDir'] . '/thumbnail', $path);
$thumbDir = ROOT . '/' . $this->storageCnf['uploadDir'] . '/thumbnail';
if (!file_exists($thumbDir)) {
mkdir($thumbDir, 0775);
}
$resizer = (new ImageResize($path));
if ($this->storageCnf['resizeHeight'] AND empty($this->storageCnf['resizeWidth'])) {
$resizer->resizeToHeight($this->storageCnf['resizeHeight']);
} else if (empty($this->storageCnf['resizeHeight']) AND $this->storageCnf['resizeWidth']) {
$resizer->resizeToWidth($this->storageCnf['resizeWidth']);
} else {
$resizer->resize($this->storageCnf['resizeWidth'], $this->storageCnf['resizeHeight']);
}
$resizer->save($thumbPath);
return true;
}
return false;
}
}