public/index.php aktualisiert
Some checks failed
release-tag / release-image (push) Has been cancelled
Some checks failed
release-tag / release-image (push) Has been cancelled
This commit is contained in:
@@ -68,15 +68,57 @@ function getCategories(): array {
|
|||||||
/**
|
/**
|
||||||
* Get the thumbnail image uri for the given category.
|
* Get the thumbnail image uri for the given category.
|
||||||
*/
|
*/
|
||||||
function getCategoryThumbnail(string $category): string {
|
function generateImageThumbnail(string $category, string $image): void {
|
||||||
$categoryImages = getCategoryImageFiles($category);
|
$imagePath = buildPath(IMAGES_DIR, $category, $image);
|
||||||
$firstImage = $categoryImages[0] ?? '';
|
$thumbPath = buildPath(THUMBS_DIR, $category, $image);
|
||||||
if (empty($firstImage)) {
|
|
||||||
return '';
|
if (file_exists($thumbPath)) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
generateImageThumbnail($category, $firstImage);
|
if (!file_exists($imagePath)) {
|
||||||
return "thumbs/{$category}/{$firstImage}";
|
error("Could not find image at {$imagePath}");
|
||||||
|
}
|
||||||
|
|
||||||
|
$thumbDir = dirname($thumbPath);
|
||||||
|
if (!file_exists($thumbDir)) {
|
||||||
|
mkdir($thumbDir, 0777, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$imageExtension = strtolower(pathinfo($imagePath, PATHINFO_EXTENSION));
|
||||||
|
|
||||||
|
switch ($imageExtension) {
|
||||||
|
case 'webp':
|
||||||
|
$originalImage = imagecreatefromwebp($imagePath);
|
||||||
|
break;
|
||||||
|
case 'jpg':
|
||||||
|
case 'jpeg':
|
||||||
|
$originalImage = imagecreatefromjpeg($imagePath);
|
||||||
|
break;
|
||||||
|
case 'png':
|
||||||
|
$originalImage = imagecreatefrompng($imagePath);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
error("Unsupported image format: {$imageExtension}");
|
||||||
|
}
|
||||||
|
|
||||||
|
$thumbImage = imagescale($originalImage, 1200);
|
||||||
|
|
||||||
|
switch ($imageExtension) {
|
||||||
|
case 'webp':
|
||||||
|
imagewebp($thumbImage, $thumbPath, 50);
|
||||||
|
break;
|
||||||
|
case 'jpg':
|
||||||
|
case 'jpeg':
|
||||||
|
imagejpeg($thumbImage, $thumbPath, 50);
|
||||||
|
break;
|
||||||
|
case 'png':
|
||||||
|
imagepng($thumbImage, $thumbPath, 6); // Compression level for PNG: 0-9
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
imagedestroy($originalImage);
|
||||||
|
imagedestroy($thumbImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -133,7 +175,13 @@ function categoryExists(string $category): bool {
|
|||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
function getCategoryImageFiles(string $category): array {
|
function getCategoryImageFiles(string $category): array {
|
||||||
$images = glob(buildPath(IMAGES_DIR, $category, '*.webp'));
|
$extensions = ['webp', 'jpg', 'jpeg', 'png'];
|
||||||
|
$images = [];
|
||||||
|
|
||||||
|
foreach ($extensions as $extension) {
|
||||||
|
$images = array_merge($images, glob(buildPath(IMAGES_DIR, $category, "*.{$extension}")));
|
||||||
|
}
|
||||||
|
|
||||||
return array_map(fn(string $dir) => basename($dir), $images);
|
return array_map(fn(string $dir) => basename($dir), $images);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,4 +279,5 @@ class Image {
|
|||||||
public string $uri,
|
public string $uri,
|
||||||
public string $thumb,
|
public string $thumb,
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
|
?>
|
Reference in New Issue
Block a user