2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2023-01-10 22:44:09 +00:00
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
2018-11-05 22:30:18 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Misc functions *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a directory full of images
|
|
|
|
*
|
2019-06-14 14:34:37 +00:00
|
|
|
* @param string $base
|
2024-01-05 13:53:21 +00:00
|
|
|
* @return UploadResult[]
|
2018-11-05 22:30:18 +00:00
|
|
|
*/
|
2024-01-04 15:54:44 +00:00
|
|
|
function add_dir(string $base, ?array $extra_tags = []): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$results = [];
|
2018-11-05 22:30:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
foreach (list_files($base) as $full_path) {
|
|
|
|
$short_path = str_replace($base, "", $full_path);
|
|
|
|
$filename = basename($full_path);
|
2018-11-05 22:30:18 +00:00
|
|
|
|
2024-01-04 15:54:44 +00:00
|
|
|
$tags = array_merge(path_to_tags($short_path), $extra_tags);
|
2019-05-28 16:59:38 +00:00
|
|
|
try {
|
2024-01-05 13:53:21 +00:00
|
|
|
$dae = add_image($full_path, $filename, $tags);
|
|
|
|
foreach($dae->images as $image) {
|
|
|
|
$results[] = new UploadSuccess($filename, $image->id);
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
} catch (UploadException $ex) {
|
2024-01-05 13:53:21 +00:00
|
|
|
$results[] = new UploadError($filename, $ex->getMessage());
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-05 22:30:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
return $results;
|
2018-11-05 22:30:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 14:34:37 +00:00
|
|
|
/**
|
|
|
|
* Sends a DataUploadEvent for a file.
|
|
|
|
*/
|
2024-01-04 15:54:44 +00:00
|
|
|
function add_image(string $tmpname, string $filename, array $tags, ?string $source = null): DataUploadEvent
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-02-22 23:37:37 +00:00
|
|
|
return send_event(new DataUploadEvent($tmpname, [
|
|
|
|
'filename' => pathinfo($filename, PATHINFO_BASENAME),
|
2024-01-04 15:54:44 +00:00
|
|
|
'tags' => $tags,
|
2023-02-22 23:37:37 +00:00
|
|
|
'source' => $source,
|
|
|
|
]));
|
|
|
|
}
|
2020-06-02 23:05:09 +00:00
|
|
|
|
2023-02-22 23:37:37 +00:00
|
|
|
function get_file_ext(string $filename): ?string
|
|
|
|
{
|
|
|
|
return pathinfo($filename)['extension'] ?? null;
|
2019-06-12 22:35:11 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 22:30:18 +00:00
|
|
|
/**
|
|
|
|
* Given a full size pair of dimensions, return a pair scaled down to fit
|
2019-06-14 14:34:37 +00:00
|
|
|
* into the configured thumbnail square, with ratio intact.
|
|
|
|
* Optionally uses the High-DPI scaling setting to adjust the final resolution.
|
2018-11-05 22:30:18 +00:00
|
|
|
*
|
2019-06-14 14:34:37 +00:00
|
|
|
* @param int $orig_width
|
|
|
|
* @param int $orig_height
|
|
|
|
* @param bool $use_dpi_scaling Enables the High-DPI scaling.
|
|
|
|
* @return array
|
2018-11-05 22:30:18 +00:00
|
|
|
*/
|
2019-06-14 14:34:37 +00:00
|
|
|
function get_thumbnail_size(int $orig_width, int $orig_height, bool $use_dpi_scaling = false): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
2018-11-05 22:30:18 +00:00
|
|
|
|
2020-06-11 21:58:19 +00:00
|
|
|
$fit = $config->get_string(ImageConfig::THUMB_FIT);
|
|
|
|
|
2020-08-28 14:15:45 +00:00
|
|
|
if (in_array($fit, [
|
|
|
|
Media::RESIZE_TYPE_FILL,
|
|
|
|
Media::RESIZE_TYPE_STRETCH,
|
|
|
|
Media::RESIZE_TYPE_FIT_BLUR,
|
|
|
|
Media::RESIZE_TYPE_FIT_BLUR_PORTRAIT
|
|
|
|
])) {
|
2020-06-11 21:58:19 +00:00
|
|
|
return [$config->get_int(ImageConfig::THUMB_WIDTH), $config->get_int(ImageConfig::THUMB_HEIGHT)];
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($orig_width === 0) {
|
|
|
|
$orig_width = 192;
|
|
|
|
}
|
|
|
|
if ($orig_height === 0) {
|
|
|
|
$orig_height = 192;
|
|
|
|
}
|
2018-11-05 22:30:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($orig_width > $orig_height * 5) {
|
|
|
|
$orig_width = $orig_height * 5;
|
|
|
|
}
|
|
|
|
if ($orig_height > $orig_width * 5) {
|
|
|
|
$orig_height = $orig_width * 5;
|
|
|
|
}
|
2018-11-05 22:30:18 +00:00
|
|
|
|
|
|
|
|
2019-09-29 13:30:55 +00:00
|
|
|
if ($use_dpi_scaling) {
|
2019-06-18 18:45:59 +00:00
|
|
|
list($max_width, $max_height) = get_thumbnail_max_size_scaled();
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
2019-06-18 18:45:59 +00:00
|
|
|
$max_width = $config->get_int(ImageConfig::THUMB_WIDTH);
|
|
|
|
$max_height = $config->get_int(ImageConfig::THUMB_HEIGHT);
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 18:45:59 +00:00
|
|
|
$output = get_scaled_by_aspect_ratio($orig_width, $orig_height, $max_width, $max_height);
|
2019-06-09 18:22:48 +00:00
|
|
|
|
2019-06-18 18:45:59 +00:00
|
|
|
if ($output[2] > 1 && $config->get_bool('thumb_upscale')) {
|
2019-06-09 18:22:48 +00:00
|
|
|
return [(int)$orig_width, (int)$orig_height];
|
|
|
|
} else {
|
2019-06-18 18:45:59 +00:00
|
|
|
return $output;
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:32:47 +00:00
|
|
|
function get_scaled_by_aspect_ratio(int $original_width, int $original_height, int $max_width, int $max_height): array
|
2019-06-09 18:22:48 +00:00
|
|
|
{
|
2023-11-11 21:49:12 +00:00
|
|
|
$xscale = ($max_width / $original_width);
|
|
|
|
$yscale = ($max_height / $original_height);
|
2019-06-09 18:22:48 +00:00
|
|
|
|
2019-06-18 18:45:59 +00:00
|
|
|
$scale = ($yscale < $xscale) ? $yscale : $xscale ;
|
2019-06-09 18:22:48 +00:00
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
return [(int)($original_width * $scale), (int)($original_height * $scale), $scale];
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 14:34:37 +00:00
|
|
|
/**
|
2019-06-18 18:45:59 +00:00
|
|
|
* Fetches the thumbnails height and width settings and applies the High-DPI scaling setting before returning the dimensions.
|
2019-06-14 14:34:37 +00:00
|
|
|
*
|
|
|
|
* @return array [width, height]
|
|
|
|
*/
|
2019-06-18 18:45:59 +00:00
|
|
|
function get_thumbnail_max_size_scaled(): array
|
2019-06-09 18:22:48 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
2019-06-18 18:45:59 +00:00
|
|
|
$scaling = $config->get_int(ImageConfig::THUMB_SCALING);
|
2023-11-11 21:49:12 +00:00
|
|
|
$max_width = $config->get_int(ImageConfig::THUMB_WIDTH) * ($scaling / 100);
|
|
|
|
$max_height = $config->get_int(ImageConfig::THUMB_HEIGHT) * ($scaling / 100);
|
2019-06-18 18:45:59 +00:00
|
|
|
return [$max_width, $max_height];
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-14 16:05:55 +00:00
|
|
|
function create_image_thumb(string $hash, string $mime, string $engine = null)
|
2019-09-29 13:30:55 +00:00
|
|
|
{
|
2020-06-11 21:58:19 +00:00
|
|
|
global $config;
|
|
|
|
|
2020-02-23 22:12:18 +00:00
|
|
|
$inname = warehouse_path(Image::IMAGE_DIR, $hash);
|
2019-06-18 18:45:59 +00:00
|
|
|
$outname = warehouse_path(Image::THUMBNAIL_DIR, $hash);
|
|
|
|
$tsize = get_thumbnail_max_size_scaled();
|
2020-06-11 21:58:19 +00:00
|
|
|
create_scaled_image(
|
|
|
|
$inname,
|
|
|
|
$outname,
|
|
|
|
$tsize,
|
2020-06-14 16:05:55 +00:00
|
|
|
$mime,
|
2020-06-11 21:58:19 +00:00
|
|
|
$engine,
|
|
|
|
$config->get_string(ImageConfig::THUMB_FIT)
|
|
|
|
);
|
2020-02-23 22:12:18 +00:00
|
|
|
}
|
2019-06-18 18:45:59 +00:00
|
|
|
|
2020-06-11 21:58:19 +00:00
|
|
|
|
|
|
|
|
2020-06-14 16:05:55 +00:00
|
|
|
function create_scaled_image(string $inname, string $outname, array $tsize, string $mime, ?string $engine = null, ?string $resize_type = null)
|
2020-02-23 22:12:18 +00:00
|
|
|
{
|
|
|
|
global $config;
|
2019-09-29 13:30:55 +00:00
|
|
|
if (empty($engine)) {
|
2019-06-18 18:45:59 +00:00
|
|
|
$engine = $config->get_string(ImageConfig::THUMB_ENGINE);
|
|
|
|
}
|
2020-06-11 21:58:19 +00:00
|
|
|
if (empty($resize_type)) {
|
|
|
|
$resize_type = $config->get_string(ImageConfig::THUMB_FIT);
|
|
|
|
}
|
2019-06-18 18:45:59 +00:00
|
|
|
|
2020-06-14 16:05:55 +00:00
|
|
|
$output_mime = $config->get_string(ImageConfig::THUMB_MIME);
|
2019-06-25 23:43:57 +00:00
|
|
|
|
2019-06-24 15:05:16 +00:00
|
|
|
send_event(new MediaResizeEvent(
|
2019-06-18 18:45:59 +00:00
|
|
|
$engine,
|
|
|
|
$inname,
|
2020-06-14 16:05:55 +00:00
|
|
|
$mime,
|
2019-06-18 18:45:59 +00:00
|
|
|
$outname,
|
|
|
|
$tsize[0],
|
|
|
|
$tsize[1],
|
2020-06-11 21:58:19 +00:00
|
|
|
$resize_type,
|
2020-06-14 16:05:55 +00:00
|
|
|
$output_mime,
|
2020-06-14 16:36:52 +00:00
|
|
|
$config->get_string(ImageConfig::THUMB_ALPHA_COLOR),
|
2019-06-18 18:45:59 +00:00
|
|
|
$config->get_int(ImageConfig::THUMB_QUALITY),
|
|
|
|
true,
|
2020-06-11 21:58:19 +00:00
|
|
|
true
|
2019-06-18 18:45:59 +00:00
|
|
|
));
|
2019-06-12 22:35:11 +00:00
|
|
|
}
|
2020-10-08 22:07:33 +00:00
|
|
|
|
|
|
|
function redirect_to_next_image(Image $image): void
|
|
|
|
{
|
|
|
|
global $page;
|
|
|
|
|
|
|
|
if (isset($_GET['search'])) {
|
2023-12-24 21:12:59 +00:00
|
|
|
$search_terms = Tag::explode($_GET['search']);
|
2020-10-08 22:07:33 +00:00
|
|
|
$query = "search=" . url_escape($_GET['search']);
|
|
|
|
} else {
|
|
|
|
$search_terms = [];
|
|
|
|
$query = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$target_image = $image->get_next($search_terms);
|
|
|
|
|
2023-06-27 16:45:35 +00:00
|
|
|
if ($target_image === null) {
|
2023-08-18 12:38:55 +00:00
|
|
|
$redirect_target = referer_or(search_link(), ['post/view']);
|
2020-10-08 22:07:33 +00:00
|
|
|
} else {
|
|
|
|
$redirect_target = make_link("post/view/{$target_image->id}", null, $query);
|
|
|
|
}
|
|
|
|
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect($redirect_target);
|
|
|
|
}
|