|
|
@@ -293,6 +293,236 @@ class UtilService
|
|
|
* @return array|bool|string
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
+// public static function setSharePoster($config = array(), $path)
|
|
|
+// {
|
|
|
+// try {
|
|
|
+// $imageDefault = array(
|
|
|
+// 'left' => 0,
|
|
|
+// 'top' => 0,
|
|
|
+// 'right' => 0,
|
|
|
+// 'bottom' => 0,
|
|
|
+// 'width' => 100,
|
|
|
+// 'height' => 100,
|
|
|
+// 'opacity' => 100,
|
|
|
+// 'circle' => false // 新增:是否裁剪为圆形
|
|
|
+// );
|
|
|
+// $textDefault = array(
|
|
|
+// 'text' => '',
|
|
|
+// 'left' => 0,
|
|
|
+// 'top' => 0,
|
|
|
+// 'fontSize' => 32, //字号
|
|
|
+// 'fontColor' => '255,255,255', //字体颜色
|
|
|
+// 'angle' => 0,
|
|
|
+// );
|
|
|
+// $background = $config['background'];//海报最底层得背景
|
|
|
+// if (substr($background, 0, 1) === '/') {
|
|
|
+// $background = substr($background, 1);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 检查背景图片是否存在
|
|
|
+// if (!file_exists($background) && !filter_var($background, FILTER_VALIDATE_URL)) {
|
|
|
+// throw new \Exception('背景图片不存在: ' . $background);
|
|
|
+// }
|
|
|
+//
|
|
|
+// $backgroundInfo = getimagesize($background);
|
|
|
+// if ($backgroundInfo === false) {
|
|
|
+// throw new \Exception('无法获取背景图片信息');
|
|
|
+// }
|
|
|
+//
|
|
|
+// $backgroundContent = file_get_contents($background);
|
|
|
+// if ($backgroundContent === false) {
|
|
|
+// throw new \Exception('无法读取背景图片');
|
|
|
+// }
|
|
|
+//
|
|
|
+// $background = imagecreatefromstring($backgroundContent);
|
|
|
+// if ($background === false) {
|
|
|
+// throw new \Exception('无法创建背景图片资源');
|
|
|
+// }
|
|
|
+//
|
|
|
+// $backgroundWidth = $backgroundInfo[0]; //背景宽度
|
|
|
+// $backgroundHeight = $backgroundInfo[1]; //背景高度
|
|
|
+// $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
|
|
|
+//
|
|
|
+// // 创建黑色背景
|
|
|
+// $blackColor = imagecolorallocate($imageRes, 0, 0, 0);
|
|
|
+// imagefill($imageRes, 0, 0, $blackColor);
|
|
|
+// imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
|
|
|
+//
|
|
|
+// if (!empty($config['image'])) {
|
|
|
+// foreach ($config['image'] as $key => $val) {
|
|
|
+// $val = array_merge($imageDefault, $val);
|
|
|
+//
|
|
|
+// // 检查图片URL是否存在
|
|
|
+// if (empty($val['url'])) {
|
|
|
+// continue; // 跳过空的图片URL
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 处理URL:如果是相对路径,转换为绝对路径
|
|
|
+// $imageUrl = $val['url'];
|
|
|
+// if (!filter_var($imageUrl, FILTER_VALIDATE_URL) && file_exists($imageUrl)) {
|
|
|
+// // 如果是本地文件路径,直接使用
|
|
|
+// $imageContent = file_get_contents($imageUrl);
|
|
|
+// } else {
|
|
|
+// // 尝试获取远程图片
|
|
|
+// $imageContent = @file_get_contents($imageUrl);
|
|
|
+// }
|
|
|
+//
|
|
|
+// if ($imageContent === false) {
|
|
|
+// // 如果无法获取图片,跳过
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// $res = imagecreatefromstring($imageContent);
|
|
|
+// if ($res === false) {
|
|
|
+// // 如果无法创建图片资源,跳过
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// $resWidth = imagesx($res);
|
|
|
+// $resHeight = imagesy($res);
|
|
|
+//
|
|
|
+// // 如果是圆形,先创建圆形图片
|
|
|
+// if ($val['circle']) {
|
|
|
+// // 创建一个正方形的画布
|
|
|
+// $canvas = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
+// if ($canvas === false) {
|
|
|
+// imagedestroy($res);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 用白色填充背景
|
|
|
+// $whiteColor = imagecolorallocate($canvas, 255, 255, 255);
|
|
|
+// if ($whiteColor === false) {
|
|
|
+// imagedestroy($res);
|
|
|
+// imagedestroy($canvas);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// $fillResult = imagefill($canvas, 0, 0, $whiteColor);
|
|
|
+// if ($fillResult === false) {
|
|
|
+// imagedestroy($res);
|
|
|
+// imagedestroy($canvas);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 将原图缩放到指定大小
|
|
|
+// $sourceResized = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
+// if ($sourceResized === false) {
|
|
|
+// imagedestroy($res);
|
|
|
+// imagedestroy($canvas);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// $copyResult = imagecopyresampled($sourceResized, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
+// if ($copyResult === false) {
|
|
|
+// imagedestroy($res);
|
|
|
+// imagedestroy($canvas);
|
|
|
+// imagedestroy($sourceResized);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 创建圆形:只复制圆形区域内的像素
|
|
|
+// $centerX = $val['width'] / 2;
|
|
|
+// $centerY = $val['height'] / 2;
|
|
|
+// $radius = min($val['width'], $val['height']) / 2;
|
|
|
+//
|
|
|
+// for ($x = 0; $x < $val['width']; $x++) {
|
|
|
+// for ($y = 0; $y < $val['height']; $y++) {
|
|
|
+// $distance = sqrt(pow($x - $centerX, 2) + pow($y - $centerY, 2));
|
|
|
+// if ($distance <= $radius) {
|
|
|
+// $pixelColor = imagecolorat($sourceResized, $x, $y);
|
|
|
+// imagesetpixel($canvas, $x, $y, $pixelColor);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// imagedestroy($sourceResized);
|
|
|
+// imagedestroy($res);
|
|
|
+// } else {
|
|
|
+// // 非圆形,按原逻辑处理
|
|
|
+// $canvas = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
+// if ($canvas === false) {
|
|
|
+// imagedestroy($res);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// $bgColor = imagecolorallocate($canvas, 255, 255, 255);
|
|
|
+// if ($bgColor === false) {
|
|
|
+// imagedestroy($res);
|
|
|
+// imagedestroy($canvas);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// $fillResult = imagefill($canvas, 0, 0, $bgColor);
|
|
|
+// if ($fillResult === false) {
|
|
|
+// imagedestroy($res);
|
|
|
+// imagedestroy($canvas);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// $copyResult = imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
+// if ($copyResult === false) {
|
|
|
+// imagedestroy($res);
|
|
|
+// imagedestroy($canvas);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//
|
|
|
+// imagedestroy($res);
|
|
|
+// }
|
|
|
+//
|
|
|
+// $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
|
|
|
+// $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
|
|
|
+//
|
|
|
+// // 合并到主图像
|
|
|
+// $mergeResult = imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height'], $val['opacity']);
|
|
|
+// if ($mergeResult === false) {
|
|
|
+// // 合并失败,但继续处理其他图片
|
|
|
+// }
|
|
|
+//
|
|
|
+// imagedestroy($canvas);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // ... 后面的代码保持不变
|
|
|
+// if (isset($config['text']) && !empty($config['text'])) {
|
|
|
+// foreach ($config['text'] as $key => $val) {
|
|
|
+// $val = array_merge($textDefault, $val);
|
|
|
+// list($R, $G, $B) = explode(',', $val['fontColor']);
|
|
|
+// $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
|
|
|
+// $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
|
|
|
+// $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
|
|
|
+// imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// ob_start();
|
|
|
+// imagejpeg($imageRes);
|
|
|
+// imagedestroy($imageRes);
|
|
|
+// $res = ob_get_contents();
|
|
|
+// ob_end_clean();
|
|
|
+//
|
|
|
+// $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
|
|
|
+// $uploadType = (int)sys_config('upload_type', 1);
|
|
|
+// $upload = new Upload($uploadType, [
|
|
|
+// 'accessKey' => sys_config('accessKey'),
|
|
|
+// 'secretKey' => sys_config('secretKey'),
|
|
|
+// 'uploadUrl' => sys_config('uploadUrl'),
|
|
|
+// 'storageName' => sys_config('storage_name'),
|
|
|
+// 'storageRegion' => sys_config('storage_region'),
|
|
|
+// ]);
|
|
|
+// $res = $upload->to($path)->validate()->stream($res, $key);
|
|
|
+// if ($res === false) {
|
|
|
+// return $upload->getError();
|
|
|
+// } else {
|
|
|
+// $info = $upload->getUploadInfo();
|
|
|
+// $info['image_type'] = $uploadType;
|
|
|
+// return $info;
|
|
|
+// }
|
|
|
+// } catch (\Exception $e) {
|
|
|
+// @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-报错内容\r\n", 8);
|
|
|
+// return '生成图片时,系统错误: ' . $e->getMessage();
|
|
|
+// }
|
|
|
+// }
|
|
|
public static function setSharePoster($config = array(), $path)
|
|
|
{
|
|
|
try {
|
|
|
@@ -304,7 +534,7 @@ class UtilService
|
|
|
'width' => 100,
|
|
|
'height' => 100,
|
|
|
'opacity' => 100,
|
|
|
- 'circle' => false // 新增:是否裁剪为圆形
|
|
|
+ 'circle' => false // 是否裁剪为圆形
|
|
|
);
|
|
|
$textDefault = array(
|
|
|
'text' => '',
|
|
|
@@ -314,6 +544,7 @@ class UtilService
|
|
|
'fontColor' => '255,255,255', //字体颜色
|
|
|
'angle' => 0,
|
|
|
);
|
|
|
+
|
|
|
$background = $config['background'];//海报最底层得背景
|
|
|
if (substr($background, 0, 1) === '/') {
|
|
|
$background = substr($background, 1);
|
|
|
@@ -341,13 +572,22 @@ class UtilService
|
|
|
|
|
|
$backgroundWidth = $backgroundInfo[0]; //背景宽度
|
|
|
$backgroundHeight = $backgroundInfo[1]; //背景高度
|
|
|
+
|
|
|
+ // 创建主图像,支持透明度
|
|
|
$imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
|
|
|
|
|
|
- // 创建黑色背景
|
|
|
- $blackColor = imagecolorallocate($imageRes, 0, 0, 0);
|
|
|
- imagefill($imageRes, 0, 0, $blackColor);
|
|
|
+ // 设置透明背景
|
|
|
+ imagealphablending($imageRes, false);
|
|
|
+ imagesavealpha($imageRes, true);
|
|
|
+ $transparent = imagecolorallocatealpha($imageRes, 255, 255, 255, 127);
|
|
|
+ imagefilledrectangle($imageRes, 0, 0, $backgroundWidth, $backgroundHeight, $transparent);
|
|
|
+
|
|
|
+ // 复制背景图片
|
|
|
imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
|
|
|
|
|
|
+ // 恢复混合模式
|
|
|
+ imagealphablending($imageRes, true);
|
|
|
+
|
|
|
if (!empty($config['image'])) {
|
|
|
foreach ($config['image'] as $key => $val) {
|
|
|
$val = array_merge($imageDefault, $val);
|
|
|
@@ -381,51 +621,29 @@ class UtilService
|
|
|
$resWidth = imagesx($res);
|
|
|
$resHeight = imagesy($res);
|
|
|
|
|
|
- // 如果是圆形,先创建圆形图片
|
|
|
+ // 如果是圆形,创建圆形图片
|
|
|
if ($val['circle']) {
|
|
|
- // 创建一个正方形的画布
|
|
|
+ // 创建一个支持透明的画布
|
|
|
$canvas = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
- if ($canvas === false) {
|
|
|
- imagedestroy($res);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // 用白色填充背景
|
|
|
- $whiteColor = imagecolorallocate($canvas, 255, 255, 255);
|
|
|
- if ($whiteColor === false) {
|
|
|
- imagedestroy($res);
|
|
|
- imagedestroy($canvas);
|
|
|
- continue;
|
|
|
- }
|
|
|
|
|
|
- $fillResult = imagefill($canvas, 0, 0, $whiteColor);
|
|
|
- if ($fillResult === false) {
|
|
|
- imagedestroy($res);
|
|
|
- imagedestroy($canvas);
|
|
|
- continue;
|
|
|
- }
|
|
|
+ // 设置透明背景
|
|
|
+ imagealphablending($canvas, false);
|
|
|
+ imagesavealpha($canvas, true);
|
|
|
+ $transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
|
|
|
+ imagefilledrectangle($canvas, 0, 0, $val['width'], $val['height'], $transparent);
|
|
|
|
|
|
// 将原图缩放到指定大小
|
|
|
$sourceResized = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
- if ($sourceResized === false) {
|
|
|
- imagedestroy($res);
|
|
|
- imagedestroy($canvas);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- $copyResult = imagecopyresampled($sourceResized, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
- if ($copyResult === false) {
|
|
|
- imagedestroy($res);
|
|
|
- imagedestroy($canvas);
|
|
|
- imagedestroy($sourceResized);
|
|
|
- continue;
|
|
|
- }
|
|
|
+ imagecopyresampled($sourceResized, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
|
|
|
// 创建圆形:只复制圆形区域内的像素
|
|
|
$centerX = $val['width'] / 2;
|
|
|
$centerY = $val['height'] / 2;
|
|
|
$radius = min($val['width'], $val['height']) / 2;
|
|
|
|
|
|
+ // 恢复混合模式
|
|
|
+ imagealphablending($canvas, true);
|
|
|
+
|
|
|
for ($x = 0; $x < $val['width']; $x++) {
|
|
|
for ($y = 0; $y < $val['height']; $y++) {
|
|
|
$distance = sqrt(pow($x - $centerX, 2) + pow($y - $centerY, 2));
|
|
|
@@ -441,31 +659,11 @@ class UtilService
|
|
|
} else {
|
|
|
// 非圆形,按原逻辑处理
|
|
|
$canvas = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
- if ($canvas === false) {
|
|
|
- imagedestroy($res);
|
|
|
- continue;
|
|
|
- }
|
|
|
|
|
|
$bgColor = imagecolorallocate($canvas, 255, 255, 255);
|
|
|
- if ($bgColor === false) {
|
|
|
- imagedestroy($res);
|
|
|
- imagedestroy($canvas);
|
|
|
- continue;
|
|
|
- }
|
|
|
+ imagefill($canvas, 0, 0, $bgColor);
|
|
|
|
|
|
- $fillResult = imagefill($canvas, 0, 0, $bgColor);
|
|
|
- if ($fillResult === false) {
|
|
|
- imagedestroy($res);
|
|
|
- imagedestroy($canvas);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- $copyResult = imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
- if ($copyResult === false) {
|
|
|
- imagedestroy($res);
|
|
|
- imagedestroy($canvas);
|
|
|
- continue;
|
|
|
- }
|
|
|
+ imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
|
|
|
imagedestroy($res);
|
|
|
}
|
|
|
@@ -474,16 +672,18 @@ class UtilService
|
|
|
$val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
|
|
|
|
|
|
// 合并到主图像
|
|
|
- $mergeResult = imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height'], $val['opacity']);
|
|
|
- if ($mergeResult === false) {
|
|
|
- // 合并失败,但继续处理其他图片
|
|
|
+ if ($val['circle']) {
|
|
|
+ // 对于圆形图片,使用imagecopy以支持透明度
|
|
|
+ imagecopy($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height']);
|
|
|
+ } else {
|
|
|
+ // 对于普通图片,使用imagecopymerge
|
|
|
+ imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height'], $val['opacity']);
|
|
|
}
|
|
|
|
|
|
imagedestroy($canvas);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // ... 后面的代码保持不变
|
|
|
if (isset($config['text']) && !empty($config['text'])) {
|
|
|
foreach ($config['text'] as $key => $val) {
|
|
|
$val = array_merge($textDefault, $val);
|
|
|
@@ -495,8 +695,11 @@ class UtilService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 输出图片前再次确保保存alpha通道
|
|
|
+ imagesavealpha($imageRes, true);
|
|
|
+
|
|
|
ob_start();
|
|
|
- imagejpeg($imageRes);
|
|
|
+ imagejpeg($imageRes, null, 90); // 使用较高质量
|
|
|
imagedestroy($imageRes);
|
|
|
$res = ob_get_contents();
|
|
|
ob_end_clean();
|
|
|
@@ -524,6 +727,26 @@ class UtilService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 辅助函数:带透明度的图像复制
|
|
|
+ * 注意:这个函数在PHP 5.5+中可能不需要,因为imagecopy已经支持透明度
|
|
|
+ */
|
|
|
+ function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) {
|
|
|
+ // 创建一个临时图像
|
|
|
+ $tmp = imagecreatetruecolor($src_w, $src_h);
|
|
|
+
|
|
|
+ // 复制源图像到临时图像
|
|
|
+ imagecopy($tmp, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
|
|
|
+
|
|
|
+ // 合并源图像到临时图像
|
|
|
+ imagecopy($tmp, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
|
|
|
+
|
|
|
+ // 合并临时图像到目标图像
|
|
|
+ imagecopymerge($dst_im, $tmp, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
|
|
|
+
|
|
|
+ // 销毁临时图像
|
|
|
+ imagedestroy($tmp);
|
|
|
+ }
|
|
|
/**
|
|
|
* TODO 获取小程序二维码是否生成
|
|
|
* @param $url
|