Files
BarangaySystem/app/Http/Controllers/Helpers/PhotoURL.php
2026-06-06 18:43:00 +08:00

124 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Helpers;
use App\Models\FileList;
use Hypervel\Http\Request;
use App\Http\Controllers\FilesMainController;
class PhotoURL
{
public static function getDropzoneDetailsbyHashkey(string $filelist_hashkey): array|false
{
$fileList = FileList::where('hashkey', $filelist_hashkey)->first();
if (!$fileList) {
return false;
}
$fileContent = $fileList->fileContent()->first();
$file_url = $fileList->resolvedUrl();
$filename = $fileList->filename ?? $fileContent?->titlename ?? 'unknown_file';
$thumbnail = null;
return [
'url' => $file_url,
'name' => $filename,
'size' => $fileContent->size_in_bytes ?? 0,
'hashkey' => $fileList->hashkey,
'thumbnail' => $thumbnail ?? $file_url,
];
}
public static function photoURLArraytoDropzoneData(array|false|null $PhotoURLArray)
{
if (empty($PhotoURLArray)) {
return false;
}
$dropzoneArray = [];
foreach ($PhotoURLArray as $hashkey) {
$dropzoneArray[] = self::getDropzoneDetailsbyHashkey($hashkey) ?? [];
}
return $dropzoneArray;
}
function generateThumbnailGD(string $sourcePath, string $destinationPath, int $width = 200, int $height = 200): bool
{
if (!file_exists($sourcePath)) {
return false;
}
$imageInfo = getimagesize($sourcePath);
if (!$imageInfo) return false;
[$srcWidth, $srcHeight] = $imageInfo;
$mime = $imageInfo['mime'];
// Create image resource from source
switch ($mime) {
case 'image/jpeg':
$srcImage = imagecreatefromjpeg($sourcePath);
break;
case 'image/png':
$srcImage = imagecreatefrompng($sourcePath);
break;
case 'image/gif':
$srcImage = imagecreatefromgif($sourcePath);
break;
case 'image/webp':
$srcImage = imagecreatefromwebp($sourcePath);
break;
default:
return false; // unsupported
}
// Calculate new dimensions preserving aspect ratio
$aspectRatio = $srcWidth / $srcHeight;
if ($width / $height > $aspectRatio) {
$width = (int)($height * $aspectRatio);
} else {
$height = (int)($width / $aspectRatio);
}
// Create the new thumbnail image
$thumbImage = imagecreatetruecolor($width, $height);
// Preserve transparency for PNG/GIF
if (in_array($mime, ['image/png', 'image/gif'])) {
imagecolortransparent($thumbImage, imagecolorallocatealpha($thumbImage, 0, 0, 0, 127));
imagealphablending($thumbImage, false);
imagesavealpha($thumbImage, true);
}
// Resize
imagecopyresampled($thumbImage, $srcImage, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
// Save to file
switch ($mime) {
case 'image/jpeg':
imagejpeg($thumbImage, $destinationPath, 85);
break;
case 'image/png':
imagepng($thumbImage, $destinationPath);
break;
case 'image/gif':
imagegif($thumbImage, $destinationPath);
break;
case 'image/webp':
imagewebp($thumbImage, $destinationPath, 85);
break;
}
// Cleanup
imagedestroy($srcImage);
imagedestroy($thumbImage);
return true;
}
}