Repository navigation
Expand file tree
/
Copy pathimage_frame_processor.php
More file actions
489 lines (420 loc) · 17.8 KB
/
Copy pathimage_frame_processor.php
File metadata and controls
489 lines (420 loc) · 17.8 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/**
* Procesador de Marcos Temáticos para Imágenes
* Añade diseños personalizados según el tipo de evento
*/
class ImageFrameProcessor {
private $frame_templates = [
'boda' => [
'border_color' => '#f8f9fa',
'border_width' => 30,
'corner_style' => 'ornate',
'watermark' => 'elegant_hearts',
'text_color' => '#8b7355',
'font_family' => 'serif'
],
'cumpleanos' => [
'border_color' => '#ff6b9d',
'border_width' => 25,
'corner_style' => 'fun',
'watermark' => 'balloons',
'text_color' => '#ffffff',
'font_family' => 'sans-serif'
],
'quinceanos' => [
'border_color' => '#e91e63',
'border_width' => 35,
'corner_style' => 'princess',
'watermark' => 'crown',
'text_color' => '#ffffff',
'font_family' => 'script'
],
'bautizo' => [
'border_color' => '#e3f2fd',
'border_width' => 20,
'corner_style' => 'gentle',
'watermark' => 'cross',
'text_color' => '#1976d2',
'font_family' => 'serif'
],
'graduacion' => [
'border_color' => '#1a237e',
'border_width' => 25,
'corner_style' => 'academic',
'watermark' => 'graduation_cap',
'text_color' => '#ffffff',
'font_family' => 'sans-serif'
],
'baby_shower' => [
'border_color' => '#fff3e0',
'border_width' => 30,
'corner_style' => 'soft',
'watermark' => 'baby_items',
'text_color' => '#ff8a65',
'font_family' => 'script'
],
'aniversario' => [
'border_color' => '#3e2723',
'border_width' => 25,
'corner_style' => 'classic',
'watermark' => 'rings',
'text_color' => '#d4af37',
'font_family' => 'serif'
],
'corporativo' => [
'border_color' => '#263238',
'border_width' => 20,
'corner_style' => 'modern',
'watermark' => 'none',
'text_color' => '#ffffff',
'font_family' => 'sans-serif'
]
];
public function processImage($image_path, $event_type, $event_name, $date, $contributor_name) {
try {
// Verificar que la imagen existe
if (!file_exists($image_path)) {
throw new Exception("Imagen no encontrada: $image_path");
}
// Obtener configuración del marco
$frame_config = $this->frame_templates[$event_type] ?? $this->frame_templates['corporativo'];
// Crear imagen desde archivo
$image_info = getimagesize($image_path);
$image = $this->createImageFromFile($image_path, $image_info[2]);
if (!$image) {
throw new Exception("No se pudo crear la imagen desde: $image_path");
}
// Obtener dimensiones originales
$original_width = imagesx($image);
$original_height = imagesy($image);
// Calcular nuevas dimensiones con marco
$border_width = $frame_config['border_width'];
$new_width = $original_width + ($border_width * 2);
$new_height = $original_height + ($border_width * 2) + 60; // +60 para texto inferior
// Crear lienzo con marco
$framed_image = imagecreatetruecolor($new_width, $new_height);
// Color de fondo del marco
$border_color = $this->hexToRgb($frame_config['border_color']);
$bg_color = imagecolorallocate($framed_image, $border_color[0], $border_color[1], $border_color[2]);
imagefill($framed_image, 0, 0, $bg_color);
// Añadir patrón decorativo según el estilo
$this->addDecorativePattern($framed_image, $frame_config, $new_width, $new_height);
// Copiar imagen original al centro
imagecopy($framed_image, $image, $border_width, $border_width, 0, 0, $original_width, $original_height);
// Añadir texto del evento
$this->addEventText($framed_image, $frame_config, $event_name, $date, $contributor_name, $new_width, $new_height);
// Añadir marca de agua temática
$this->addThematicWatermark($framed_image, $frame_config, $new_width, $new_height);
// Limpiar imagen original
imagedestroy($image);
return $framed_image;
} catch (Exception $e) {
error_log("Error procesando imagen: " . $e->getMessage());
return false;
}
}
private function createImageFromFile($path, $type) {
switch ($type) {
case IMAGETYPE_JPEG:
return imagecreatefromjpeg($path);
case IMAGETYPE_PNG:
return imagecreatefrompng($path);
case IMAGETYPE_GIF:
return imagecreatefromgif($path);
case IMAGETYPE_WEBP:
return imagecreatefromwebp($path);
default:
return false;
}
}
private function hexToRgb($hex) {
$hex = ltrim($hex, '#');
return [
hexdec(substr($hex, 0, 2)),
hexdec(substr($hex, 2, 2)),
hexdec(substr($hex, 4, 2))
];
}
private function addDecorativePattern($image, $config, $width, $height) {
$border_width = $config['border_width'];
switch ($config['corner_style']) {
case 'ornate': // Para bodas
$this->drawOrnateCorners($image, $width, $height, $border_width);
break;
case 'fun': // Para cumpleaños
$this->drawFunPattern($image, $width, $height, $border_width);
break;
case 'princess': // Para quinceaños
$this->drawPrincessPattern($image, $width, $height, $border_width);
break;
case 'gentle': // Para bautizos
$this->drawGentlePattern($image, $width, $height, $border_width);
break;
case 'academic': // Para graduaciones
$this->drawAcademicPattern($image, $width, $height, $border_width);
break;
}
}
private function drawOrnateCorners($image, $width, $height, $border) {
// Color dorado para ornamentos
$gold = imagecolorallocate($image, 212, 175, 55);
// Esquinas ornamentadas para bodas
$corner_size = $border / 2;
// Esquina superior izquierda
for ($i = 0; $i < $corner_size; $i++) {
imagearc($image, $border, $border, $i * 2, $i * 2, 0, 90, $gold);
}
// Esquina superior derecha
for ($i = 0; $i < $corner_size; $i++) {
imagearc($image, $width - $border, $border, $i * 2, $i * 2, 90, 180, $gold);
}
// Esquina inferior izquierda
for ($i = 0; $i < $corner_size; $i++) {
imagearc($image, $border, $height - $border - 60, $i * 2, $i * 2, 270, 360, $gold);
}
// Esquina inferior derecha
for ($i = 0; $i < $corner_size; $i++) {
imagearc($image, $width - $border, $height - $border - 60, $i * 2, $i * 2, 180, 270, $gold);
}
}
private function drawFunPattern($image, $width, $height, $border) {
// Colores divertidos
$colors = [
imagecolorallocate($image, 255, 193, 7), // Amarillo
imagecolorallocate($image, 76, 175, 80), // Verde
imagecolorallocate($image, 33, 150, 243), // Azul
imagecolorallocate($image, 233, 30, 99) // Rosa
];
// Puntos coloridos en el marco
for ($i = 0; $i < 50; $i++) {
$x = rand(5, $width - 5);
$y = rand(5, $border - 5); // Solo en el borde superior
$color = $colors[array_rand($colors)];
imagefilledellipse($image, $x, $y, 8, 8, $color);
}
// También en el borde inferior
for ($i = 0; $i < 50; $i++) {
$x = rand(5, $width - 5);
$y = rand($height - $border, $height - 5);
$color = $colors[array_rand($colors)];
imagefilledellipse($image, $x, $y, 8, 8, $color);
}
}
private function drawPrincessPattern($image, $width, $height, $border) {
// Color rosa/dorado para quinceaños
$pink = imagecolorallocate($image, 233, 30, 99);
$gold = imagecolorallocate($image, 255, 215, 0);
// Estrellas en las esquinas
for ($i = 0; $i < 20; $i++) {
$x = rand(5, $border - 5);
$y = rand(5, $border - 5);
$this->drawStar($image, $x, $y, 5, $pink);
}
// Border decorativo
for ($x = 0; $x < $width; $x += 20) {
imageline($image, $x, 5, $x + 10, 15, $gold);
imageline($image, $x, $height - 65, $x + 10, $height - 55, $gold);
}
}
private function drawGentlePattern($image, $width, $height, $border) {
// Patrón suave para bautizos
$light_blue = imagecolorallocate($image, 144, 202, 249);
// Líneas suaves
for ($i = 0; $i < $border; $i += 5) {
imageline($image, $i, 0, $i, $height, $light_blue);
imageline($image, $width - $i, 0, $width - $i, $height, $light_blue);
}
}
private function drawAcademicPattern($image, $width, $height, $border) {
// Patrón académico para graduaciones
$gold = imagecolorallocate($image, 255, 215, 0);
// Líneas diagonales (como togas)
for ($i = 0; $i < $width; $i += 30) {
imageline($image, $i, 0, $i + 15, $border, $gold);
imageline($image, $i, $height - 60, $i + 15, $height, $gold);
}
}
private function drawStar($image, $cx, $cy, $size, $color) {
// Dibujar una estrella simple
$points = [];
for ($i = 0; $i < 10; $i++) {
$angle = $i * M_PI / 5;
$radius = ($i % 2) ? $size / 2 : $size;
$points[] = $cx + cos($angle) * $radius;
$points[] = $cy + sin($angle) * $radius;
}
imagefilledpolygon($image, $points, 10, $color);
}
private function addEventText($image, $config, $event_name, $date, $contributor, $width, $height) {
// Color del texto
$text_color_rgb = $this->hexToRgb($config['text_color']);
$text_color = imagecolorallocate($image, $text_color_rgb[0], $text_color_rgb[1], $text_color_rgb[2]);
// Fuente (usar fuente del sistema)
$font_size = 12;
// Texto del evento (parte superior)
$text_y = $height - 45;
imagettftext($image, $font_size, 0, 10, $text_y, $text_color, $this->getSystemFont(),
strtoupper($event_name));
// Fecha y contribuyente (parte inferior)
$bottom_text = date('d/m/Y', strtotime($date)) . " • " . $contributor;
imagettftext($image, 10, 0, 10, $height - 15, $text_color, $this->getSystemFont(), $bottom_text);
}
private function addThematicWatermark($image, $config, $width, $height) {
// Añadir marca de agua temática según el tipo de evento
$watermark_color = imagecolorallocatealpha($image, 255, 255, 255, 100); // Semi-transparente
switch ($config['watermark']) {
case 'elegant_hearts':
$this->drawHearts($image, $width, $height, $watermark_color);
break;
case 'balloons':
$this->drawBalloons($image, $width, $height, $watermark_color);
break;
case 'crown':
$this->drawCrown($image, $width - 50, 30, $watermark_color);
break;
case 'cross':
$this->drawCross($image, $width - 40, 30, $watermark_color);
break;
case 'graduation_cap':
$this->drawGraduationCap($image, $width - 60, 30, $watermark_color);
break;
}
}
private function drawHearts($image, $width, $height, $color) {
// Corazones pequeños dispersos
for ($i = 0; $i < 5; $i++) {
$x = rand(50, $width - 100);
$y = rand(50, $height - 150);
$this->drawHeart($image, $x, $y, 15, $color);
}
}
private function drawHeart($image, $cx, $cy, $size, $color) {
// Dibujar un corazón simple
imagefilledellipse($image, $cx - $size/3, $cy - $size/3, $size, $size, $color);
imagefilledellipse($image, $cx + $size/3, $cy - $size/3, $size, $size, $color);
$points = [
$cx - $size/2, $cy,
$cx, $cy + $size,
$cx + $size/2, $cy
];
imagefilledpolygon($image, $points, 3, $color);
}
private function drawBalloons($image, $width, $height, $color) {
// Globos para cumpleaños
for ($i = 0; $i < 3; $i++) {
$x = rand(100, $width - 150);
$y = rand(80, $height - 200);
imagefilledellipse($image, $x, $y, 20, 30, $color);
imageline($image, $x, $y + 15, $x, $y + 40, $color);
}
}
private function drawCrown($image, $x, $y, $color) {
// Corona para quinceaños
$points = [
$x, $y + 20,
$x + 10, $y,
$x + 20, $y + 15,
$x + 30, $y,
$x + 40, $y + 20,
$x + 35, $y + 25,
$x + 5, $y + 25
];
imagefilledpolygon($image, $points, 7, $color);
}
private function drawCross($image, $x, $y, $color) {
// Cruz para bautizos
imagefilledrectangle($image, $x + 15, $y, $x + 25, $y + 40, $color);
imagefilledrectangle($image, $x, $y + 15, $x + 40, $y + 25, $color);
}
private function drawGraduationCap($image, $x, $y, $color) {
// Birrete de graduación
imagefilledrectangle($image, $x, $y + 15, $x + 40, $y + 25, $color);
imagefilledpolygon($image, [$x - 5, $y + 15, $x + 45, $y + 15, $x + 40, $y, $x, $y], 4, $color);
imageline($image, $x + 40, $y, $x + 50, $y - 10, $color);
}
private function getSystemFont() {
// Intentar usar fuentes del sistema
$fonts = [
'/System/Library/Fonts/Arial.ttf',
'/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
'./fonts/arial.ttf',
5 // Fuente incorporada como fallback
];
foreach ($fonts as $font) {
if (is_string($font) && file_exists($font)) {
return $font;
}
}
return 5; // Fuente incorporada
}
public function saveFramedImage($framed_image, $output_path, $quality = 90) {
$extension = strtolower(pathinfo($output_path, PATHINFO_EXTENSION));
switch ($extension) {
case 'jpg':
case 'jpeg':
return imagejpeg($framed_image, $output_path, $quality);
case 'png':
return imagepng($framed_image, $output_path, (int)(9 - ($quality / 10)));
case 'webp':
return imagewebp($framed_image, $output_path, $quality);
default:
return imagejpeg($framed_image, $output_path, $quality);
}
}
}
/**
* Función de uso principal
*/
function processEventPhotos($galeria_id, $event_type, $apply_frames = true) {
if (!$apply_frames) {
return false; // No procesar si no se requieren marcos
}
try {
$db = Database::getInstance();
// Obtener datos de la galería
$sql = "SELECT * FROM galerias WHERE id = ?";
$stmt = $db->query($sql, [$galeria_id]);
$gallery = $stmt->fetch();
if (!$gallery) {
throw new Exception("Galería no encontrada");
}
// Obtener fotos
$sql = "SELECT * FROM fotos WHERE galeria_id = ?";
$stmt = $db->query($sql, [$galeria_id]);
$photos = $stmt->fetchAll();
$processor = new ImageFrameProcessor();
$processed_photos = [];
foreach ($photos as $photo) {
if (file_exists($photo['ruta_foto'])) {
// Procesar imagen con marco temático
$framed_image = $processor->processImage(
$photo['ruta_foto'],
$event_type,
$gallery['nombre_evento'],
$gallery['fecha_evento'],
$photo['nombre_invitado'] ?: 'Invitado'
);
if ($framed_image) {
// Crear directorio para imágenes procesadas
$processed_dir = './uploads/processed/';
if (!is_dir($processed_dir)) {
mkdir($processed_dir, 0755, true);
}
// Nombre del archivo procesado
$filename = pathinfo($photo['ruta_foto'], PATHINFO_FILENAME);
$processed_path = $processed_dir . $filename . '_framed.jpg';
// Guardar imagen procesada
if ($processor->saveFramedImage($framed_image, $processed_path, 95)) {
$processed_photos[] = $processed_path;
}
imagedestroy($framed_image);
}
}
}
return $processed_photos;
} catch (Exception $e) {
error_log("Error procesando fotos: " . $e->getMessage());
return false;
}
}
?>