Auto commit: 2025-12-18T00:54:07.143Z
This commit is contained in:
parent
0244805935
commit
7dedde4564
@ -1,2 +0,0 @@
|
|||||||
RewriteEngine On
|
|
||||||
RewriteRule ^test_redirect$ /index.php [R=302,L]
|
|
||||||
125
includes/ImageProcessor.php
Normal file
125
includes/ImageProcessor.php
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class ImageProcessor {
|
||||||
|
private static $uploadDir = 'assets/images/uploads/';
|
||||||
|
private static $maxWidth = 400; // Default max width for thumbnails
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the uploaded image, moves it to the upload directory, and resizes it.
|
||||||
|
*
|
||||||
|
* @param array $file The $_FILES array entry for the uploaded file.
|
||||||
|
* @return string|false The path to the saved thumbnail or false on error.
|
||||||
|
*/
|
||||||
|
public static function processAndSaveImage(array $file) {
|
||||||
|
if (!isset($file['tmp_name']) || $file['error'] !== UPLOAD_ERR_OK) {
|
||||||
|
return false; // No file uploaded or an error occurred
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate file type
|
||||||
|
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
|
||||||
|
if (!in_array($file['type'], $allowedTypes)) {
|
||||||
|
return false; // Invalid file type
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure upload directory exists
|
||||||
|
if (!is_dir(self::$uploadDir)) {
|
||||||
|
mkdir(self::$uploadDir, 0775, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a unique file name
|
||||||
|
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
|
||||||
|
$fileName = uniqid('thumbnail_') . '.' . $extension;
|
||||||
|
$targetPath = self::$uploadDir . $fileName;
|
||||||
|
|
||||||
|
// Move the uploaded file
|
||||||
|
if (!move_uploaded_file($file['tmp_name'], $targetPath)) {
|
||||||
|
return false; // Failed to move uploaded file
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resize image
|
||||||
|
if (!self::resizeImage($targetPath, $file['type'])) {
|
||||||
|
// If resizing fails, you might want to delete the original uploaded file
|
||||||
|
unlink($targetPath);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $targetPath; // Return the path to the saved and resized image
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resizes an image to a maximum width, maintaining aspect ratio.
|
||||||
|
*
|
||||||
|
* @param string $imagePath The path to the image file.
|
||||||
|
* @param string $mimeType The MIME type of the image.
|
||||||
|
* @return bool True on success, false on failure.
|
||||||
|
*/
|
||||||
|
private static function resizeImage(string $imagePath, string $mimeType) {
|
||||||
|
list($width, $height) = getimagesize($imagePath);
|
||||||
|
|
||||||
|
if ($width <= self::$maxWidth) {
|
||||||
|
return true; // No resizing needed
|
||||||
|
}
|
||||||
|
|
||||||
|
$newWidth = self::$maxWidth;
|
||||||
|
$newHeight = (int) (self::$maxWidth * ($height / $width));
|
||||||
|
|
||||||
|
$image = null;
|
||||||
|
switch ($mimeType) {
|
||||||
|
case 'image/jpeg':
|
||||||
|
$image = imagecreatefromjpeg($imagePath);
|
||||||
|
break;
|
||||||
|
case 'image/png':
|
||||||
|
$image = imagecreatefrompng($imagePath);
|
||||||
|
break;
|
||||||
|
case 'image/gif':
|
||||||
|
$image = imagecreatefromgif($imagePath);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return false; // Unsupported image type for resizing
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$image) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
|
||||||
|
|
||||||
|
// Preserve transparency for PNG and GIF
|
||||||
|
if ($mimeType == 'image/png') {
|
||||||
|
imagealphablending($resizedImage, false);
|
||||||
|
imagesavealpha($resizedImage, true);
|
||||||
|
$transparent = imagecolorallocatealpha($resizedImage, 255, 255, 255, 127);
|
||||||
|
imagefilledrectangle($resizedImage, 0, 0, $newWidth, $newHeight, $transparent);
|
||||||
|
} elseif ($mimeType == 'image/gif') {
|
||||||
|
$trnprt_indx = imagecolortransparent($image);
|
||||||
|
if ($trnprt_indx >= 0) {
|
||||||
|
// Get the transparent color from the original image
|
||||||
|
$trnprt_color = imagecolorsforindex($image, $trnprt_indx);
|
||||||
|
// Allocate the same color in the new image
|
||||||
|
$trnprt_indx = imagecolorallocate($resizedImage, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
|
||||||
|
imagefill($resizedImage, 0, 0, $trnprt_indx);
|
||||||
|
imagecolortransparent($resizedImage, $trnprt_indx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
||||||
|
|
||||||
|
$success = false;
|
||||||
|
switch ($mimeType) {
|
||||||
|
case 'image/jpeg':
|
||||||
|
$success = imagejpeg($resizedImage, $imagePath, 90); // 90% quality
|
||||||
|
break;
|
||||||
|
case 'image/png':
|
||||||
|
$success = imagepng($resizedImage, $imagePath);
|
||||||
|
break;
|
||||||
|
case 'image/gif':
|
||||||
|
$success = imagegif($resizedImage, $imagePath);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
imagedestroy($image);
|
||||||
|
imagedestroy($resizedImage);
|
||||||
|
|
||||||
|
return $success;
|
||||||
|
}
|
||||||
|
}
|
||||||
36
index.php
36
index.php
@ -25,25 +25,6 @@
|
|||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<?php
|
|
||||||
$debug_current_category_id = $current_category_id ?? "NULL";
|
|
||||||
$debug_current_category_name = $current_category_name ?? "Not Set";
|
|
||||||
$debug_current_links_count = isset($current_links) ? count($current_links) : 0;
|
|
||||||
$debug_sql_query = "SQL Query not set yet.";
|
|
||||||
if (isset($link_stmt) && $link_stmt instanceof PDOStatement) {
|
|
||||||
$debug_sql_query = $link_stmt->queryString;
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
<pre style="background-color: #fdd; border: 1px solid #f99; padding: 10px; margin: 10px;">
|
|
||||||
DEBUG INFORMATION:
|
|
||||||
GET Parameters: <?php echo htmlspecialchars(json_encode($_GET, JSON_PRETTY_PRINT)); ?>
|
|
||||||
Current Category ID: <?php echo htmlspecialchars($debug_current_category_id); ?>
|
|
||||||
Current Category Name: <?php echo htmlspecialchars($debug_current_category_name); ?>
|
|
||||||
Current Links Count: <?php echo htmlspecialchars($debug_current_links_count); ?>
|
|
||||||
Raw Query String: <?php echo htmlspecialchars($_SERVER['QUERY_STRING'] ?? 'Not Set'); ?>
|
|
||||||
SQL Query for links: <?php echo htmlspecialchars($debug_sql_query); ?>
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/db/config.php';
|
require_once __DIR__ . '/db/config.php';
|
||||||
|
|
||||||
@ -71,9 +52,9 @@ if (isset($_GET['category']) && filter_var($_GET['category'], FILTER_VALIDATE_IN
|
|||||||
$link_stmt = null;
|
$link_stmt = null;
|
||||||
if ($current_category_id) {
|
if ($current_category_id) {
|
||||||
$link_stmt = $pdo->prepare(
|
$link_stmt = $pdo->prepare(
|
||||||
"SELECT l.*, s.name as subcategory_name FROM links l " .
|
"SELECT l.*, s.name as subcategory_name FROM links l " .
|
||||||
"JOIN subcategories s ON l.subcategory_id = s.id " .
|
"JOIN subcategories s ON l.subcategory_id = s.id " .
|
||||||
"WHERE s.category_id = ? AND l.status = \'approved\' ORDER BY s.name ASC, l.created_at DESC"
|
"WHERE s.category_id = ? AND l.status = 'approved' ORDER BY s.name ASC, l.created_at DESC"
|
||||||
);
|
);
|
||||||
$link_stmt->execute([$current_category_id]);
|
$link_stmt->execute([$current_category_id]);
|
||||||
} else {
|
} else {
|
||||||
@ -87,7 +68,16 @@ if ($current_category_id) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
$current_links = $link_stmt->fetchAll();
|
$current_links = $link_stmt->fetchAll();
|
||||||
|
?>
|
||||||
|
<pre style="background-color: #fdd; border: 1px solid #f99; padding: 10px; margin: 10px;">
|
||||||
|
DEBUG INFORMATION:
|
||||||
|
GET Parameters: <?php echo htmlspecialchars(json_encode($_GET, JSON_PRETTY_PRINT)); ?>
|
||||||
|
Current Category ID: <?php echo htmlspecialchars($current_category_id ?? "NULL"); ?>
|
||||||
|
Current Category Name: <?php echo htmlspecialchars($current_category_name ?? "Not Set"); ?>
|
||||||
|
Current Links Count: <?php echo htmlspecialchars(isset($current_links) ? count($current_links) : 0); ?>
|
||||||
|
Raw Query String: <?php echo htmlspecialchars($_SERVER['QUERY_STRING'] ?? 'Not Set'); ?>
|
||||||
|
SQL Query for links: <?php echo htmlspecialchars($link_stmt->queryString ?? "SQL Query not set yet."); ?>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
15
submit.php
15
submit.php
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
require_once __DIR__ . '/db/config.php';
|
require_once __DIR__ . '/db/config.php';
|
||||||
|
require_once __DIR__ . '/includes/ImageProcessor.php';
|
||||||
|
|
||||||
if (!isset($_SESSION['user_id'])) {
|
if (!isset($_SESSION['user_id'])) {
|
||||||
header("Location: login.php");
|
header("Location: login.php");
|
||||||
@ -39,6 +40,15 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|||||||
// For now, thumbnail is not implemented
|
// For now, thumbnail is not implemented
|
||||||
$thumbnail_url = null;
|
$thumbnail_url = null;
|
||||||
|
|
||||||
|
if (isset($_FILES['thumbnail']) && $_FILES['thumbnail']['error'] === UPLOAD_ERR_OK) {
|
||||||
|
$uploadedImagePath = ImageProcessor::processAndSaveImage($_FILES['thumbnail']);
|
||||||
|
if ($uploadedImagePath) {
|
||||||
|
$thumbnail_url = $uploadedImagePath;
|
||||||
|
} else {
|
||||||
|
$errors[] = 'Failed to process uploaded image. Please ensure it is a valid image file (JPEG, PNG, GIF).';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$stmt = $pdo->prepare("INSERT INTO links (user_id, subcategory_id, title, url, description, thumbnail_url, status) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
$stmt = $pdo->prepare("INSERT INTO links (user_id, subcategory_id, title, url, description, thumbnail_url, status) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
||||||
$stmt->execute([$_SESSION['user_id'], $subcategory_id, $title, $url, $description, $thumbnail_url, $status]);
|
$stmt->execute([$_SESSION['user_id'], $subcategory_id, $title, $url, $description, $thumbnail_url, $status]);
|
||||||
@ -90,7 +100,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|||||||
<p class="mb-0">Thank you for your submission! It will be reviewed shortly.</p>
|
<p class="mb-0">Thank you for your submission! It will be reviewed shortly.</p>
|
||||||
</div>
|
</div>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<form action="submit.php" method="POST">
|
<form action="submit.php" method="POST" enctype="multipart/form-data">
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="title" class="form-label">Title</label>
|
<label for="title" class="form-label">Title</label>
|
||||||
<input type="text" class="form-control" id="title" name="title" required>
|
<input type="text" class="form-control" id="title" name="title" required>
|
||||||
@ -123,6 +133,9 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|||||||
<label for="description" class="form-label">Description</label>
|
<label for="description" class="form-label">Description</label>
|
||||||
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
|
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="thumbnail" class="form-label">Image (optional)</label>
|
||||||
|
<input type="file" class="form-control" id="thumbnail" name="thumbnail" accept="image/*">
|
||||||
<button type="submit" class="btn btn-primary">Submit Link</button>
|
<button type="submit" class="btn btn-primary">Submit Link</button>
|
||||||
</form>
|
</form>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user