197 行
8.4 KiB
PHP
197 行
8.4 KiB
PHP
<?php
|
||
/**
|
||
* 生成测试用户头像与商家Logo资源
|
||
*
|
||
* @return void
|
||
*/
|
||
function generateAvatarsAndLogos() {
|
||
$avatarDir = __DIR__ . '/../public/uploads/avatars';
|
||
$merchantDir = __DIR__ . '/../public/uploads/merchants';
|
||
|
||
if (!is_dir($avatarDir)) {
|
||
mkdir($avatarDir, 0755, true);
|
||
}
|
||
if (!is_dir($merchantDir)) {
|
||
mkdir($merchantDir, 0755, true);
|
||
}
|
||
|
||
// 定义清新色盘(Mint Green 主题协调配色:翠绿、薄荷青、暖橙、湖蓝、薰衣草紫、青黛、珊瑚红等)
|
||
$palette = [
|
||
'green' => [[0, 210, 106], [0, 168, 84]], // 品牌浅翠绿
|
||
'mint' => [[16, 185, 129], [5, 150, 105]], // 薄荷青
|
||
'blue' => [[59, 130, 246], [37, 99, 235]], // 湖蓝
|
||
'orange' => [[245, 158, 11], [217, 119, 6]], // 阳光金橙
|
||
'teal' => [[20, 184, 166], [13, 148, 136]], // 青碧
|
||
'purple' => [[139, 92, 246], [124, 58, 237]], // 浅紫
|
||
'rose' => [[244, 63, 94], [225, 29, 72]], // 珊瑚粉
|
||
'slate' => [[100, 116, 139], [71, 85, 105]], // 雅灰
|
||
];
|
||
|
||
/**
|
||
* 生成带首字徽标的现代化圆形/微圆角PNG头像
|
||
*
|
||
* @param string $filePath 输出文件绝对路径
|
||
* @param string $char 显示的文字(1个汉字或英文)
|
||
* @param array $theme 配色方案
|
||
* @param int $size 图片尺寸
|
||
* @param bool $isSquare 是否为商家微圆角方形
|
||
* @return void
|
||
*/
|
||
$createBadge = function ($filePath, $char, $theme, $size = 128, $isSquare = false) {
|
||
$img = imagecreatetruecolor($size, $size);
|
||
imagealphablending($img, false);
|
||
imagesavealpha($img, true);
|
||
|
||
$trans = imagecolorallocatealpha($img, 0, 0, 0, 127);
|
||
imagefill($img, 0, 0, $trans);
|
||
|
||
$c1 = $theme[0];
|
||
$c2 = $theme[1];
|
||
|
||
// 绘制渐变背景
|
||
for ($y = 0; $y < $size; $y++) {
|
||
$ratio = $y / $size;
|
||
$r = (int)($c1[0] * (1 - $ratio) + $c2[0] * $ratio);
|
||
$g = (int)($c1[1] * (1 - $ratio) + $c2[1] * $ratio);
|
||
$b = (int)($c1[2] * (1 - $ratio) + $c2[2] * $ratio);
|
||
$col = imagecolorallocate($img, $r, $g, $b);
|
||
|
||
for ($x = 0; $x < $size; $x++) {
|
||
if ($isSquare) {
|
||
// 圆角矩形(圆角半径 24)
|
||
$rad = 24;
|
||
$in = true;
|
||
if ($x < $rad && $y < $rad) {
|
||
$in = (($x - $rad) * ($x - $rad) + ($y - $rad) * ($y - $rad)) <= ($rad * $rad);
|
||
} elseif ($x >= ($size - $rad) && $y < $rad) {
|
||
$in = (($x - ($size - $rad)) * ($x - ($size - $rad)) + ($y - $rad) * ($y - $rad)) <= ($rad * $rad);
|
||
} elseif ($x < $rad && $y >= ($size - $rad)) {
|
||
$in = (($x - $rad) * ($x - $rad) + ($y - ($size - $rad)) * ($y - ($size - $rad))) <= ($rad * $rad);
|
||
} elseif ($x >= ($size - $rad) && $y >= ($size - $rad)) {
|
||
$in = (($x - ($size - $rad)) * ($x - ($size - $rad)) + ($y - ($size - $rad)) * ($y - ($size - $rad))) <= ($rad * $rad);
|
||
}
|
||
if ($in) {
|
||
imagesetpixel($img, $x, $y, $col);
|
||
}
|
||
} else {
|
||
// 圆形头像
|
||
$cx = $size / 2;
|
||
$cy = $size / 2;
|
||
$distSq = ($x - $cx) * ($x - $cx) + ($y - $cy) * ($y - $cy);
|
||
if ($distSq <= ($size / 2) * ($size / 2)) {
|
||
imagesetpixel($img, $x, $y, $col);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
imagealphablending($img, true);
|
||
|
||
// 尝试加载系统常见中文字体,若无则使用 imagestring 兜底
|
||
$fontCandidates = [
|
||
'C:\\Windows\\Fonts\\msyh.ttc', // 微软雅黑
|
||
'C:\\Windows\\Fonts\\msyh.ttf',
|
||
'C:\\Windows\\Fonts\\simhei.ttf', // 黑体
|
||
'C:\\Windows\\Fonts\\simsun.ttc',
|
||
];
|
||
$fontFile = null;
|
||
foreach ($fontCandidates as $f) {
|
||
if (file_exists($f)) {
|
||
$fontFile = $f;
|
||
break;
|
||
}
|
||
}
|
||
|
||
$white = imagecolorallocate($img, 255, 255, 255);
|
||
if ($fontFile) {
|
||
$fontSize = $isSquare ? 46 : 42;
|
||
$box = imagettfbbox($fontSize, 0, $fontFile, $char);
|
||
$tw = $box[2] - $box[0];
|
||
$th = $box[1] - $box[7];
|
||
$tx = (int)(($size - $tw) / 2) - $box[0];
|
||
$ty = (int)(($size - $th) / 2) - $box[7];
|
||
imagettftext($img, $fontSize, 0, $tx, $ty, $white, $fontFile, $char);
|
||
} else {
|
||
$fSize = 5;
|
||
$tw = imagefontwidth($fSize) * strlen($char);
|
||
$th = imagefontheight($fSize);
|
||
$tx = (int)(($size - $tw) / 2);
|
||
$ty = (int)(($size - $th) / 2);
|
||
imagestring($img, $fSize, $tx, $ty, $char, $white);
|
||
}
|
||
|
||
imagepng($img, $filePath, 6);
|
||
imagedestroy($img);
|
||
};
|
||
|
||
// 1. 生成用户头像列表
|
||
$userAvatars = [
|
||
'user_1001.png' => ['助', $palette['green']],
|
||
'user_1002.png' => ['王', $palette['blue']],
|
||
'user_1003.png' => ['洁', $palette['teal']],
|
||
'user_1004.png' => ['测', $palette['slate']],
|
||
'user_2001.png' => ['顺', $palette['orange']],
|
||
'user_2002.png' => ['安', $palette['green']],
|
||
'user_2003.png' => ['骑', $palette['rose']],
|
||
'user_2004.png' => ['车', $palette['blue']],
|
||
'user_2005.png' => ['净', $palette['teal']],
|
||
'user_2006.png' => ['豪', $palette['purple']],
|
||
'user_2007.png' => ['优', $palette['orange']],
|
||
'user_2008.png' => ['琴', $palette['rose']],
|
||
'user_2009.png' => ['筑', $palette['green']],
|
||
'user_2010.png' => ['黔', $palette['blue']],
|
||
'user_2027.png' => ['林', $palette['teal']],
|
||
'user_2028.png' => ['超', $palette['orange']],
|
||
'user_default.png' => ['同', $palette['green']],
|
||
];
|
||
|
||
foreach ($userAvatars as $fileName => $cfg) {
|
||
$path = $avatarDir . '/' . $fileName;
|
||
$createBadge($path, $cfg[0], $cfg[1], 128, false);
|
||
}
|
||
echo "已成功生成 " . count($userAvatars) . " 个用户头像到 public/uploads/avatars/" . PHP_EOL;
|
||
|
||
// 2. 生成商家 Logo 列表
|
||
$merchantLogos = [
|
||
'm_shunjie.png' => ['顺', $palette['orange']],
|
||
'm_anjia.png' => ['安', $palette['green']],
|
||
'm_jiejing.png' => ['洁', $palette['teal']],
|
||
'm_chehang.png' => ['车', $palette['blue']],
|
||
'm_youxuan.png' => ['优', $palette['purple']],
|
||
'm_zaowang.png' => ['枣', $palette['rose']],
|
||
];
|
||
|
||
foreach ($merchantLogos as $fileName => $cfg) {
|
||
$path = $merchantDir . '/' . $fileName;
|
||
$createBadge($path, $cfg[0], $cfg[1], 128, true);
|
||
}
|
||
echo "已成功生成 " . count($merchantLogos) . " 个商家Logo到 public/uploads/merchants/" . PHP_EOL;
|
||
|
||
// 3. 生成商家营业执照模拟图
|
||
$licenseImg = imagecreatetruecolor(400, 280);
|
||
$bg = imagecolorallocate($licenseImg, 252, 250, 242); // 羊皮纸色
|
||
imagefill($licenseImg, 0, 0, $bg);
|
||
$border = imagecolorallocate($licenseImg, 180, 160, 120);
|
||
imagerectangle($licenseImg, 10, 10, 389, 269, $border);
|
||
imagerectangle($licenseImg, 14, 14, 385, 265, $border);
|
||
$textDark = imagecolorallocate($licenseImg, 50, 50, 50);
|
||
$red = imagecolorallocate($licenseImg, 220, 38, 38);
|
||
|
||
$fontFile = 'C:\\Windows\\Fonts\\msyh.ttc';
|
||
if (file_exists($fontFile)) {
|
||
imagettftext($licenseImg, 18, 0, 140, 60, $textDark, $fontFile, '营业执照');
|
||
imagettftext($licenseImg, 10, 0, 40, 100, $textDark, $fontFile, '统一社会信用代码:91520102MA6XYZ8888');
|
||
imagettftext($licenseImg, 10, 0, 40, 130, $textDark, $fontFile, '类型:有限责任公司(自然人独资)');
|
||
imagettftext($licenseImg, 10, 0, 40, 160, $textDark, $fontFile, '登记机关:贵阳市市场监督管理局');
|
||
imagettftext($licenseImg, 10, 0, 40, 190, $textDark, $fontFile, '经营状态:开业(官方核验通过)');
|
||
// 模拟红色印章
|
||
imageellipse($licenseImg, 320, 200, 60, 60, $red);
|
||
imagettftext($licenseImg, 9, 0, 298, 204, $red, $fontFile, '官方核验');
|
||
}
|
||
imagepng($licenseImg, $merchantDir . '/license_default.png', 6);
|
||
imagedestroy($licenseImg);
|
||
echo "已成功生成营业执照模拟图到 public/uploads/merchants/license_default.png" . PHP_EOL;
|
||
}
|
||
|
||
generateAvatarsAndLogos();
|