upload_image.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /**
  3. * Image Upload Handler
  4. * Handles image uploads for the WYSIWYG editor
  5. */
  6. // Start session
  7. if (session_status() === PHP_SESSION_NONE) {
  8. session_start();
  9. }
  10. require_once '../includes/config.php';
  11. require_once '../includes/database.php';
  12. require_once '../includes/auth.php';
  13. // Require authentication
  14. $auth = new Auth();
  15. $auth->requireAuth();
  16. // Set headers for JSON response
  17. header('Content-Type: application/json');
  18. // Create uploads directory if it doesn't exist
  19. $uploadsDir = '../uploads/images';
  20. $thumbsDir = '../uploads/images/thumbs';
  21. if (!file_exists($uploadsDir)) {
  22. mkdir($uploadsDir, 0755, true);
  23. }
  24. if (!file_exists($thumbsDir)) {
  25. mkdir($thumbsDir, 0755, true);
  26. }
  27. // Handle file upload
  28. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['images'])) {
  29. $uploadedFiles = [];
  30. $errors = [];
  31. // Handle multiple files
  32. $files = $_FILES['images'];
  33. $fileCount = count($files['name']);
  34. for ($i = 0; $i < $fileCount; $i++) {
  35. if ($files['error'][$i] === UPLOAD_ERR_OK) {
  36. $file = [
  37. 'name' => $files['name'][$i],
  38. 'type' => $files['type'][$i],
  39. 'tmp_name' => $files['tmp_name'][$i],
  40. 'error' => $files['error'][$i],
  41. 'size' => $files['size'][$i]
  42. ];
  43. // Validate file
  44. $validation = validateImageFile($file);
  45. if ($validation['valid']) {
  46. // Generate unique filename
  47. $extension = pathinfo($file['name'], PATHINFO_EXTENSION);
  48. $filename = uniqid() . '.' . $extension;
  49. $filepath = $uploadsDir . '/' . $filename;
  50. $thumbFilename = 'thumb_' . $filename;
  51. $thumbPath = $thumbsDir . '/' . $thumbFilename;
  52. // Move file to uploads directory
  53. if (move_uploaded_file($file['tmp_name'], $filepath)) {
  54. // Generate thumbnail with fallback
  55. $thumbFilename = null;
  56. $thumbnailUrl = '/uploads/images/' . $filename; // Fallback to original
  57. // Try to generate thumbnail
  58. $thumbFilenameTemp = 'thumb_' . $filename;
  59. $thumbPathTemp = $thumbsDir . '/' . $thumbFilenameTemp;
  60. if (generateThumbnail($filepath, $thumbPathTemp)) {
  61. // Thumbnail generation successful
  62. $thumbFilename = $thumbFilenameTemp;
  63. $thumbnailUrl = '/uploads/images/thumbs/' . $thumbFilenameTemp;
  64. } else {
  65. // Thumbnail generation failed, log error but continue
  66. error_log('Thumbnail generation failed for ' . $file['name'] . ', using original image as fallback');
  67. }
  68. // Save to database
  69. $imageId = saveImageToDatabase($filename, $file['name'], $file['size'], $thumbFilename);
  70. if ($imageId) {
  71. $uploadedFiles[] = [
  72. 'id' => $imageId,
  73. 'filename' => $filename,
  74. 'original_name' => $file['name'],
  75. 'size' => $file['size'],
  76. 'url' => '/uploads/images/' . $filename,
  77. 'thumbnail_url' => $thumbnailUrl
  78. ];
  79. } else {
  80. $errors[] = 'Failed to save ' . $file['name'] . ' to database';
  81. unlink($filepath); // Remove uploaded file
  82. if ($thumbFilename && file_exists($thumbPathTemp)) {
  83. unlink($thumbPathTemp); // Remove thumbnail if generated
  84. }
  85. }
  86. } else {
  87. $errors[] = 'Failed to upload ' . $file['name'];
  88. }
  89. } else {
  90. $errors[] = $validation['error'];
  91. }
  92. } else {
  93. $errors[] = 'Error uploading file: ' . getUploadErrorMessage($files['error'][$i]);
  94. }
  95. }
  96. echo json_encode([
  97. 'success' => empty($errors),
  98. 'files' => $uploadedFiles,
  99. 'errors' => $errors
  100. ]);
  101. exit;
  102. }
  103. // Handle GET request to fetch existing images
  104. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  105. $images = getExistingImages();
  106. echo json_encode([
  107. 'success' => true,
  108. 'images' => $images
  109. ]);
  110. exit;
  111. }
  112. // Handle DELETE request to remove image
  113. if ($_SERVER['REQUEST_METHOD'] === 'DELETE' && isset($_GET['id'])) {
  114. $imageId = (int)$_GET['id'];
  115. $success = deleteImage($imageId);
  116. echo json_encode([
  117. 'success' => $success
  118. ]);
  119. exit;
  120. }
  121. echo json_encode(['success' => false, 'error' => 'Invalid request']);
  122. /**
  123. * Validate uploaded image file
  124. */
  125. function validateImageFile($file) {
  126. // Check file size (20MB max)
  127. $maxSize = 20 * 1024 * 1024; // 20MB
  128. if ($file['size'] > $maxSize) {
  129. return ['valid' => false, 'error' => 'File too large. Maximum size is 20MB'];
  130. }
  131. // Check file type
  132. $allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
  133. if (!in_array($file['type'], $allowedTypes)) {
  134. return ['valid' => false, 'error' => 'Invalid file type. Only JPG, PNG, GIF, and WebP are allowed'];
  135. }
  136. // Check file extension
  137. $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
  138. $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
  139. if (!in_array($extension, $allowedExtensions)) {
  140. return ['valid' => false, 'error' => 'Invalid file extension'];
  141. }
  142. return ['valid' => true];
  143. }
  144. /**
  145. * Generate thumbnail for uploaded image
  146. */
  147. function generateThumbnail($sourcePath, $destPath, $maxWidth = 300, $maxHeight = 200) {
  148. // Check if GD extension is available
  149. if (!extension_loaded('gd')) {
  150. error_log('GD extension not available for thumbnail generation');
  151. return false;
  152. }
  153. // Check if source file exists and is readable
  154. if (!file_exists($sourcePath) || !is_readable($sourcePath)) {
  155. error_log('Source file not found or not readable: ' . $sourcePath);
  156. return false;
  157. }
  158. try {
  159. // Get image info
  160. $imageInfo = getimagesize($sourcePath);
  161. if (!$imageInfo) {
  162. error_log('Unable to get image info for: ' . $sourcePath);
  163. return false;
  164. }
  165. $width = $imageInfo[0];
  166. $height = $imageInfo[1];
  167. $type = $imageInfo[2];
  168. error_log("Processing image: {$sourcePath} - Size: {$width}x{$height}, Type: {$type}");
  169. // Calculate new dimensions
  170. $ratio = min($maxWidth / $width, $maxHeight / $height);
  171. $newWidth = (int)($width * $ratio);
  172. $newHeight = (int)($height * $ratio);
  173. error_log("New thumbnail size: {$newWidth}x{$newHeight}");
  174. // Create image resource based on type
  175. $source = null;
  176. switch ($type) {
  177. case IMAGETYPE_JPEG:
  178. $source = imagecreatefromjpeg($sourcePath);
  179. break;
  180. case IMAGETYPE_PNG:
  181. $source = imagecreatefrompng($sourcePath);
  182. break;
  183. case IMAGETYPE_GIF:
  184. $source = imagecreatefromgif($sourcePath);
  185. break;
  186. default:
  187. error_log("Unsupported image type: {$type}");
  188. return false;
  189. }
  190. if (!$source) {
  191. error_log('Failed to create image resource from: ' . $sourcePath);
  192. return false;
  193. }
  194. // Create new image
  195. $thumb = imagecreatetruecolor($newWidth, $newHeight);
  196. if (!$thumb) {
  197. error_log('Failed to create true color image for thumbnail');
  198. imagedestroy($source);
  199. return false;
  200. }
  201. // Preserve transparency for PNG and GIF
  202. if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_GIF) {
  203. imagealphablending($thumb, false);
  204. imagesavealpha($thumb, true);
  205. $transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
  206. imagefilledrectangle($thumb, 0, 0, $newWidth, $newHeight, $transparent);
  207. }
  208. // Resize image
  209. $resizeResult = imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
  210. if (!$resizeResult) {
  211. error_log('Failed to resize image');
  212. imagedestroy($source);
  213. imagedestroy($thumb);
  214. return false;
  215. }
  216. // Ensure destination directory exists
  217. $destDir = dirname($destPath);
  218. if (!is_dir($destDir)) {
  219. if (!mkdir($destDir, 0755, true)) {
  220. error_log('Failed to create thumbnail directory: ' . $destDir);
  221. imagedestroy($source);
  222. imagedestroy($thumb);
  223. return false;
  224. }
  225. }
  226. // Save thumbnail
  227. $result = false;
  228. switch ($type) {
  229. case IMAGETYPE_JPEG:
  230. $result = imagejpeg($thumb, $destPath, 85);
  231. break;
  232. case IMAGETYPE_PNG:
  233. $result = imagepng($thumb, $destPath, 8);
  234. break;
  235. case IMAGETYPE_GIF:
  236. $result = imagegif($thumb, $destPath);
  237. break;
  238. }
  239. // Clean up
  240. imagedestroy($source);
  241. imagedestroy($thumb);
  242. if ($result && file_exists($destPath)) {
  243. error_log('Thumbnail successfully created: ' . $destPath);
  244. return true;
  245. } else {
  246. error_log('Failed to save thumbnail: ' . $destPath);
  247. return false;
  248. }
  249. } catch (Exception $e) {
  250. error_log('Error generating thumbnail: ' . $e->getMessage());
  251. return false;
  252. }
  253. }
  254. /**
  255. * Save image information to database
  256. */
  257. function saveImageToDatabase($filename, $originalName, $size, $thumbnailFilename = null) {
  258. $db = Database::getInstance();
  259. try {
  260. $db->insert('images', [
  261. 'filename' => $filename,
  262. 'thumbnail_filename' => $thumbnailFilename,
  263. 'original_name' => $originalName,
  264. 'file_size' => $size,
  265. 'uploaded_at' => date('Y-m-d H:i:s'),
  266. 'uploaded_by' => $_SESSION['user_id'] ?? 1
  267. ]);
  268. return $db->lastInsertId();
  269. } catch (Exception $e) {
  270. error_log('Failed to save image to database: ' . $e->getMessage());
  271. return false;
  272. }
  273. }
  274. /**
  275. * Get existing images from database
  276. */
  277. function getExistingImages() {
  278. $db = Database::getInstance();
  279. try {
  280. $images = $db->fetchAll("SELECT * FROM images ORDER BY uploaded_at DESC");
  281. $result = [];
  282. foreach ($images as $image) {
  283. // Use thumbnail if available, otherwise use original image
  284. $thumbnailUrl = $image['thumbnail_filename']
  285. ? '/uploads/images/thumbs/' . $image['thumbnail_filename']
  286. : '/uploads/images/' . $image['filename'];
  287. $result[] = [
  288. 'id' => $image['id'],
  289. 'filename' => $image['filename'],
  290. 'thumbnail_filename' => $image['thumbnail_filename'],
  291. 'original_name' => $image['original_name'],
  292. 'size' => $image['file_size'],
  293. 'url' => '/uploads/images/' . $image['filename'],
  294. 'thumbnail_url' => $thumbnailUrl,
  295. 'uploaded_at' => $image['uploaded_at']
  296. ];
  297. }
  298. return $result;
  299. } catch (Exception $e) {
  300. error_log('Failed to fetch images: ' . $e->getMessage());
  301. return [];
  302. }
  303. }
  304. /**
  305. * Delete image from database and filesystem
  306. */
  307. function deleteImage($imageId) {
  308. $db = Database::getInstance();
  309. try {
  310. // Get image info
  311. $image = $db->fetch("SELECT * FROM images WHERE id = ?", [$imageId]);
  312. if (!$image) {
  313. return false;
  314. }
  315. // Delete from database
  316. $db->delete('images', 'id = ?', [$imageId]);
  317. // Delete file from filesystem
  318. $filepath = '../uploads/images/' . $image['filename'];
  319. if (file_exists($filepath)) {
  320. unlink($filepath);
  321. }
  322. return true;
  323. } catch (Exception $e) {
  324. error_log('Failed to delete image: ' . $e->getMessage());
  325. return false;
  326. }
  327. }
  328. /**
  329. * Get upload error message
  330. */
  331. function getUploadErrorMessage($errorCode) {
  332. switch ($errorCode) {
  333. case UPLOAD_ERR_INI_SIZE:
  334. return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  335. case UPLOAD_ERR_FORM_SIZE:
  336. return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  337. case UPLOAD_ERR_PARTIAL:
  338. return 'The uploaded file was only partially uploaded';
  339. case UPLOAD_ERR_NO_FILE:
  340. return 'No file was uploaded';
  341. case UPLOAD_ERR_NO_TMP_DIR:
  342. return 'Missing a temporary folder';
  343. case UPLOAD_ERR_CANT_WRITE:
  344. return 'Failed to write file to disk';
  345. case UPLOAD_ERR_EXTENSION:
  346. return 'A PHP extension stopped the file upload';
  347. default:
  348. return 'Unknown upload error';
  349. }
  350. }
  351. ?>