PhotoMosaic: rewrite getRow to use availableLayouts

This commit is contained in:
Aaron van Geffen 2023-12-02 00:24:43 +01:00
parent 8700fc1417
commit d45b467bb1

View File

@ -8,11 +8,13 @@
class PhotoMosaic class PhotoMosaic
{ {
const NUM_DAYS_CUTOFF = 7;
private AssetIterator $iterator; private AssetIterator $iterator;
private $layouts; private $layouts;
private $processedImages = 0;
private $queue = []; private $queue = [];
const NUM_DAYS_CUTOFF = 7;
public function __construct(AssetIterator $iterator) public function __construct(AssetIterator $iterator)
{ {
$this->iterator = $iterator; $this->iterator = $iterator;
@ -31,7 +33,8 @@ class PhotoMosaic
'panorama' => [Image::TYPE_PANORAMA], 'panorama' => [Image::TYPE_PANORAMA],
// Big-small juxtapositions // Big-small juxtapositions
'portrait' => [Image::TYPE_PORTRAIT, Image::TYPE_LANDSCAPE, Image::TYPE_LANDSCAPE], 'portrait' => [Image::TYPE_PORTRAIT, Image::TYPE_LANDSCAPE, Image::TYPE_LANDSCAPE,
Image::TYPE_LANDSCAPE, Image::TYPE_LANDSCAPE],
'landscape' => [Image::TYPE_LANDSCAPE, Image::TYPE_LANDSCAPE, Image::TYPE_LANDSCAPE], 'landscape' => [Image::TYPE_LANDSCAPE, Image::TYPE_LANDSCAPE, Image::TYPE_LANDSCAPE],
// Single row of three // Single row of three
@ -79,87 +82,39 @@ class PhotoMosaic
public function getRow() public function getRow()
{ {
// Fetch the first image... $currentImages = [];
$image = $this->fetchImage(); $selectedLayout = null;
foreach ($this->layouts as $layout => $requiredImageTypes)
// No image at all?
if (!$image)
return false;
// Is it a panorama? Then we've got our row!
elseif ($image->isPanorama())
return [[$image], 'panorama'];
// Alright, let's initalise a proper row, then.
$photos = [$image];
$num_portrait = $image->isPortrait() ? 1 : 0;
$num_landscape = $image->isLandscape() ? 1 : 0;
// Get an initial batch of non-panorama images to work with.
for ($i = 1; $i < 3 && ($image = $this->fetchImage(Image::TYPE_LANDSCAPE | Image::TYPE_PORTRAIT, $image)); $i++)
{ {
$num_portrait += $image->isPortrait() ? 1 : 0; // Fetch images per the layout requirements
$num_landscape += $image->isLandscape() ? 1 : 0; // TODO: pass last image back into fetchImage for date cutoff purposes
$photos[] = $image; $currentImages = array_filter(array_map([$this, 'fetchImage'], $requiredImageTypes));
}
// Sort photos by priority and date captured. // Matching requirements?
usort($photos, self::orderPhotos(...)); if (count($currentImages) === count($requiredImageTypes))
// Three portraits?
if ($num_portrait === 3)
return [$photos, 'portraits'];
// At least one portrait?
if ($num_portrait >= 1)
{
// Grab two more landscapes, so we can put a total of four tiles on the side.
for ($i = 0; $image && $i < 2 && ($image = $this->fetchImage(Image::TYPE_LANDSCAPE | Image::TYPE_PORTRAIT, $image)); $i++)
$photos[] = $image;
// We prefer to have the portrait on the side, so prepare to process that first.
usort($photos, function($a, $b) {
if ($a->isPortrait() && !$b->isPortrait())
return -1;
elseif ($b->isPortrait() && !$a->isPortrait())
return 1;
else
return self::orderPhotos($a, $b);
});
// We might not have a full set of photos, but only bother if we have at least three.
if (count($photos) > 3)
return [$photos, 'portrait'];
}
// One landscape at least, hopefully?
if ($num_landscape >= 1)
{
if (count($photos) === 3)
{ {
// We prefer to have the landscape on the side, so prepare to process that first. $selectedLayout = $layout;
usort($photos, function($a, $b) { break;
if ($a->isLandscape() && !$b->isLandscape())
return -1;
elseif ($b->isLandscape() && !$a->isLandscape())
return 1;
else
return self::orderPhotos($a, $b);
});
return [$photos, 'landscape'];
} }
elseif (count($photos) === 2)
return [$photos, 'duo']; // Push mismatches back into the queue
else array_map([$this, 'pushToQueue'], $currentImages);
return [$photos, 'single']; $currentImages = [];
} }
// Last resort: majority vote if ($selectedLayout)
if ($num_portrait > $num_landscape) {
return [$photos, 'portraits']; // Hurray, we've got something that works
usort($currentImages, [$this, 'orderPhotos']);
$this->processedImages += count($currentImages);
return [$currentImages, $selectedLayout];
}
else else
return [$photos, 'landscapes']; {
// Ensure we have no images left in the iterator before giving up
assert($this->processedImages === $this->iterator->num());
return false;
}
} }
private static function matchTypeMask(Image $image, $type_mask) private static function matchTypeMask(Image $image, $type_mask)
@ -171,13 +126,13 @@ class PhotoMosaic
private static function orderPhotos(Image $a, Image $b) private static function orderPhotos(Image $a, Image $b)
{ {
// Show images of highest priority first. // Leave images of different types as-is
$priority_diff = $a->getPriority() - $b->getPriority(); if ($a->isLandscape() !== $b->isLandscape())
if ($priority_diff !== 0) return 0;
return -$priority_diff;
// In other cases, we'll just show the newest first. // Otherwise, show images of highest priority first
return $a->getDateCaptured() <=> $b->getDateCaptured(); $priority_diff = $a->getPriority() - $b->getPriority();
return -$priority_diff;
} }
private function pushToQueue(Image $image) private function pushToQueue(Image $image)