Add optional images to slides
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:
1 parent
8c7818ee40
commit
a395b50894
30 files changed
+1705
-11
No files matched your search
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Test\TestCase;
|
||||
|
||||
use Cake\Core\Configure;
|
||||
use Laminas\Diactoros\UploadedFile;
|
||||
|
||||
/**
|
||||
* Helpers for tests with slide images: generated pictures and a throw-away upload directory.
|
||||
* Call clearImages() from tearDown().
|
||||
*/
|
||||
trait ImageTestTrait
|
||||
{
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
protected array $imageFiles = [];
|
||||
|
||||
/**
|
||||
* Writes a picture to a temporary file.
|
||||
*
|
||||
* @param int $width Width in pixels
|
||||
* @param int $height Height in pixels
|
||||
* @param string $format jpeg|png|gif|webp
|
||||
* @return string Path
|
||||
*/
|
||||
protected function makeImage(int $width = 400, int $height = 300, string $format = 'jpeg'): string
|
||||
{
|
||||
$image = imagecreatetruecolor($width, $height);
|
||||
imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, (int)imagecolorallocate($image, 200, 40, 40));
|
||||
imagefilledellipse($image, intdiv($width, 2), intdiv($height, 2), intdiv($width, 2), intdiv($height, 2), (int)imagecolorallocate($image, 250, 250, 250));
|
||||
$path = tempnam(sys_get_temp_dir(), 'plen');
|
||||
match ($format) {
|
||||
'png' => imagepng($image, $path),
|
||||
'gif' => imagegif($image, $path),
|
||||
'webp' => imagewebp($image, $path),
|
||||
default => imagejpeg($image, $path),
|
||||
};
|
||||
$this->imageFiles[] = $path;
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts a form with an uploaded file in the field `image` (the test request carries files
|
||||
* separately from the form data).
|
||||
*
|
||||
* @param string $url URL
|
||||
* @param array<string, mixed> $data Form data
|
||||
* @param string|null $path File to upload, null = no upload field at all
|
||||
* @param int $error Upload error code (UPLOAD_ERR_NO_FILE = field left empty)
|
||||
* @param string $name Client file name
|
||||
* @return void
|
||||
*/
|
||||
protected function postWithImage(string $url, array $data, ?string $path, int $error = UPLOAD_ERR_OK, string $name = 'bild.jpg'): void
|
||||
{
|
||||
$this->_request['files'] = $path === null ? [] : [
|
||||
'image' => new UploadedFile($path, (int)filesize($path), $error, $name, 'image/jpeg'),
|
||||
];
|
||||
try {
|
||||
$this->post($url, $data);
|
||||
} finally {
|
||||
$this->_request['files'] = [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string The upload directory of the tests
|
||||
*/
|
||||
protected function uploadRoot(): string
|
||||
{
|
||||
return (string)Configure::read('Plenario.images.path');
|
||||
}
|
||||
|
||||
/**
|
||||
* Files stored for a poll.
|
||||
*
|
||||
* @param int $pollId Poll id
|
||||
* @return list<string> File names
|
||||
*/
|
||||
protected function storedImages(int $pollId): array
|
||||
{
|
||||
$files = array_map('basename', glob($this->uploadRoot() . DS . $pollId . DS . '*') ?: []);
|
||||
sort($files);
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes temporary pictures and the upload directory of the tests.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function clearImages(): void
|
||||
{
|
||||
foreach ($this->imageFiles as $file) {
|
||||
@unlink($file);
|
||||
}
|
||||
$this->imageFiles = [];
|
||||
foreach (glob($this->uploadRoot() . DS . '*', GLOB_ONLYDIR) ?: [] as $directory) {
|
||||
array_map('unlink', glob($directory . DS . '*') ?: []);
|
||||
@rmdir($directory);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user