239 lines
7.2 KiB
PHP
239 lines
7.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Service;
|
||
|
|
|
||
|
|
class BrandingFaviconService
|
||
|
|
{
|
||
|
|
private const MAX_SIZE = 5242880; // 5 MB
|
||
|
|
private const SIZES = [
|
||
|
|
16 => 'favicon-16x16.png',
|
||
|
|
32 => 'favicon-32x32.png',
|
||
|
|
96 => 'favicon-96x96.png',
|
||
|
|
180 => 'apple-touch-icon.png',
|
||
|
|
192 => 'web-app-manifest-192x192.png',
|
||
|
|
512 => 'web-app-manifest-512x512.png',
|
||
|
|
];
|
||
|
|
|
||
|
|
public static function storageBase(): string
|
||
|
|
{
|
||
|
|
if (defined('APP_STORAGE_PATH') && APP_STORAGE_PATH) {
|
||
|
|
return rtrim(APP_STORAGE_PATH, '/');
|
||
|
|
}
|
||
|
|
return rtrim(dirname(__DIR__, 2) . '/storage', '/');
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function storageDir(): string
|
||
|
|
{
|
||
|
|
return self::storageBase() . '/branding/favicon';
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function publicDir(): string
|
||
|
|
{
|
||
|
|
return rtrim(dirname(__DIR__, 2) . '/web/favicon', '/');
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function hasFavicon(): bool
|
||
|
|
{
|
||
|
|
$path = self::publicDir() . '/favicon-32x32.png';
|
||
|
|
return is_file($path);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function delete(): void
|
||
|
|
{
|
||
|
|
$dir = self::storageDir();
|
||
|
|
if (is_dir($dir)) {
|
||
|
|
$matches = glob($dir . '/*') ?: [];
|
||
|
|
foreach ($matches as $file) {
|
||
|
|
if (is_file($file)) {
|
||
|
|
@unlink($file);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$public = self::publicDir();
|
||
|
|
foreach (self::SIZES as $file) {
|
||
|
|
$target = $public . '/' . $file;
|
||
|
|
if (is_file($target)) {
|
||
|
|
@unlink($target);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function saveUpload(array $file): array
|
||
|
|
{
|
||
|
|
if (empty($file) || !isset($file['tmp_name'])) {
|
||
|
|
return ['ok' => false, 'error' => t('No file uploaded')];
|
||
|
|
}
|
||
|
|
if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
|
||
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
||
|
|
}
|
||
|
|
if (($file['size'] ?? 0) > self::MAX_SIZE) {
|
||
|
|
return ['ok' => false, 'error' => t('File is too large')];
|
||
|
|
}
|
||
|
|
|
||
|
|
$tmpPath = $file['tmp_name'];
|
||
|
|
$mime = self::detectMime($tmpPath);
|
||
|
|
if ($mime !== 'image/png') {
|
||
|
|
return ['ok' => false, 'error' => t('Invalid image file')];
|
||
|
|
}
|
||
|
|
if (!self::canResize()) {
|
||
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
||
|
|
}
|
||
|
|
|
||
|
|
$dir = self::storageDir();
|
||
|
|
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
|
||
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
||
|
|
}
|
||
|
|
|
||
|
|
self::delete();
|
||
|
|
$originalPath = $dir . '/original.png';
|
||
|
|
if (!move_uploaded_file($tmpPath, $originalPath)) {
|
||
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
||
|
|
}
|
||
|
|
@chmod($originalPath, 0644);
|
||
|
|
|
||
|
|
$publicDir = self::publicDir();
|
||
|
|
if (!is_dir($publicDir) && !mkdir($publicDir, 0755, true) && !is_dir($publicDir)) {
|
||
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach (self::SIZES as $size => $fileName) {
|
||
|
|
$target = $publicDir . '/' . $fileName;
|
||
|
|
if (!self::resizeSquare($originalPath, $target, $size)) {
|
||
|
|
return ['ok' => false, 'error' => t('Upload failed')];
|
||
|
|
}
|
||
|
|
@chmod($target, 0644);
|
||
|
|
}
|
||
|
|
|
||
|
|
self::updateManifest();
|
||
|
|
return ['ok' => true, 'path' => $originalPath, 'mime' => $mime];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function detectMime(string $path): string
|
||
|
|
{
|
||
|
|
if (function_exists('finfo_open')) {
|
||
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||
|
|
if ($finfo) {
|
||
|
|
$mime = finfo_file($finfo, $path);
|
||
|
|
if (is_string($mime) && $mime !== '') {
|
||
|
|
return $mime;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return 'application/octet-stream';
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function canResize(): bool
|
||
|
|
{
|
||
|
|
return function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled');
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function loadImage(string $path)
|
||
|
|
{
|
||
|
|
if (function_exists('imagecreatefrompng')) {
|
||
|
|
return imagecreatefrompng($path);
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function resizeSquare(string $sourcePath, string $targetPath, int $size): bool
|
||
|
|
{
|
||
|
|
$src = self::loadImage($sourcePath);
|
||
|
|
if (!$src) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
$srcWidth = imagesx($src);
|
||
|
|
$srcHeight = imagesy($src);
|
||
|
|
if ($srcWidth === 0 || $srcHeight === 0) {
|
||
|
|
imagedestroy($src);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$cropSize = min($srcWidth, $srcHeight);
|
||
|
|
$srcX = (int) round(($srcWidth - $cropSize) / 2);
|
||
|
|
$srcY = (int) round(($srcHeight - $cropSize) / 2);
|
||
|
|
|
||
|
|
$dst = imagecreatetruecolor($size, $size);
|
||
|
|
imagealphablending($dst, false);
|
||
|
|
imagesavealpha($dst, true);
|
||
|
|
$transparent = imagecolorallocatealpha($dst, 0, 0, 0, 127);
|
||
|
|
imagefilledrectangle($dst, 0, 0, $size, $size, $transparent);
|
||
|
|
|
||
|
|
imagecopyresampled(
|
||
|
|
$dst,
|
||
|
|
$src,
|
||
|
|
0,
|
||
|
|
0,
|
||
|
|
$srcX,
|
||
|
|
$srcY,
|
||
|
|
$size,
|
||
|
|
$size,
|
||
|
|
$cropSize,
|
||
|
|
$cropSize
|
||
|
|
);
|
||
|
|
|
||
|
|
$saved = false;
|
||
|
|
if (function_exists('imagepng')) {
|
||
|
|
$saved = imagepng($dst, $targetPath, 6);
|
||
|
|
}
|
||
|
|
|
||
|
|
imagedestroy($src);
|
||
|
|
imagedestroy($dst);
|
||
|
|
|
||
|
|
return $saved;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function updateManifest(): void
|
||
|
|
{
|
||
|
|
$manifestPath = self::publicDir() . '/site.webmanifest';
|
||
|
|
$data = [];
|
||
|
|
if (is_file($manifestPath)) {
|
||
|
|
$json = file_get_contents($manifestPath);
|
||
|
|
$decoded = json_decode((string) $json, true);
|
||
|
|
if (is_array($decoded)) {
|
||
|
|
$data = $decoded;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$title = null;
|
||
|
|
if (class_exists('MintyPHP\\Service\\SettingService')) {
|
||
|
|
$title = \MintyPHP\Service\SettingService::getAppTitle();
|
||
|
|
}
|
||
|
|
if ($title === null || $title === '') {
|
||
|
|
$title = defined('APP_NAME') ? APP_NAME : 'IMVS';
|
||
|
|
}
|
||
|
|
|
||
|
|
$data['name'] = $title;
|
||
|
|
$data['short_name'] = $title;
|
||
|
|
if (!isset($data['theme_color'])) {
|
||
|
|
$data['theme_color'] = '#ffffff';
|
||
|
|
}
|
||
|
|
if (!isset($data['background_color'])) {
|
||
|
|
$data['background_color'] = '#ffffff';
|
||
|
|
}
|
||
|
|
if (!isset($data['display'])) {
|
||
|
|
$data['display'] = 'standalone';
|
||
|
|
}
|
||
|
|
if (!isset($data['icons']) || !is_array($data['icons'])) {
|
||
|
|
$data['icons'] = [
|
||
|
|
[
|
||
|
|
'src' => '/favicon/web-app-manifest-192x192.png',
|
||
|
|
'sizes' => '192x192',
|
||
|
|
'type' => 'image/png',
|
||
|
|
'purpose' => 'maskable',
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'src' => '/favicon/web-app-manifest-512x512.png',
|
||
|
|
'sizes' => '512x512',
|
||
|
|
'type' => 'image/png',
|
||
|
|
'purpose' => 'maskable',
|
||
|
|
],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
$encoded = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||
|
|
if (is_string($encoded)) {
|
||
|
|
file_put_contents($manifestPath, $encoded . "\n");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|