//绘图
public function draw_image($text,$address) {
$image = imagecreatefrompng('usdt.png');
$color = imagecolorallocate($image, 0, 0, 0);
// 使用 TrueType 字体
$font_size = 40;
$font_path = 'DejaVuSans.ttf';
// 获取图片尺寸
$img_width = imagesx($image);
$img_height = imagesy($image);
// 计算金额文字位置使其居中
$bbox = imagettfbbox($font_size, 0, $font_path, $text);
$text_width = $bbox[2] - $bbox[0];
$text_height = $bbox[1] - $bbox[7];
$x = ($img_width - $text_width) / 2;
$y = 140 + $text_height;
// 绘制金额
imagettftext($image, $font_size, 0, $x, $y, $color, $font_path, $text);
// 添加地址文本
$font_size_address = 15;
// 计算地址文字位置
$bbox_address = imagettfbbox($font_size_address, 0, $font_path, $address);
$address_width = $bbox_address[2] - $bbox_address[0];
$x_address = ($img_width - $address_width) / 2 + 150;
$y_address = $img_height - 73;
// 绘制地址
imagettftext($image, $font_size_address, 0, $x_address, $y_address, $color, $font_path, $address);
// 生成随机文件名
$random_name = uniqid() . '.png';
// 使用完整的服务器路径
$upload_dir = ROOT_PATH . 'public/usdt/';
// 确保目录存在并设置正确的权限
if (!is_dir($upload_dir)) {
if (!mkdir($upload_dir, 0777, true)) {
throw new \Exception('Failed to create directory: ' . $upload_dir);
}
chmod($upload_dir, 0777);
}
// 完整的文件路径
$file_path = $upload_dir . $random_name;
// 保存图片
if (!imagepng($image, $file_path)) {
throw new \Exception('Failed to save image: ' . $file_path);
}
// 设置文件权限
chmod($file_path, 0666);
// 释放内存
imagedestroy($image);
return $random_name;
}
发表回复