-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
215 lines (175 loc) · 5.24 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
*
* @Date: 05/23/2024
* @Author: Max Base
* @URL: https://github.com/basemax
* @Copyright: asrez group <https://asrez.com>
* @Project: Toos Catalogue - Amir Reza Heydari
*
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
define('MY_APP', true);
//////////// CONFIGS ////////////
$currentUrl = "index.php"; // FEEL FREE TO CHANGE YOUR FULL URL without index.php such as: https://site.com/gallery/
$directoryPath = 'files' . DIRECTORY_SEPARATOR;
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
$thumbnailWidth = 70;
$thumbnailHeight = 70;
//////////// FUNCTIONS ////////////
function getDirectories(string $dir): array {
$result = [];
if (is_dir($dir)) {
$items = scandir($dir);
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
$result[] = $item;
}
}
}
return $result;
}
function findFirstImage(string $dir, array $allowedExtensions): ?string {
$dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$filePath = $dir . $file;
if (is_dir($filePath)) {
$result = findFirstImage($filePath, $allowedExtensions);
if ($result !== false) {
return $result;
}
} else {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if (in_array(strtolower($extension), $allowedExtensions)) {
return $filePath;
}
}
}
return false;
}
function listAllImages(string $dir, array $allowedExtensions): array {
global $directoryPath;
$images = [];
$dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$filePath = $dir . $file;
if (is_dir($filePath)) {
$images = array_merge($images, listAllImages($filePath, $allowedExtensions));
} else {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if (in_array(strtolower($extension), $allowedExtensions) && filesize($filePath) > 0) {
$images[] = $filePath;
}
}
}
return $images;
}
function createThumbnail(string $src, int $targetWidth, int $targetHeight): void {
$imageData = file_get_contents($src);
if ($imageData === false) {
die('Failed to read the image file.');
}
$image = imagecreatefromstring($imageData);
if ($image === false) {
die('Failed to create image from string.');
}
$originalWidth = imagesx($image);
$originalHeight = imagesy($image);
$originalAspect = $originalWidth / $originalHeight;
$thumbAspect = $targetWidth / $targetHeight;
if ($originalAspect >= $thumbAspect) {
$newHeight = $targetHeight;
$newWidth = $originalWidth / ($originalHeight / $targetHeight);
} else {
$newWidth = $targetWidth;
$newHeight = $originalHeight / ($originalWidth / $targetWidth);
}
$thumb = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($thumb, $image, 0 - ($newWidth - $targetWidth) / 2, 0 - ($newHeight - $targetHeight) / 2, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
header('Content-Type: image/jpeg');
imagejpeg($thumb, null, 80);
imagedestroy($image);
imagedestroy($thumb);
}
function sanitizePath(string $path): string {
$path = str_replace(array('./', '../', '.\\', '..\\'), '', $path);
return $path;
}
function endsWithAllowedExtension(string $filePath, array $allowedExtensions): bool {
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
return in_array(strtolower($extension), $allowedExtensions);
}
//////////// MAIN ////////////
// CROP ON THE FLY - PAGE
if (isset($_GET["image"])) {
$image = urldecode($_GET['image']);
$image = sanitizePath($image); // TO KEEP IT SAFE
if (strpos($image, $directoryPath) !== 0) {
header("Location: $currentUrl");
exit();
}
if (!endsWithAllowedExtension($image, $allowedExtensions)) {
header("Location: $currentUrl");
exit();
}
if (!is_file($image) || !file_exists($image)) {
header("Location: $currentUrl");
exit();
}
// var_dump($image);
// exit();
createThumbnail($image, $thumbnailWidth, $thumbnailHeight);
}
// CATEGORY PAGE
else if (isset($_GET["category"])) {
// $category = strtolower(trim(urldecode($_GET["category"])));
$category = urldecode($_GET["category"]);
if ($category === "") {
header("Location: $currentUrl");
exit();
}
$categories = getDirectories($directoryPath);
if (!in_array($category, $categories)) {
header("Location: $currentUrl");
exit();
}
$title = ucwords($category);
$allImages = listAllImages($directoryPath . $category, $allowedExtensions);
if (empty($allImages)) {
header("Location: $currentUrl");
exit();
}
$_allImages = $allImages;
require "layouts/category.php";
}
// HOME PAGE
else {
$_categories = [];
$categories = getDirectories($directoryPath);
foreach ($categories as $category_item) {
$categoryPath = $directoryPath . DIRECTORY_SEPARATOR . $category_item;
$categoryImage = findFirstImage($categoryPath, $allowedExtensions);
// Skip if no image found in the directory.
if ($categoryImage === false) continue;
$_categories[] = [
"dir" => $category_item,
"name" => ucwords($category_item),
"image" => $categoryImage,
];
}
require "layouts/home.php";
}