Add output image types for upload

This commit is contained in:
Visman 2023-07-12 16:39:44 +07:00
parent e928df83da
commit 9d0ec76e8b
6 changed files with 78 additions and 6 deletions

View file

@ -60,11 +60,10 @@ class Attachments extends Manager
$p2 = (int) ($id / 1000);
$p3 = \substr($name, 0, 235 - \strlen($ext)) . '_' . \sprintf("%03d", $id - $p2);
$path = "{$p1}/{$p2}/{$p3}.{$ext}";
$location = $this->c->DIR_PUBLIC . self::FOLDER . $path;
$dir = $this->c->DIR_PUBLIC . self::FOLDER . "{$p1}/{$p2}";
if (
! \is_dir($dir = \implode('/', \explode('/', $location, -1)))
! \is_dir($dir)
&& \mkdir($dir, 0755, true)
) {
\file_put_contents("{$dir}/index.html", self::HTML_CONT);
@ -75,12 +74,16 @@ class Attachments extends Manager
if ($file instanceof Image) {
$file->setQuality($this->c->config->i_upload_img_quality ?? 75)
->resize($this->c->config->i_upload_img_axis_limit, $this->c->config->i_upload_img_axis_limit);
if (! empty($this->c->config->s_upload_img_outf)) {
$ext = '(' . \strtr($this->c->config->s_upload_img_outf, [',' => '|']) . ')';
}
}
$status = $file->toFile($location);
$status = $file->toFile("{$dir}/{$p3}.{$ext}");
if (true !== $status) {
$this->c->Log->warning("Attachments Failed processing {$path}", [
$this->c->Log->warning("Attachments Failed processing {$p1}/{$p2}/{$p3}.{$ext}", [
'user' => $this->user->fLog(),
'error' => $file->error(),
]);
@ -95,7 +98,10 @@ class Attachments extends Manager
return null;
}
$size = $this->c->Files->size(\filesize($location), 'K');
$location = $file->path();
$path = "{$p1}/{$p2}/{$file->name()}.{$file->ext()}";
$size = $this->c->Files->size(\filesize($location), 'K');
$vars = [
':id' => $id,
':path' => $path,

View file

@ -1575,6 +1575,7 @@ class Install extends Admin
'b_upload' => 0,
'i_upload_img_quality' => 75,
'i_upload_img_axis_limit' => 1920,
's_upload_img_outf' => 'webp,jpg,png,gif',
];
foreach ($forkConfig as $name => $value) {

View file

@ -721,6 +721,12 @@ class Update extends Admin
*/
protected function stageNumber60(array $args): ?int
{
$config = $this->c->config;
$config->s_upload_img_outf = 'webp,jpg,png,gif';
$config->save();
$this->c->DB->addField('::users', 'u_up_size_mb', 'INT(10) UNSIGNED', false, 0);
return null;

View file

@ -32,9 +32,11 @@ class Uploads extends Admin
if ('POST' === $method) {
$v = $this->c->Validator->reset()
->addValidators([
'ext_check' => [$this, 'vExtsCheck'],
])->addRules([
'token' => 'token:AdminUploads',
'b_upload' => 'required|integer|in:0,1',
's_upload_img_outf' => 'required|string:trim|max:255|ext_check',
'i_upload_img_quality' => 'required|integer|min:0|max:100',
'i_upload_img_axis_limit' => 'required|integer|min:100|max:20000',
])->addAliases([
@ -44,6 +46,7 @@ class Uploads extends Admin
if ($v->validation($_POST)) {
$this->c->config->b_upload = $v->b_upload;
$this->c->config->s_upload_img_outf = $v->s_upload_img_outf;
$this->c->config->i_upload_img_quality = $v->i_upload_img_quality;
$this->c->config->i_upload_img_axis_limit = $v->i_upload_img_axis_limit;
$this->c->config->save();
@ -82,6 +85,14 @@ class Uploads extends Admin
'caption' => 'Uploads mode label',
'help' => ['Uploads mode help', __('User groups'), $this->c->Router->link('AdminGroups')],
],
's_upload_img_outf' => [
'required' => true,
'type' => 'text',
'maxlength' => '255',
'value' => $config->s_upload_img_outf,
'caption' => 'Output image types label',
'help' => 'Output image types help',
],
'i_upload_img_quality' => [
'type' => 'number',
'min' => '0',
@ -109,4 +120,40 @@ class Uploads extends Admin
],
];
}
/**
* Наводит порядок в расширениях
*/
public function vExtsCheck(Validator $v, string $exts): string
{
$allowed = [
'webp' => true,
'jpg' => true,
'jpeg' => 'jpg',
'png' => true,
'gif' => true,
'avif' => true,
];
$exts = \explode(',', \mb_strtolower($exts, 'UTF-8'));
$result = [];
foreach ($exts as $ext) {
$ext = \trim($ext);
if (isset($allowed[$ext])) {
if (\is_string($allowed[$ext])) {
$ext = $allowed[$ext];
}
$result[$ext] = $ext;
}
}
if (empty($result)) {
return 'webp';
} else {
return \implode(',', $result);
}
}
}

View file

@ -32,3 +32,9 @@ msgstr "Axis limit"
msgid "Upload axis limit help"
msgstr "The uploaded image will be resized if its width/height is greater than this limit."
msgid "Output image types label"
msgstr "Output image types"
msgid "Output image types help"
msgstr "Types of pictures to save to this site, separated by commas. If a picture of another type is loaded, it will be converted to the first type from this list."

View file

@ -32,3 +32,9 @@ msgstr "Ограничение по оси"
msgid "Upload axis limit help"
msgstr "К загруженному изображению будет применен ресайз, если его ширина/высота больше этого ограничения."
msgid "Output image types label"
msgstr "Вых. типы картинок"
msgid "Output image types help"
msgstr "Типы картинок для сохранения на этот сайт, через запятую. Если загружена картинка другого типа, то она будет конвертирована в первый тип из этого списка."