|
@@ -11,6 +11,9 @@
|
|
|
namespace library\services;
|
|
|
|
|
|
use library\exceptions\UtilException;
|
|
|
+use library\services\upload\Upload as Upload;
|
|
|
+use think\facade\Config;
|
|
|
+use dh2y\qrcode\QRcode;
|
|
|
|
|
|
class UtilService {
|
|
|
|
|
@@ -174,4 +177,173 @@ class UtilService {
|
|
|
return $p;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * TODO 生成分享二维码图片
|
|
|
+ * @param array $config
|
|
|
+ * @param $path
|
|
|
+ * @return array|bool|string
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public static function setSharePoster($config = array(), $path)
|
|
|
+ {
|
|
|
+ $imageDefault = array(
|
|
|
+ 'left' => 0,
|
|
|
+ 'top' => 0,
|
|
|
+ 'right' => 0,
|
|
|
+ 'bottom' => 0,
|
|
|
+ 'width' => 100,
|
|
|
+ 'height' => 100,
|
|
|
+ 'opacity' => 100
|
|
|
+ );
|
|
|
+ $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);
|
|
|
+ }
|
|
|
+ $backgroundInfo = getimagesize($background);
|
|
|
+ $background = imagecreatefromstring(file_get_contents($background));
|
|
|
+ $backgroundWidth = $backgroundInfo[0]; //背景宽度
|
|
|
+ $backgroundHeight = $backgroundInfo[1]; //背景高度
|
|
|
+ $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
|
|
|
+ $color = imagecolorallocate($imageRes, 0, 0, 0);
|
|
|
+ imagefill($imageRes, 0, 0, $color);
|
|
|
+ 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);
|
|
|
+ $info = getimagesize($val['url']);
|
|
|
+ $function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
|
|
|
+ if ($val['stream']) {
|
|
|
+ $info = getimagesizefromstring($val['url']);
|
|
|
+ $function = 'imagecreatefromstring';
|
|
|
+ }
|
|
|
+ $res = $function($val['url']);
|
|
|
+ $resWidth = $info[0];
|
|
|
+ $resHeight = $info[1];
|
|
|
+ $canvas = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
+ imagefill($canvas, 0, 0, $color);
|
|
|
+ imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
+ $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'];
|
|
|
+ imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);//左,上,右,下,宽度,高度,透明度
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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 = UploadService::init();
|
|
|
+ $res = $upload->to($path)->validate()->stream($res, $key);
|
|
|
+ if ($res === false) {
|
|
|
+ return $upload->getError();
|
|
|
+ } else {
|
|
|
+ $info = $upload->getUploadInfo();
|
|
|
+ $info['image_type'] = $uploadType;
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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 = 2;
|
|
|
+ $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 {
|
|
|
+ $config = [
|
|
|
+ 'accessKey' => config('qiniu')['accessKeyId'],
|
|
|
+ 'secretKey' => config('qiniu')['accessKeySecret'],
|
|
|
+ 'uploadUrl' => 'imgs.boofly.cn',
|
|
|
+ 'storageName' => config('qiniu')['bucket'],
|
|
|
+ 'storageRegion' => 's3-cn-south-1.qiniucs.com',
|
|
|
+ ];
|
|
|
+ $upload = new Upload(2,$config);
|
|
|
+ $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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|