fix_thumbnails.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Fix broken thumbnails in database
  4. * This script removes thumbnail_filename for images that don't have actual thumbnail files
  5. */
  6. require_once '../includes/config.php';
  7. require_once '../includes/database.php';
  8. // Create uploads directory if it doesn't exist
  9. $thumbsDir = '../uploads/images/thumbs';
  10. if (!file_exists($thumbsDir)) {
  11. mkdir($thumbsDir, 0755, true);
  12. }
  13. $db = Database::getInstance();
  14. echo "<h1>Fixing Broken Thumbnails</h1>\n";
  15. // Get all images with thumbnail filenames
  16. $images = $db->fetchAll("SELECT id, filename, thumbnail_filename FROM images WHERE thumbnail_filename IS NOT NULL AND thumbnail_filename != ''");
  17. echo "<p>Found " . count($images) . " images with thumbnail filenames</p>\n";
  18. $fixed = 0;
  19. $removed = 0;
  20. foreach ($images as $image) {
  21. $thumbPath = $thumbsDir . '/' . $image['thumbnail_filename'];
  22. if (!file_exists($thumbPath)) {
  23. echo "<p>Thumbnail file missing for image ID {$image['id']}: {$image['thumbnail_filename']}</p>\n";
  24. // Remove thumbnail filename from database (fallback to original)
  25. $db->update('images', ['thumbnail_filename' => null], 'id = ?', [$image['id']]);
  26. echo "<p style='color: orange;'>✓ Removed thumbnail filename for image ID {$image['id']}</p>\n";
  27. $removed++;
  28. } else {
  29. echo "<p style='color: green;'>✓ Thumbnail exists for image ID {$image['id']}</p>\n";
  30. $fixed++;
  31. }
  32. }
  33. echo "<h2>Summary</h2>\n";
  34. echo "<p>Images with valid thumbnails: {$fixed}</p>\n";
  35. echo "<p>Broken thumbnail entries removed: {$removed}</p>\n";
  36. // Test thumbnail generation on a sample image
  37. $testImages = $db->fetchAll("SELECT id, filename FROM images WHERE thumbnail_filename IS NULL LIMIT 3");
  38. if (!empty($testImages)) {
  39. echo "<h2>Testing Thumbnail Generation</h2>\n";
  40. foreach ($testImages as $image) {
  41. $sourcePath = '../uploads/images/' . $image['filename'];
  42. $thumbFilename = 'thumb_' . $image['filename'];
  43. $thumbPath = $thumbsDir . '/' . $thumbFilename;
  44. if (file_exists($sourcePath)) {
  45. echo "<p>Testing thumbnail generation for: {$image['filename']}</p>\n";
  46. // Include the thumbnail generation function
  47. require_once 'upload_image.php';
  48. if (generateThumbnail($sourcePath, $thumbPath)) {
  49. // Update database with new thumbnail filename
  50. $db->update('images', ['thumbnail_filename' => $thumbFilename], 'id = ?', [$image['id']]);
  51. echo "<p style='color: green;'>✓ Successfully generated thumbnail for image ID {$image['id']}</p>\n";
  52. } else {
  53. echo "<p style='color: red;'>✗ Failed to generate thumbnail for image ID {$image['id']}</p>\n";
  54. }
  55. } else {
  56. echo "<p style='color: red;'>✗ Source file not found: {$image['filename']}</p>\n";
  57. }
  58. }
  59. }
  60. echo "<p><a href='edit.php'>← Back to Editor</a></p>\n";
  61. ?>