upload_image.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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
  55. $thumbnailGenerated = generateThumbnail($filepath, $thumbPath);
  56. // Save to database
  57. $imageId = saveImageToDatabase($filename, $file['name'], $file['size'], $thumbFilename);
  58. if ($imageId) {
  59. $uploadedFiles[] = [
  60. 'id' => $imageId,
  61. 'filename' => $filename,
  62. 'original_name' => $file['name'],
  63. 'size' => $file['size'],
  64. 'url' => '/uploads/images/' . $filename,
  65. 'thumbnail_url' => '/uploads/images/thumbs/' . $thumbFilename
  66. ];
  67. } else {
  68. $errors[] = 'Failed to save ' . $file['name'] . ' to database';
  69. unlink($filepath); // Remove uploaded file
  70. if ($thumbnailGenerated) {
  71. unlink($thumbPath); // Remove thumbnail if generated
  72. }
  73. }
  74. } else {
  75. $errors[] = 'Failed to upload ' . $file['name'];
  76. }
  77. } else {
  78. $errors[] = $validation['error'];
  79. }
  80. } else {
  81. $errors[] = 'Error uploading file: ' . getUploadErrorMessage($files['error'][$i]);
  82. }
  83. }
  84. echo json_encode([
  85. 'success' => empty($errors),
  86. 'files' => $uploadedFiles,
  87. 'errors' => $errors
  88. ]);
  89. exit;
  90. }
  91. // Handle GET request to fetch existing images
  92. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  93. $images = getExistingImages();
  94. echo json_encode([
  95. 'success' => true,
  96. 'images' => $images
  97. ]);
  98. exit;
  99. }
  100. // Handle DELETE request to remove image
  101. if ($_SERVER['REQUEST_METHOD'] === 'DELETE' && isset($_GET['id'])) {
  102. $imageId = (int)$_GET['id'];
  103. $success = deleteImage($imageId);
  104. echo json_encode([
  105. 'success' => $success
  106. ]);
  107. exit;
  108. }
  109. echo json_encode(['success' => false, 'error' => 'Invalid request']);
  110. /**
  111. * Validate uploaded image file
  112. */
  113. function validateImageFile($file) {
  114. // Check file size (20MB max)
  115. $maxSize = 20 * 1024 * 1024; // 20MB
  116. if ($file['size'] > $maxSize) {
  117. return ['valid' => false, 'error' => 'File too large. Maximum size is 20MB'];
  118. }
  119. // Check file type
  120. $allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
  121. if (!in_array($file['type'], $allowedTypes)) {
  122. return ['valid' => false, 'error' => 'Invalid file type. Only JPG, PNG, GIF, and WebP are allowed'];
  123. }
  124. // Check file extension
  125. $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
  126. $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
  127. if (!in_array($extension, $allowedExtensions)) {
  128. return ['valid' => false, 'error' => 'Invalid file extension'];
  129. }
  130. return ['valid' => true];
  131. }
  132. /**
  133. * Generate thumbnail for uploaded image
  134. */
  135. function generateThumbnail($sourcePath, $destPath, $maxWidth = 300, $maxHeight = 200) {
  136. // Check if GD extension is available
  137. if (!extension_loaded('gd')) {
  138. error_log('GD extension not available for thumbnail generation');
  139. return false;
  140. }
  141. try {
  142. // Get image info
  143. $imageInfo = getimagesize($sourcePath);
  144. if (!$imageInfo) {
  145. return false;
  146. }
  147. $width = $imageInfo[0];
  148. $height = $imageInfo[1];
  149. $type = $imageInfo[2];
  150. // Calculate new dimensions
  151. $ratio = min($maxWidth / $width, $maxHeight / $height);
  152. $newWidth = (int)($width * $ratio);
  153. $newHeight = (int)($height * $ratio);
  154. // Create image resource based on type
  155. switch ($type) {
  156. case IMAGETYPE_JPEG:
  157. $source = imagecreatefromjpeg($sourcePath);
  158. break;
  159. case IMAGETYPE_PNG:
  160. $source = imagecreatefrompng($sourcePath);
  161. break;
  162. case IMAGETYPE_GIF:
  163. $source = imagecreatefromgif($sourcePath);
  164. break;
  165. default:
  166. return false;
  167. }
  168. if (!$source) {
  169. return false;
  170. }
  171. // Create new image
  172. $thumb = imagecreatetruecolor($newWidth, $newHeight);
  173. // Preserve transparency for PNG and GIF
  174. if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_GIF) {
  175. imagealphablending($thumb, false);
  176. imagesavealpha($thumb, true);
  177. $transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
  178. imagefilledrectangle($thumb, 0, 0, $newWidth, $newHeight, $transparent);
  179. }
  180. // Resize image
  181. imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
  182. // Save thumbnail
  183. $result = false;
  184. switch ($type) {
  185. case IMAGETYPE_JPEG:
  186. $result = imagejpeg($thumb, $destPath, 85);
  187. break;
  188. case IMAGETYPE_PNG:
  189. $result = imagepng($thumb, $destPath, 8);
  190. break;
  191. case IMAGETYPE_GIF:
  192. $result = imagegif($thumb, $destPath);
  193. break;
  194. }
  195. // Clean up
  196. imagedestroy($source);
  197. imagedestroy($thumb);
  198. return $result;
  199. } catch (Exception $e) {
  200. error_log('Error generating thumbnail: ' . $e->getMessage());
  201. return false;
  202. }
  203. }
  204. /**
  205. * Save image information to database
  206. */
  207. function saveImageToDatabase($filename, $originalName, $size, $thumbnailFilename = null) {
  208. $db = Database::getInstance();
  209. try {
  210. $db->insert('images', [
  211. 'filename' => $filename,
  212. 'thumbnail_filename' => $thumbnailFilename,
  213. 'original_name' => $originalName,
  214. 'file_size' => $size,
  215. 'uploaded_at' => date('Y-m-d H:i:s'),
  216. 'uploaded_by' => $_SESSION['user_id'] ?? 1
  217. ]);
  218. return $db->lastInsertId();
  219. } catch (Exception $e) {
  220. error_log('Failed to save image to database: ' . $e->getMessage());
  221. return false;
  222. }
  223. }
  224. /**
  225. * Get existing images from database
  226. */
  227. function getExistingImages() {
  228. $db = Database::getInstance();
  229. try {
  230. $images = $db->fetchAll("SELECT * FROM images ORDER BY uploaded_at DESC");
  231. $result = [];
  232. foreach ($images as $image) {
  233. // Use thumbnail if available, otherwise use original image
  234. $thumbnailUrl = $image['thumbnail_filename']
  235. ? '/uploads/images/thumbs/' . $image['thumbnail_filename']
  236. : '/uploads/images/' . $image['filename'];
  237. $result[] = [
  238. 'id' => $image['id'],
  239. 'filename' => $image['filename'],
  240. 'thumbnail_filename' => $image['thumbnail_filename'],
  241. 'original_name' => $image['original_name'],
  242. 'size' => $image['file_size'],
  243. 'url' => '/uploads/images/' . $image['filename'],
  244. 'thumbnail_url' => $thumbnailUrl,
  245. 'uploaded_at' => $image['uploaded_at']
  246. ];
  247. }
  248. return $result;
  249. } catch (Exception $e) {
  250. error_log('Failed to fetch images: ' . $e->getMessage());
  251. return [];
  252. }
  253. }
  254. /**
  255. * Delete image from database and filesystem
  256. */
  257. function deleteImage($imageId) {
  258. $db = Database::getInstance();
  259. try {
  260. // Get image info
  261. $image = $db->fetch("SELECT * FROM images WHERE id = ?", [$imageId]);
  262. if (!$image) {
  263. return false;
  264. }
  265. // Delete from database
  266. $db->delete('images', 'id = ?', [$imageId]);
  267. // Delete file from filesystem
  268. $filepath = '../uploads/images/' . $image['filename'];
  269. if (file_exists($filepath)) {
  270. unlink($filepath);
  271. }
  272. return true;
  273. } catch (Exception $e) {
  274. error_log('Failed to delete image: ' . $e->getMessage());
  275. return false;
  276. }
  277. }
  278. /**
  279. * Get upload error message
  280. */
  281. function getUploadErrorMessage($errorCode) {
  282. switch ($errorCode) {
  283. case UPLOAD_ERR_INI_SIZE:
  284. return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  285. case UPLOAD_ERR_FORM_SIZE:
  286. return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  287. case UPLOAD_ERR_PARTIAL:
  288. return 'The uploaded file was only partially uploaded';
  289. case UPLOAD_ERR_NO_FILE:
  290. return 'No file was uploaded';
  291. case UPLOAD_ERR_NO_TMP_DIR:
  292. return 'Missing a temporary folder';
  293. case UPLOAD_ERR_CANT_WRITE:
  294. return 'Failed to write file to disk';
  295. case UPLOAD_ERR_EXTENSION:
  296. return 'A PHP extension stopped the file upload';
  297. default:
  298. return 'Unknown upload error';
  299. }
  300. }
  301. ?>