|
|
@@ -61,8 +61,22 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['images'])) {
|
|
|
|
|
|
// Move file to uploads directory
|
|
|
if (move_uploaded_file($file['tmp_name'], $filepath)) {
|
|
|
- // Generate thumbnail
|
|
|
- $thumbnailGenerated = generateThumbnail($filepath, $thumbPath);
|
|
|
+ // Generate thumbnail with fallback
|
|
|
+ $thumbFilename = null;
|
|
|
+ $thumbnailUrl = '/uploads/images/' . $filename; // Fallback to original
|
|
|
+
|
|
|
+ // Try to generate thumbnail
|
|
|
+ $thumbFilenameTemp = 'thumb_' . $filename;
|
|
|
+ $thumbPathTemp = $thumbsDir . '/' . $thumbFilenameTemp;
|
|
|
+
|
|
|
+ if (generateThumbnail($filepath, $thumbPathTemp)) {
|
|
|
+ // Thumbnail generation successful
|
|
|
+ $thumbFilename = $thumbFilenameTemp;
|
|
|
+ $thumbnailUrl = '/uploads/images/thumbs/' . $thumbFilenameTemp;
|
|
|
+ } else {
|
|
|
+ // Thumbnail generation failed, log error but continue
|
|
|
+ error_log('Thumbnail generation failed for ' . $file['name'] . ', using original image as fallback');
|
|
|
+ }
|
|
|
|
|
|
// Save to database
|
|
|
$imageId = saveImageToDatabase($filename, $file['name'], $file['size'], $thumbFilename);
|
|
|
@@ -74,13 +88,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['images'])) {
|
|
|
'original_name' => $file['name'],
|
|
|
'size' => $file['size'],
|
|
|
'url' => '/uploads/images/' . $filename,
|
|
|
- 'thumbnail_url' => '/uploads/images/thumbs/' . $thumbFilename
|
|
|
+ 'thumbnail_url' => $thumbnailUrl
|
|
|
];
|
|
|
} else {
|
|
|
$errors[] = 'Failed to save ' . $file['name'] . ' to database';
|
|
|
unlink($filepath); // Remove uploaded file
|
|
|
- if ($thumbnailGenerated) {
|
|
|
- unlink($thumbPath); // Remove thumbnail if generated
|
|
|
+ if ($thumbFilename && file_exists($thumbPathTemp)) {
|
|
|
+ unlink($thumbPathTemp); // Remove thumbnail if generated
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
@@ -160,10 +174,17 @@ function generateThumbnail($sourcePath, $destPath, $maxWidth = 300, $maxHeight =
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ // Check if source file exists and is readable
|
|
|
+ if (!file_exists($sourcePath) || !is_readable($sourcePath)) {
|
|
|
+ error_log('Source file not found or not readable: ' . $sourcePath);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
// Get image info
|
|
|
$imageInfo = getimagesize($sourcePath);
|
|
|
if (!$imageInfo) {
|
|
|
+ error_log('Unable to get image info for: ' . $sourcePath);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -171,12 +192,17 @@ function generateThumbnail($sourcePath, $destPath, $maxWidth = 300, $maxHeight =
|
|
|
$height = $imageInfo[1];
|
|
|
$type = $imageInfo[2];
|
|
|
|
|
|
+ error_log("Processing image: {$sourcePath} - Size: {$width}x{$height}, Type: {$type}");
|
|
|
+
|
|
|
// Calculate new dimensions
|
|
|
$ratio = min($maxWidth / $width, $maxHeight / $height);
|
|
|
$newWidth = (int)($width * $ratio);
|
|
|
$newHeight = (int)($height * $ratio);
|
|
|
|
|
|
+ error_log("New thumbnail size: {$newWidth}x{$newHeight}");
|
|
|
+
|
|
|
// Create image resource based on type
|
|
|
+ $source = null;
|
|
|
switch ($type) {
|
|
|
case IMAGETYPE_JPEG:
|
|
|
$source = imagecreatefromjpeg($sourcePath);
|
|
|
@@ -188,15 +214,22 @@ function generateThumbnail($sourcePath, $destPath, $maxWidth = 300, $maxHeight =
|
|
|
$source = imagecreatefromgif($sourcePath);
|
|
|
break;
|
|
|
default:
|
|
|
+ error_log("Unsupported image type: {$type}");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (!$source) {
|
|
|
+ error_log('Failed to create image resource from: ' . $sourcePath);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// Create new image
|
|
|
$thumb = imagecreatetruecolor($newWidth, $newHeight);
|
|
|
+ if (!$thumb) {
|
|
|
+ error_log('Failed to create true color image for thumbnail');
|
|
|
+ imagedestroy($source);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
// Preserve transparency for PNG and GIF
|
|
|
if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_GIF) {
|
|
|
@@ -207,7 +240,24 @@ function generateThumbnail($sourcePath, $destPath, $maxWidth = 300, $maxHeight =
|
|
|
}
|
|
|
|
|
|
// Resize image
|
|
|
- imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
|
+ $resizeResult = imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
|
+ if (!$resizeResult) {
|
|
|
+ error_log('Failed to resize image');
|
|
|
+ imagedestroy($source);
|
|
|
+ imagedestroy($thumb);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Ensure destination directory exists
|
|
|
+ $destDir = dirname($destPath);
|
|
|
+ if (!is_dir($destDir)) {
|
|
|
+ if (!mkdir($destDir, 0755, true)) {
|
|
|
+ error_log('Failed to create thumbnail directory: ' . $destDir);
|
|
|
+ imagedestroy($source);
|
|
|
+ imagedestroy($thumb);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
// Save thumbnail
|
|
|
$result = false;
|
|
|
@@ -227,7 +277,13 @@ function generateThumbnail($sourcePath, $destPath, $maxWidth = 300, $maxHeight =
|
|
|
imagedestroy($source);
|
|
|
imagedestroy($thumb);
|
|
|
|
|
|
- return $result;
|
|
|
+ if ($result && file_exists($destPath)) {
|
|
|
+ error_log('Thumbnail successfully created: ' . $destPath);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ error_log('Failed to save thumbnail: ' . $destPath);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
error_log('Error generating thumbnail: ' . $e->getMessage());
|