Commit inicial con archivos existentes
This commit is contained in:
134
includes/get_gallery.php
Executable file
134
includes/get_gallery.php
Executable file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/session_check.php';
|
||||
require_once __DIR__ . '/db.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Directorio donde se almacenan las imágenes
|
||||
$uploadDir = __DIR__ . '/../uploads/';
|
||||
$baseUrl = '/bot/uploads/';
|
||||
|
||||
$response = [
|
||||
'success' => false,
|
||||
'message' => '',
|
||||
'images' => []
|
||||
];
|
||||
|
||||
try {
|
||||
// Verificar si el directorio existe
|
||||
if (!is_dir($uploadDir)) {
|
||||
throw new Exception('El directorio de imágenes no existe');
|
||||
}
|
||||
|
||||
// Obtener archivos del directorio
|
||||
$files = scandir($uploadDir);
|
||||
$imageFiles = array_filter($files, function($file) use ($uploadDir) {
|
||||
// Solo archivos de imagen
|
||||
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
||||
$imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
||||
return in_array($ext, $imageExtensions) && is_file($uploadDir . $file);
|
||||
});
|
||||
|
||||
// Construir la respuesta con las imágenes
|
||||
foreach ($imageFiles as $file) {
|
||||
$filePath = $uploadDir . $file;
|
||||
$fileUrl = $baseUrl . $file;
|
||||
|
||||
// Crear miniatura si no existe
|
||||
$thumbnailPath = $uploadDir . 'thumbs/' . $file;
|
||||
$thumbnailUrl = $baseUrl . 'thumbs/' . $file;
|
||||
|
||||
if (!file_exists($thumbnailPath)) {
|
||||
// Crear directorio de miniaturas si no existe
|
||||
if (!is_dir(dirname($thumbnailPath))) {
|
||||
mkdir(dirname($thumbnailPath), 0755, true);
|
||||
}
|
||||
|
||||
// Crear miniatura
|
||||
createThumbnail($filePath, $thumbnailPath, 200, 200);
|
||||
}
|
||||
|
||||
$response['images'][] = [
|
||||
'name' => $file,
|
||||
'url' => $fileUrl,
|
||||
'thumbnail' => file_exists($thumbnailPath) ? $thumbnailUrl : $fileUrl,
|
||||
'size' => filesize($filePath),
|
||||
'mtime' => filemtime($filePath)
|
||||
];
|
||||
}
|
||||
|
||||
$response['success'] = true;
|
||||
$response['message'] = count($response['images']) . ' imágenes encontradas';
|
||||
|
||||
} catch (Exception $e) {
|
||||
$response['message'] = 'Error al cargar la galería: ' . $e->getMessage();
|
||||
}
|
||||
|
||||
echo json_encode($response);
|
||||
|
||||
/**
|
||||
* Crea una miniatura de una imagen
|
||||
*/
|
||||
function createThumbnail($sourcePath, $destPath, $maxWidth, $maxHeight) {
|
||||
$sourceInfo = getimagesize($sourcePath);
|
||||
if (!$sourceInfo) return false;
|
||||
|
||||
list($origWidth, $origHeight, $type) = $sourceInfo;
|
||||
|
||||
// Calcular nuevas dimensiones manteniendo la relación de aspecto
|
||||
$ratio = $origWidth / $origHeight;
|
||||
if ($maxWidth / $maxHeight > $ratio) {
|
||||
$maxWidth = $maxHeight * $ratio;
|
||||
} else {
|
||||
$maxHeight = $maxWidth / $ratio;
|
||||
}
|
||||
|
||||
// Crear imagen de origen
|
||||
switch ($type) {
|
||||
case IMAGETYPE_JPEG:
|
||||
$source = imagecreatefromjpeg($sourcePath);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
$source = imagecreatefrompng($sourcePath);
|
||||
break;
|
||||
case IMAGETYPE_GIF:
|
||||
$source = imagecreatefromgif($sourcePath);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$source) return false;
|
||||
|
||||
// Crear imagen de destino
|
||||
$thumb = imagecreatetruecolor($maxWidth, $maxHeight);
|
||||
|
||||
// Preservar transparencia para PNG y GIF
|
||||
if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_GIF) {
|
||||
imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127));
|
||||
imagealphablending($thumb, false);
|
||||
imagesavealpha($thumb, true);
|
||||
}
|
||||
|
||||
// Redimensionar
|
||||
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $maxWidth, $maxHeight, $origWidth, $origHeight);
|
||||
|
||||
// Guardar miniatura
|
||||
switch ($type) {
|
||||
case IMAGETYPE_JPEG:
|
||||
imagejpeg($thumb, $destPath, 90);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
imagepng($thumb, $destPath, 9);
|
||||
break;
|
||||
case IMAGETYPE_GIF:
|
||||
imagegif($thumb, $destPath);
|
||||
break;
|
||||
}
|
||||
|
||||
// Liberar memoria
|
||||
imagedestroy($source);
|
||||
imagedestroy($thumb);
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user