2025-03-17 22:22:23 +01:00
|
|
|
<?php
|
|
|
|
namespace Vichan\Service\Media;
|
|
|
|
|
2025-03-26 00:36:04 +01:00
|
|
|
use Vichan\Data\{MediaInstallResult, ThumbGenerationResult};
|
2025-03-25 23:49:17 +01:00
|
|
|
use Vichan\Functions\Metadata;
|
2025-03-17 22:22:23 +01:00
|
|
|
|
|
|
|
|
2025-03-28 15:26:06 +01:00
|
|
|
class MimeMapMediaHandler implements MediaHandler {
|
2025-03-25 23:49:17 +01:00
|
|
|
use MediaHandlerTrait;
|
|
|
|
|
2025-03-28 15:26:06 +01:00
|
|
|
/**
|
|
|
|
* @var callable(string): ?string
|
|
|
|
*/
|
|
|
|
private mixed $mime_mapper;
|
2025-03-17 22:22:23 +01:00
|
|
|
|
2025-03-25 22:19:13 +01:00
|
|
|
private static function getShape(string $file_path) {
|
|
|
|
$ret = \getimagesize($file_path);
|
|
|
|
if ($ret === false) {
|
2025-03-25 23:49:17 +01:00
|
|
|
throw new MediaException("Could not read image sizes of '$file_path'", MediaException::ERR_NO_OPEN);
|
2025-03-25 22:19:13 +01:00
|
|
|
}
|
|
|
|
if ($ret[2] == \IMAGETYPE_UNKNOWN) {
|
2025-03-25 23:49:17 +01:00
|
|
|
throw new \RuntimeException("Error '$file_path' is not an image", MediaException::ERR_BAD_MEDIA_TYPE);
|
2025-03-25 22:19:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$width = $ret[0];
|
|
|
|
$height = $ret[1];
|
|
|
|
$mime = $ret['mime'];
|
|
|
|
return [ $width, $height, $mime ];
|
|
|
|
}
|
|
|
|
|
2025-03-28 15:26:06 +01:00
|
|
|
private function generateThumbImpl(
|
|
|
|
string $source_file_mime,
|
|
|
|
int $max_width,
|
|
|
|
int $max_height
|
|
|
|
): ThumbGenerationResult {
|
|
|
|
$thumb_path = ($this->mime_mapper)($source_file_mime);
|
|
|
|
list($thumb_width, $thumb_height, $thumb_mime) = self::getShape($thumb_path);
|
|
|
|
|
|
|
|
return new ThumbGenerationResult(
|
|
|
|
$thumb_path,
|
|
|
|
$thumb_mime,
|
|
|
|
\min($thumb_width, $max_width),
|
|
|
|
\min($thumb_height, $max_height)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct(callable $mime_mapper) {
|
|
|
|
$this->mime_mapper = $mime_mapper;
|
2025-03-17 22:22:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function supportsMime(string $mime): bool {
|
2025-03-28 15:26:06 +01:00
|
|
|
return ($this->mime_mapper)($mime) !== null;
|
2025-03-17 22:22:23 +01:00
|
|
|
}
|
|
|
|
|
2025-03-18 11:51:36 +01:00
|
|
|
public function openHandle(string $file_path, string $file_mime, int $file_kind): mixed {
|
2025-03-20 17:38:47 +01:00
|
|
|
return [ $file_path, $file_mime, $file_kind ];
|
2025-03-18 14:24:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function closeHandle(mixed $handle) {
|
|
|
|
// No-op
|
|
|
|
}
|
|
|
|
|
|
|
|
public function installMediaAndGenerateThumb(
|
|
|
|
mixed $handle,
|
2025-03-28 11:21:10 +01:00
|
|
|
string $media_preferred_out_file_basepath,
|
|
|
|
string $thumb_preferred_out_file_basepath,
|
2025-03-18 14:24:32 +01:00
|
|
|
int $thumb_max_width,
|
|
|
|
int $thumb_max_height
|
2025-03-26 00:36:04 +01:00
|
|
|
): MediaInstallResult {
|
2025-03-20 17:38:47 +01:00
|
|
|
list($source_file_path, $source_file_mime, $source_file_kind) = $handle;
|
2025-03-28 11:21:10 +01:00
|
|
|
$out_path = $media_preferred_out_file_basepath . '.' . Metadata\mime_to_ext($source_file_mime);
|
2025-03-18 14:24:32 +01:00
|
|
|
|
2025-03-25 23:49:17 +01:00
|
|
|
$this->move_or_link_or_copy($source_file_kind, $source_file_path, $out_path);
|
2025-03-18 14:24:32 +01:00
|
|
|
|
2025-03-28 15:26:06 +01:00
|
|
|
$thumb = $this->generateThumbImpl(
|
|
|
|
$source_file_mime,
|
2025-03-18 14:24:32 +01:00
|
|
|
$thumb_max_width,
|
|
|
|
$thumb_max_height
|
|
|
|
);
|
2025-03-26 00:36:04 +01:00
|
|
|
return new MediaInstallResult($thumb, $out_path);
|
2025-03-18 11:31:23 +01:00
|
|
|
}
|
|
|
|
|
2025-03-17 22:22:23 +01:00
|
|
|
public function generateThumb(
|
2025-03-18 11:31:23 +01:00
|
|
|
mixed $handle,
|
2025-03-28 11:21:10 +01:00
|
|
|
string $preferred_out_file_basepath,
|
2025-03-17 22:22:23 +01:00
|
|
|
int $max_width,
|
|
|
|
int $max_height
|
|
|
|
): ThumbGenerationResult {
|
2025-03-28 15:26:06 +01:00
|
|
|
return $this->generateThumbImpl($handle[1], $max_width, $max_height);
|
2025-03-17 22:22:23 +01:00
|
|
|
}
|
|
|
|
}
|