* @day: 2017/11/02 */ namespace crmeb\services; use crmeb\exceptions\UtilException; use think\facade\Config; use dh2y\qrcode\QRcode; use crmeb\services\upload\Upload; class UtilService { public static function filtrate($string, $admin = false) { $ra = array( // '/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '/script/', '/javascript/', '/vbscript/', '/expression/', '/applet/', '/meta/', '/xml/', '/blink/', '/link/', '/style/', '/embed/', '/object/', '/frame/', '/layer/', '/title/', '/bgsound/', '/base/', '/onload/', '/onunload/', '/onchange/', '/onsubmit/', '/onreset/', '/onselect/', '/onblur/', '/onfocus/', '/onabort/', '/onkeydown/', '/onkeypress/', '/onkeyup/', '/onclick/', '/ondblclick/', '/onmousedown/', '/onmousemove/', '/onmouseout/', '/onmouseover/', '/onmouseup/', '/onunload/', "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU", "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU", ); $str = preg_replace($ra, "", $string); if ($admin) { return $str; } return addslashes($str); } public static function sweep($array, $admin = false) { if (is_array($array)) { foreach ($array as $k => $v) { $array[$k] = self::sweep($v, $admin); } } else { $array = self::filtrate($array, $admin); } return $array; } /** * 获取POST请求的数据 * @param $params * @param null $request * @param bool $suffix * @return array */ public static function postMore($params, $request = null, $suffix = false, $admin = false) { if ($request === null) $request = app('request'); $p = []; $i = 0; foreach ($params as $param) { if (!is_array($param)) { $p[$suffix == true ? $i++ : $param] = self::sweep($request->param($param), $admin); } else { if (!isset($param[1])) $param[1] = null; if (!isset($param[2])) $param[2] = ''; if (!isset($param[3])) $param[3] = ''; if (!isset($param[4])) $param[4] = ''; if (!isset($param[5])) $param[5] = ''; if (is_array($param[0])) { $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1]; $keyName = $param[0][0]; } else { $name = is_array($param[1]) ? $param[0] . '/a' : $param[0]; $keyName = $param[0]; } $p[$suffix == true ? $i++ : ($param[3] ? $param[3] : $keyName)] = self::sweep($request->param($name, $param[1], $param[2]), $admin); if (not_empty_check($param[4])) { if (!is_array($param[4])) { if (is_string($param[4]) && !function_exists($param[4])) { throw new UtilException('验证的方法' . $param[4] . '未定义'); } if (!$param[4]($request->param($name, $param[1], $param[2]))) { throw new UtilException($param[5]); } } else { foreach ($param[4] as $k => $v) { if (is_string($v) && !function_exists($v)) { throw new UtilException('验证的方法' . $v . '未定义'); } if (!$v($request->param($name, $param[1], $param[2]))) { throw new UtilException(is_array($param[5]) ? $param[5][$k] : $param[5]); } } } } } } return $p; } /** * 获取请求的数据 * @param $params * @param null $request * @param bool $suffix * @return array */ public static function getMore($params, $request = null, $suffix = false) { if ($request === null) $request = app('request'); $p = []; $i = 0; foreach ($params as $param) { if (!is_array($param)) { $p[$suffix == true ? $i++ : $param] = self::sweep($request->param($param)); } else { if (!isset($param[1])) $param[1] = null; if (!isset($param[2])) $param[2] = ''; if (!isset($param[3])) $param[3] = ''; if (!isset($param[4])) $param[4] = ''; if (!isset($param[5])) $param[5] = '参数错误'; if (is_array($param[0])) { $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1]; $keyName = $param[0][0]; } else { $name = is_array($param[1]) ? $param[0] . '/a' : $param[0]; $keyName = $param[0]; } $p[$suffix == true ? $i++ : ($param[3] ? $param[3] : $keyName)] = self::sweep($request->param($name, $param[1], $param[2])); if (not_empty_check($param[4])) { if (!is_array($param[4])) { if (is_string($param[4]) && !function_exists($param[4])) { throw new UtilException('验证的方法' . $param[4] . '未定义'); } if (!$param[4]($request->param($name, $param[1], $param[2]))) { throw new UtilException($param[5]); } } else { foreach ($param[4] as $k => $v) { if (is_string($v) && !function_exists($v)) { throw new UtilException('验证的方法' . $v . '未定义'); } if (!$v($request->param($name, $param[1], $param[2]))) { throw new UtilException(is_array($param[5]) ? $param[5][$k] : $param[5]); } } } } } } return $p; } /** * TODO 砍价 拼团 分享海报生成 * @param array $data * @param $path * @return array|bool|string * @throws \Exception */ public static function setShareMarketingPoster($data = array(), $path) { $config = array( 'text' => array( array( 'text' => $data['price'],//TODO 价格 'left' => 116, 'top' => 200, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件 'fontSize' => 50, //字号 'fontColor' => '255,0,0', //字体颜色 'angle' => 0, ), array( 'text' => $data['label'],//TODO 标签 'left' => 450, 'top' => 188, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件 'fontSize' => 24, //字号 'fontColor' => '255,255,255', //字体颜色 'angle' => 0, ), array( 'text' => $data['msg'],//TODO 简述 'left' => 80, 'top' => 270, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件 'fontSize' => 22, //字号 'fontColor' => '40,40,40', //字体颜色 'angle' => 0, ) ), 'image' => array( array( 'url' => $data['image'], //图片 'stream' => 0, 'left' => 120, 'top' => 340, 'right' => 0, 'bottom' => 0, 'width' => 450, 'height' => 450, 'opacity' => 100 ), array( 'url' => $data['url'], //二维码资源 'stream' => 0, 'left' => 260, 'top' => 890, 'right' => 0, 'bottom' => 0, 'width' => 160, 'height' => 160, 'opacity' => 100 ) ), 'background' => 'static/poster/poster.jpg' ); if (!file_exists($config['background'])) exception('缺少系统预设背景图片'); if (strlen($data['title']) < 36) { $text = array( 'text' => $data['title'],//TODO 标题 'left' => 76, 'top' => 100, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件 'fontSize' => 32, //字号 'fontColor' => '0,0,0', //字体颜色 'angle' => 0, ); array_push($config['text'], $text); } else { $titleOne = array( 'text' => mb_strimwidth($data['title'], 0, 24),//TODO 标题 'left' => 76, 'top' => 70, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件 'fontSize' => 32, //字号 'fontColor' => '0,0,0', //字体颜色 'angle' => 0, ); $titleTwo = array( 'text' => mb_strimwidth($data['title'], 24, 24),//TODO 标题 'left' => 76, 'top' => 120, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件 'fontSize' => 32, //字号 'fontColor' => '0,0,0', //字体颜色 'angle' => 0, ); array_push($config['text'], $titleOne); array_push($config['text'], $titleTwo); } return self::setSharePoster($config, $path); } /** * TODO 生成分享二维码图片 * @param array $config * @param $path * @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); // 设置透明背景 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); // 检查图片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']); // 设置透明背景 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']); 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)); if ($distance <= $radius) { $pixelColor = imagecolorat($sourceResized, $x, $y); imagesetpixel($canvas, $x, $y, $pixelColor); } } } imagedestroy($sourceResized); imagedestroy($res); } else { // 非圆形,按原逻辑处理 $canvas = imagecreatetruecolor($val['width'], $val['height']); $bgColor = imagecolorallocate($canvas, 255, 255, 255); imagefill($canvas, 0, 0, $bgColor); imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight); 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']; // 合并到主图像 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); 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']); } } // 输出图片前再次确保保存alpha通道 imagesavealpha($imageRes, true); ob_start(); imagejpeg($imageRes, null, 90); // 使用较高质量 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(); } } /** * 辅助函数:带透明度的图像复制 * 注意:这个函数在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 * @return array */ public static function remoteImage($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($curl); $result = json_decode($result, true); if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']]; return ['status' => true]; } /** * TODO 修改 https 和 http 移动到common * @param $url $url 域名 * @param int $type 0 返回https 1 返回 http * @return string */ public static function setHttpType($url, $type = 0) { $domainTop = substr($url, 0, 5); if ($type) { if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url)); } else { if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url)); } return $url; } /** * 获取二维码 * @param $url * @param $name * @return array|bool|string */ public static function getQRCodePath($url, $name) { if (!strlen(trim($url)) || !strlen(trim($name))) return false; try { $uploadType = sys_config('upload_type'); //TODO 没有选择默认使用本地上传 if (!$uploadType) $uploadType = 1; $uploadType = (int)$uploadType; $siteUrl = sys_config('site_url'); if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名'; $info = []; $outfile = Config::get('qrcode.cache_dir'); $code = new QRcode(); $wapCodePath = $code->png($url, $outfile . '/' . $name)->getPath(); //获取二维码生成的地址 $content = file_get_contents('.' . $wapCodePath); if ($uploadType === 1) { $info["code"] = 200; $info["name"] = $name; $info["dir"] = $wapCodePath; $info["time"] = time(); $info['size'] = 0; $info['type'] = 'image/png'; $info["image_type"] = 1; $info['thumb_path'] = $wapCodePath; return $info; } else { $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($outfile)->validate()->stream($content, $name); if ($res === false) { return $upload->getError(); } $info = $upload->getUploadInfo(); $info['image_type'] = $uploadType; return $info; } } catch (\Exception $e) { return $e->getMessage(); } } /** * @param array $data * @param string $path * @return array|bool|string * @throws \Exception */ public static function setShareProductPoster($data = array(), $path = '') { $config = array( 'text' => array( array( 'text' => $data['price'],//TODO 价格 'left' => 219, 'top' => 1055, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件 'fontSize' => 50, //字号 'fontColor' => '131,28,32', //字体颜色 'angle' => 0, ), array( 'text' => '原价:¥' . $data['ot_price'],//TODO 原价 'left' => 94, 'top' => 1105, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Light-My.otf', //字体文件 'fontSize' => 18, //字号 'fontColor' => '0,0,0', //字体颜色 'angle' => 0, ), array( 'text' => $data['user_nickname'],//TODO 用户名 'left' => 220, 'top' => 240, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Light.ttf', //字体文件 'fontSize' => 20, //字号 'fontColor' => '0,0,0', //字体颜色 'angle' => 0, ), array( 'text' => implode($data['user_nickname_pinyin'], ' '),//TODO 用户名拼音 'left' => 220, 'top' => 260, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Light.ttf', //字体文件 'fontSize' => 10, //字号 'fontColor' => '0,0,0', //字体颜色 'angle' => 0, ), ), 'image' => array( array( 'url' => $data['image'], //图片 'stream' => 0, 'left' => 94, 'top' => 359, 'right' => 0, 'bottom' => 0, 'width' => 561, 'height' => 613, 'opacity' => 100 ), array( 'url' => $data['url'], //二维码资源 'stream' => 0, 'left' => 557, 'top' => 1013, 'right' => 0, 'bottom' => 0, 'width' => 99, 'height' => 99, 'opacity' => 100 ), array( 'url' => $data['user_avatar'], //头像资源 'stream' => 0, 'left' => 98, 'top' => 211, 'right' => 0, 'bottom' => 0, 'width' => 92, 'height' => 92, 'opacity' => 100 ) ), 'background' => 'static/poster/product.jpg' ); if (!file_exists($config['background'])) exception('缺少系统预设背景图片'); if (strlen($data['title']) < 27) { $text = array( 'text' => $data['title'],//TODO 标题 'left' => 130, 'top' => 1172, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件 'fontSize' => 22, //字号 'fontColor' => '255,255,255', //字体颜色 'angle' => 0, ); array_push($config['text'], $text); } else { $titleOne = array( 'text' => mb_substr($data['title'], 0, 8) . '…',//TODO 标题 'left' => 130, 'top' => 1172, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件 'fontSize' => 22, //字号 'fontColor' => '255,255,255', //字体颜色 'angle' => 0, ); /*$titleTwo = array( 'text' => mb_substr($data['title'], 12, 12),//TODO 标题 'left' => 60, 'top' => 1310, 'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf', //字体文件 'fontSize' => 40, //字号 'fontColor' => '255,255,255', //字体颜色 'angle' => 0, );*/ array_push($config['text'], $titleOne); // array_push($config['text'], $titleTwo); } return self::setSharePoster($config, $path); } }