| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- /**
- * Fix broken thumbnails in database
- * This script removes thumbnail_filename for images that don't have actual thumbnail files
- */
- require_once '../includes/config.php';
- require_once '../includes/database.php';
- // Create uploads directory if it doesn't exist
- $thumbsDir = '../uploads/images/thumbs';
- if (!file_exists($thumbsDir)) {
- mkdir($thumbsDir, 0755, true);
- }
- $db = Database::getInstance();
- echo "<h1>Fixing Broken Thumbnails</h1>\n";
- // Get all images with thumbnail filenames
- $images = $db->fetchAll("SELECT id, filename, thumbnail_filename FROM images WHERE thumbnail_filename IS NOT NULL AND thumbnail_filename != ''");
- echo "<p>Found " . count($images) . " images with thumbnail filenames</p>\n";
- $fixed = 0;
- $removed = 0;
- foreach ($images as $image) {
- $thumbPath = $thumbsDir . '/' . $image['thumbnail_filename'];
-
- if (!file_exists($thumbPath)) {
- echo "<p>Thumbnail file missing for image ID {$image['id']}: {$image['thumbnail_filename']}</p>\n";
-
- // Remove thumbnail filename from database (fallback to original)
- $db->update('images', ['thumbnail_filename' => null], 'id = ?', [$image['id']]);
- echo "<p style='color: orange;'>✓ Removed thumbnail filename for image ID {$image['id']}</p>\n";
- $removed++;
- } else {
- echo "<p style='color: green;'>✓ Thumbnail exists for image ID {$image['id']}</p>\n";
- $fixed++;
- }
- }
- echo "<h2>Summary</h2>\n";
- echo "<p>Images with valid thumbnails: {$fixed}</p>\n";
- echo "<p>Broken thumbnail entries removed: {$removed}</p>\n";
- // Test thumbnail generation on a sample image
- $testImages = $db->fetchAll("SELECT id, filename FROM images WHERE thumbnail_filename IS NULL LIMIT 3");
- if (!empty($testImages)) {
- echo "<h2>Testing Thumbnail Generation</h2>\n";
-
- foreach ($testImages as $image) {
- $sourcePath = '../uploads/images/' . $image['filename'];
- $thumbFilename = 'thumb_' . $image['filename'];
- $thumbPath = $thumbsDir . '/' . $thumbFilename;
-
- if (file_exists($sourcePath)) {
- echo "<p>Testing thumbnail generation for: {$image['filename']}</p>\n";
-
- // Include the thumbnail generation function
- require_once 'upload_image.php';
-
- if (generateThumbnail($sourcePath, $thumbPath)) {
- // Update database with new thumbnail filename
- $db->update('images', ['thumbnail_filename' => $thumbFilename], 'id = ?', [$image['id']]);
- echo "<p style='color: green;'>✓ Successfully generated thumbnail for image ID {$image['id']}</p>\n";
- } else {
- echo "<p style='color: red;'>✗ Failed to generate thumbnail for image ID {$image['id']}</p>\n";
- }
- } else {
- echo "<p style='color: red;'>✗ Source file not found: {$image['filename']}</p>\n";
- }
- }
- }
- echo "<p><a href='edit.php'>← Back to Editor</a></p>\n";
- ?>
|