Added support for imgsize:X

This commit is contained in:
Marcin Kurczewski 2014-08-15 11:05:51 +02:00
parent 6014609a78
commit 4f68ee6097
2 changed files with 37 additions and 0 deletions

View file

@ -40,6 +40,10 @@ Command | Description
[search]datemax:...[/search] | posted on `...` or earlier (format like in `date:`) | `date_max` |
[search]filesizemin:7M[/search] | has size of at least 7 megabytes | `filesize_min` |
[search]filesizemax:30K[/search] | has size of at most 30 kilobytes | `filesize_max` |
[search]imgsize:huge[/search] | either dimension has at least 2001px | `img_size`, `imagesize`, `image_size` |
[search]imgsize:large[/search] | either dimension has at least 801px and at most 2000px | `img_size`, `imagesize`, `image_size` |
[search]imgsize:medium[/search] | either dimension has at least 301px and at most 800px | `img_size`, `imagesize`, `image_size` |
[search]imgsize:small[/search] | either dimension has at most 300px | `img_size`, `imagesize`, `image_size` |
[search]id:1,2,3[/search] | having specific post ID | `ids` |
[search]name:...[/search] | having specific post name (hash in full URLs) | `names`, `hash`, `hashes` |
[search]idmin:5[/search] | posts with ID greater than or equal to @5 | `id_min` |

View file

@ -174,6 +174,15 @@ class PostSearchParser extends AbstractSearchParser
return Sql\Functors::equalsOrLesser('post.file_size', new Sql\Binding($fileSizeMax));
}
elseif (in_array($key, ['imagesize', 'imgsize', 'image_size', 'img_size']))
{
list ($dimensionMin, $dimensionMax) = self::parseImageSize($value);
$select = 'MAX(post.image_width, post.image_height)';
return Sql\Functors::conjunction()
->add(Sql\Functors::equalsOrGreater(Sql\Statements::raw($select), $dimensionMin))
->add(Sql\Functors::equalsOrLesser(Sql\Statements::raw($select), $dimensionMax));
}
elseif ($key == 'special')
{
$activeUser = Auth::getCurrentUser();
@ -302,6 +311,30 @@ class PostSearchParser extends AbstractSearchParser
return true;
}
protected static function parseImageSize($value)
{
$sizes =
[
'huge' => 2001,
'large' => 801,
'medium' => 301,
'small' => 1
];
$value = strtolower(trim($value));
if (isset($sizes[$value]))
{
$dimensionMin = $sizes[$value];
$keys = array_keys($sizes);
$i = array_search($value, $keys) - 1;
$dimensionMax = $i >= 0 ? $sizes[$keys[$i]] - 1 : PHP_INT_MAX;
}
else
throw new SimpleException('Invalid format exception');
return [$dimensionMin, $dimensionMax];
}
protected static function parseDate($value)
{
$value = strtolower(trim($value));