2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class ResolutionLimit extends Extension
|
|
|
|
{
|
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 40;
|
|
|
|
} // early, to veto ImageUploadEvent
|
2012-01-27 18:16:46 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onImageAddition(ImageAdditionEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$min_w = $config->get_int("upload_min_width", -1);
|
|
|
|
$min_h = $config->get_int("upload_min_height", -1);
|
|
|
|
$max_w = $config->get_int("upload_max_width", -1);
|
|
|
|
$max_h = $config->get_int("upload_max_height", -1);
|
|
|
|
$ratios = explode(" ", $config->get_string("upload_ratios", ""));
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$image = $event->image;
|
2007-07-12 08:59:23 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($min_w > 0 && $image->width < $min_w) {
|
2020-10-26 15:18:37 +00:00
|
|
|
throw new UploadException("Post too small");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
if ($min_h > 0 && $image->height < $min_h) {
|
2020-10-26 15:18:37 +00:00
|
|
|
throw new UploadException("Post too small");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
if ($max_w > 0 && $image->width > $max_w) {
|
2020-10-26 15:18:37 +00:00
|
|
|
throw new UploadException("Post too large");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
if ($max_h > 0 && $image->height > $max_h) {
|
2020-10-26 15:18:37 +00:00
|
|
|
throw new UploadException("Post too large");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2008-02-06 15:54:50 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (count($ratios) > 0) {
|
|
|
|
$ok = false;
|
|
|
|
$valids = 0;
|
|
|
|
foreach ($ratios as $ratio) {
|
|
|
|
$parts = explode(":", $ratio);
|
|
|
|
if (count($parts) < 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$valids++;
|
2023-02-03 20:03:04 +00:00
|
|
|
$width = (int)$parts[0];
|
|
|
|
$height = (int)$parts[1];
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($image->width / $width == $image->height / $height) {
|
|
|
|
$ok = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($valids > 0 && !$ok) {
|
|
|
|
throw new UploadException(
|
2020-10-26 15:18:37 +00:00
|
|
|
"Post needs to be in one of these ratios: ".
|
2019-05-28 16:59:38 +00:00
|
|
|
html_escape($config->get_string("upload_ratios", ""))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-12 08:59:23 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
|
|
{
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Resolution Limits");
|
2007-07-12 08:59:23 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_label("Min ");
|
|
|
|
$sb->add_int_option("upload_min_width");
|
|
|
|
$sb->add_label(" x ");
|
|
|
|
$sb->add_int_option("upload_min_height");
|
|
|
|
$sb->add_label(" px");
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_label("<br>Max ");
|
|
|
|
$sb->add_int_option("upload_max_width");
|
|
|
|
$sb->add_label(" x ");
|
|
|
|
$sb->add_int_option("upload_max_height");
|
|
|
|
$sb->add_label(" px");
|
2007-07-12 08:59:23 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_label("<br>(-1 for no limit)");
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_label("<br>Ratios ");
|
|
|
|
$sb->add_text_option("upload_ratios");
|
|
|
|
$sb->add_label("<br>(eg. '4:3 16:9', blank for no limit)");
|
|
|
|
}
|
2007-07-12 08:59:23 +00:00
|
|
|
}
|