-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.php
More file actions
214 lines (176 loc) · 7.49 KB
/
process.php
File metadata and controls
214 lines (176 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* Bildverarbeitung
* Dateipfad: /image-compressor/process.php
*
* Verarbeitet Bilder mit ausgewählten Einstellungen
*/
// Fehlerbehandlung - keine HTML-Ausgabe
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
// Output Buffering starten um unerwartete Ausgaben zu verhindern
ob_start();
// Erhöhe Limits für Bildverarbeitung BEVOR Session startet
@set_time_limit(300); // 5 Minuten
@ini_set('memory_limit', '256M');
@ini_set('max_execution_time', '300');
// Ignoriere Benutzerabbruch
ignore_user_abort(true);
session_start();
require_once 'config/config.php';
require_once 'classes/ImageProcessor.php';
require_once 'classes/FileManager.php';
// Setze JSON-Header
header('Content-Type: application/json; charset=utf-8');
try {
// Prüfe Request
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
throw new Exception('Methode nicht erlaubt', 405);
}
// Hole Parameter
$format = $_POST['format'] ?? 'jpeg';
$quality = intval($_POST['quality'] ?? DEFAULT_QUALITY);
// Warnung bei sehr hoher Qualität
if ($quality > 95 && ($format === 'jpeg' || $format === 'webp')) {
error_log("Warnung: Sehr hohe Qualität ($quality) kann zu größeren Dateien führen");
}
// Validiere Format
if (!isset(OUTPUT_FORMATS[$format])) {
throw new Exception('Ungültiges Ausgabeformat', 400);
}
// Prüfe Session-Dateien
if (!isset($_SESSION['uploaded_files']) || empty($_SESSION['uploaded_files'])) {
throw new Exception('Keine Dateien zum Verarbeiten', 400);
}
// Initialisiere Klassen
$fileManager = new FileManager();
// Speichere verarbeitete Dateien
$_SESSION['processed_files'] = [];
$results = [];
$errors = [];
// Verarbeite jede Datei
foreach ($_SESSION['uploaded_files'] as $fileId => $fileData) {
try {
// Prüfe ob Datei existiert
if (!file_exists($fileData['path'])) {
throw new Exception('Originaldatei nicht gefunden');
}
// Erstelle neuen Processor für jede Datei (um Speicher freizugeben)
$processor = new ImageProcessor();
// Lade Bild
$processor->loadImage($fileData['path']);
$processor->setQuality($quality);
// Generiere Ausgabedateiname
$outputExtension = OUTPUT_FORMATS[$format]['extension'];
$outputFilename = pathinfo($fileData['original_name'], PATHINFO_FILENAME) . '.' . $outputExtension;
$outputFilename = $fileManager->createSafeFilename($outputFilename, $outputExtension);
$outputPath = PROCESSED_PATH . $outputFilename;
// Verarbeite Bild
if (!$processor->process($outputPath, $format)) {
throw new Exception('Fehler bei der Bildverarbeitung');
}
// Erstelle Thumbnail
$thumbnailFilename = 'thumb_' . $outputFilename;
$thumbnailPath = PROCESSED_PATH . $thumbnailFilename;
$processor->createThumbnail($thumbnailPath);
// Zerstöre Processor explizit um Speicher freizugeben
$processor->__destruct();
unset($processor);
// Sammle Ergebnisse
$originalSize = filesize($fileData['path']);
$processedSize = filesize($outputPath);
$savings = $originalSize - $processedSize;
$savingsPercent = ($originalSize > 0) ? ($savings / $originalSize) * 100 : 0;
$processedData = [
'id' => $fileId,
'original_name' => $fileData['original_name'],
'processed_name' => $outputFilename,
'processed_path' => $outputPath,
'thumbnail_path' => $thumbnailPath,
'format' => $format,
'quality' => $quality,
'original_size' => $originalSize,
'processed_size' => $processedSize,
'savings' => $savings,
'savings_percent' => $savingsPercent
];
$_SESSION['processed_files'][$fileId] = $processedData;
$results[] = [
'id' => $fileId,
'success' => true,
'original_name' => $fileData['original_name'],
'processed_name' => $outputFilename,
'thumbnail_url' => 'processed/' . $thumbnailFilename,
'original_size' => formatFileSize($originalSize),
'processed_size' => formatFileSize($processedSize),
'savings' => formatFileSize($savings),
'savings_percent' => round($savingsPercent, 1)
];
// Explizite Garbage Collection nach jeder Datei
if (function_exists('gc_collect_cycles')) {
gc_collect_cycles();
}
} catch (Exception $e) {
$errors[] = [
'id' => $fileId,
'name' => $fileData['original_name'],
'error' => $e->getMessage()
];
// Log detaillierte Fehler
error_log('Bildverarbeitung Fehler: ' . $e->getMessage() . ' für Datei: ' . $fileData['original_name']);
}
}
// Berechne Gesamtstatistiken
$totalOriginalSize = 0;
$totalProcessedSize = 0;
foreach ($_SESSION['processed_files'] as $file) {
$totalOriginalSize += $file['original_size'];
$totalProcessedSize += $file['processed_size'];
}
$totalSavings = $totalOriginalSize - $totalProcessedSize;
$totalSavingsPercent = $totalOriginalSize > 0 ? ($totalSavings / $totalOriginalSize) * 100 : 0;
// Bereinige alte Dateien
$fileManager->cleanupOldFiles();
// Lösche Original-Uploads nach erfolgreicher Verarbeitung
foreach ($_SESSION['uploaded_files'] as $fileId => $fileData) {
if (isset($_SESSION['processed_files'][$fileId]) && file_exists($fileData['path'])) {
@unlink($fileData['path']);
}
}
// Leere Upload-Session für nächsten Durchgang
unset($_SESSION['uploaded_files']);
unset($_SESSION['upload_session_id']);
// Sende erfolgreiche Antwort
$response = [
'success' => count($results) > 0,
'processed_count' => count($results),
'error_count' => count($errors),
'results' => $results,
'errors' => $errors,
'statistics' => [
'total_original_size' => formatFileSize($totalOriginalSize),
'total_processed_size' => formatFileSize($totalProcessedSize),
'total_savings' => formatFileSize($totalSavings),
'total_savings_percent' => round($totalSavingsPercent, 1)
]
];
// Lösche Output Buffer und sende Response
ob_end_clean();
echo json_encode($response);
} catch (Exception $e) {
// Bei Fehlern: Lösche Output Buffer
ob_end_clean();
// Setze korrekten HTTP-Status
$statusCode = $e->getCode() ?: 500;
http_response_code($statusCode);
// Sende JSON-Fehlerantwort
echo json_encode([
'success' => false,
'error' => $e->getMessage(),
'processed_count' => 0,
'error_count' => 1,
'results' => [],
'errors' => []
]);
}