Files
kontrans.pagedev.pl/temp/cms_gen_favicon.php
2026-02-28 11:43:07 +01:00

75 lines
2.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
require_once '../config.php';
// ─── 1. Generuj favicon.png z logo ───────────────────────────────────────────
$logo_path = __DIR__ . '/../upload/filemanager/logo.png';
$logo = imagecreatefrompng($logo_path);
$lw = imagesx($logo); // 2560
$lh = imagesy($logo); // 373
// Wycinamy tylko lewą część z symbolem drogi (pierwsze ~20% szerokości)
$crop_w = (int)($lw * 0.20); // ~512px symbol drogi bez tekstu
$crop_h = $lh;
// Tworzymy kwadrat 512x512 z białym tłem
$size = 512;
$out = imagecreatetruecolor($size, $size);
$white = imagecolorallocate($out, 255, 255, 255);
imagefilledrectangle($out, 0, 0, $size-1, $size-1, $white);
imagealphablending($out, true);
// Skalujemy wycięty symbol, 88% wielkości kanwy (z marginesem)
$margin = 0.88;
$scale = min(($size * $margin) / $crop_w, ($size * $margin) / $crop_h);
$dst_w = (int)($crop_w * $scale);
$dst_h = (int)($crop_h * $scale);
$dst_x = (int)(($size - $dst_w) / 2);
$dst_y = (int)(($size - $dst_h) / 2);
imagecopyresampled($out, $logo, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $crop_w, $crop_h);
imagepng($out, __DIR__ . '/../favicon.png', 9);
imagedestroy($out);
imagedestroy($logo);
echo "favicon.png OK (crop {$crop_w}x{$crop_h}{$dst_w}x{$dst_h} in {$size}x{$size})\n";
// ─── 2. Zaktualizuj CSS field w DB ───────────────────────────────────────────
$pdo = new PDO(
"mysql:host={$database['host']};dbname={$database['name']};charset=utf8",
$database['user'], $database['password'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
$stmt = $pdo->query("SELECT css FROM pp_layouts WHERE id = 2");
$css = $stmt->fetchColumn();
// Zamień link favicon.svg → favicon.png
$new_css = preg_replace(
'~<link rel="icon"[^>]+href="/favicon\.svg"[^>]*>~',
'<link rel="icon" type="image/png" href="/favicon.png">',
$css
);
if ($new_css !== $css) {
$pdo->prepare("UPDATE pp_layouts SET css = ? WHERE id = 2")->execute([$new_css]);
echo "CSS field updated (svg → png).\n";
} else {
echo "Favicon link not found in CSS field (no change).\n";
}
// ─── 3. Wyczyść cache ─────────────────────────────────────────────────────────
$deleted = 0;
function clear_dir($dir, &$deleted) {
foreach (glob($dir . '/*') as $item) {
if (is_dir($item)) { clear_dir($item, $deleted); @rmdir($item); }
elseif (is_file($item) && basename($item) !== 'cms_gen_favicon.php') { unlink($item); $deleted++; }
}
}
clear_dir(__DIR__, $deleted);
echo "Cache cleared: $deleted files.\n";
unlink(__FILE__);
echo "Done.\n";
?>