Add optional images to slides
CI / Deploy / PHP 8.4 (push) Successful in 1m10s
CI / Deploy / PHP 8.5 (push) Successful in 1m16s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped

Slides (except YouTube) can carry one image. Uploads are validated, rotated
by EXIF orientation, scaled to 1600 px and stored as WebP outside webroot,
which also strips camera metadata. Images have an alt text (or are marked
decorative) and a layout: top, side or full screen (content slides).

Files are removed after the commit when a slide is deleted or its image is
replaced, with the poll, and `bin/cake images_cleanup` sweeps leftovers.
Duplicated slides get their own file. Docker image gains gd with WebP.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Sonnet 5.5 committed 2026-11-14 23:17:37 +01:00
1 parent 8c7818ee40
commit a395b50894
30 files changed
+1705 -11

No files matched your search

+54
View File
@@ -19,6 +19,11 @@ use Cake\ORM\Entity;
* @property int $position
* @property bool $is_hidden Prepared, but skipped in the presentation (never live)
* @property array<string, mixed>|null $settings
* @property string|null $image File name of the slide image in the poll's upload directory (see ImageService)
* @property string|null $image_alt Alternative text of the image, '' = decorative
* @property string|null $image_layout top|side|full, null = default of the type (see imageLayout())
* @property int|null $image_width Pixels of the stored (scaled) image
* @property int|null $image_height
* @property bool $is_voting_open
* @property \Cake\I18n\DateTime|null $voting_started_at Quiz: start of the voting window
* @property \Cake\I18n\DateTime|null $voting_ends_at Quiz: deadline (null = closed manually)
@@ -135,6 +140,20 @@ class Slide extends Entity
self::TYPE_ORDER => [],
];
/**
* Where the image of a slide goes: above the results, next to the text (presenter only,
* participants' phones always show it on top) or - content slides - as large as possible.
*/
public const IMAGE_TOP = 'top';
public const IMAGE_SIDE = 'side';
public const IMAGE_FULL = 'full';
public const IMAGE_LAYOUTS = [self::IMAGE_TOP, self::IMAGE_SIDE, self::IMAGE_FULL];
/**
* Longest alternative text.
*/
public const IMAGE_ALT_MAX = 255;
/**
* @var array<string, bool>
*/
@@ -146,6 +165,41 @@ class Slide extends Entity
'is_voting_open' => true,
];
/**
* Whether slides of this type can carry an image (everything but YouTube, which has its own media).
*
* @param string $type Slide type
* @return bool
*/
public static function supportsImage(string $type): bool
{
return $type !== self::TYPE_YOUTUBE;
}
/**
* @return bool
*/
public function hasImage(): bool
{
return $this->image !== null && $this->image !== '';
}
/**
* Layout of the image: the chosen one, otherwise by type - next to the text on content slides,
* above the question's answers elsewhere. "full" only exists for content slides.
*
* @return string One of IMAGE_LAYOUTS
*/
public function imageLayout(): string
{
$layout = $this->image_layout;
if (!in_array($layout, self::IMAGE_LAYOUTS, true) || ($layout === self::IMAGE_FULL && $this->type !== self::TYPE_CONTENT)) {
return $this->type === self::TYPE_CONTENT ? self::IMAGE_SIDE : self::IMAGE_TOP;
}
return $layout;
}
/**
* @return array<string, string> type => human readable label
*/
+18
View File
@@ -4,7 +4,11 @@ declare(strict_types=1);
namespace App\Model\Table;
use App\Model\Entity\Poll;
use App\Service\ImageService;
use ArrayObject;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
@@ -54,6 +58,20 @@ class PollsTable extends Table
]);
}
/**
* After the poll is gone for good, so are the image files of its slides (the slides remove
* their own; this also sweeps what a crash left behind).
*
* @param \Cake\Event\EventInterface<\Cake\ORM\Table> $event Event
* @param \Cake\Datasource\EntityInterface $entity Deleted poll
* @param \ArrayObject<string, mixed> $options Options
* @return void
*/
public function afterDeleteCommit(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
{
(new ImageService())->deletePoll((int)$entity->get('id'));
}
/**
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
+43
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Model\Table;
use App\Model\Entity\Slide;
use App\Service\ImageService;
use App\Service\YouTubeService;
use ArrayObject;
use Cake\Database\Expression\QueryExpression;
@@ -12,6 +13,7 @@ use Cake\Event\EventInterface;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Throwable;
/**
* @property \App\Model\Table\PollsTable&\Cake\ORM\Association\BelongsTo $Polls
@@ -109,6 +111,17 @@ class SlidesTable extends Table
}
}
/**
* Number of slides of a poll that carry an image (they count against `Plenario.images.maxPerPoll`).
*
* @param int $pollId Poll id
* @return int
*/
public function countImages(int $pollId): int
{
return $this->find()->where(['poll_id' => $pollId, 'image IS NOT' => null])->count();
}
/**
* Assign the next free position for new slides.
*
@@ -346,6 +359,36 @@ class SlidesTable extends Table
$copy->position = $slide->position + 1;
$copy->startFresh();
// The copy gets its own file, so deleting one slide never takes the image of the other.
$images = new ImageService();
$imageCopy = $slide->hasImage() ? $images->copy($slide->poll_id, (string)$slide->image) : null;
if ($imageCopy !== null) {
$copy->image = $imageCopy;
$copy->image_alt = $slide->image_alt;
$copy->image_layout = $slide->image_layout;
$copy->image_width = $slide->image_width;
$copy->image_height = $slide->image_height;
}
try {
return $this->persistCopy($slide, $copy);
} catch (Throwable $e) {
// Rolled back: the copied file belongs to no slide.
$images->delete($slide->poll_id, $imageCopy);
throw $e;
}
}
/**
* Saves a duplicated slide behind its original, together with the copies of the options.
*
* @param \App\Model\Entity\Slide $slide Original incl. slide_options
* @param \App\Model\Entity\Slide $copy Prepared copy
* @return \App\Model\Entity\Slide
*/
protected function persistCopy(Slide $slide, Slide $copy): Slide
{
return $this->getConnection()->transactional(function () use ($slide, $copy): Slide {
$this->updateAll(
['position' => new QueryExpression('position + 1')],