-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
98 lines (84 loc) · 2.8 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width"/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/fancybox/fancybox.css"
/>
</head>
<body>
<div>
<?php
function createThumbnail($src, $dest, $thumbHeight) {
list($width, $height, $type) = getimagesize($src);
$aspectRatio = $width / $height;
$thumbWidth = $thumbHeight * $aspectRatio;
switch ($type) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($src);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($src);
break;
case IMAGETYPE_WEBP: // Support webp format
$image = imagecreatefromwebp($src);
break;
default:
return false; // Unsupported image type
}
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
switch ($type) {
case IMAGETYPE_JPEG:
imagejpeg($thumb, $dest);
break;
case IMAGETYPE_PNG:
imagepng($thumb, $dest);
break;
case IMAGETYPE_WEBP: // Support webp format
imagewebp($thumb, $dest);
break;
}
imagedestroy($thumb);
imagedestroy($image);
return true;
}
$galleryDir = "gallery/";
$thumbDir = "thumb/";
$allowedExtensions = array("jpg", "jpeg", "png", "gif", "webp"); // Remove "mp4" from allowed extensions
// Check and create the "thumb" directory if it doesn't exist
if (!file_exists($thumbDir)) {
mkdir($thumbDir);
}
$galleryFiles = scandir($galleryDir);
foreach ($galleryFiles as $file) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if (in_array(strtolower($extension), $allowedExtensions)) {
$imageURL = $galleryDir . $file;
$thumbURL = $thumbDir . $file;
if (!file_exists($thumbURL)) {
createThumbnail($imageURL, $thumbURL, 200); // The thumbnail will have a height of 200 pixels.
}
echo '
<a
data-fancybox="gallery"
data-src="' . $imageURL . '"
data-caption="' . $file . '"
>
<img src="' . $thumbURL . '" height="200" />
</a>
';
}
}
?>
</div>
<script src="https://cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/fancybox/fancybox.umd.js"></script>
<script>
Fancybox.bind('[data-fancybox="gallery"]', {
//
});
</script>
</body>
</html>