123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace service;
- use service\FileService;
- use app\routine\model\routine\RoutineCode;
- use Intervention\Image\AbstractFont;
- use Intervention\Image\ImageManagerStatic as ImageManager;
- class CanvasService
- {
- const FILELINK = 'uploads/';
- const BGIMG_PATH = 'public/';
- //背景图
- //const VOTEIMG = 'uploads/voteimg/voteimg.jpg';
- //字体
- //const BG_FONT = 'wap/first/zsff/font/fzcyjt.ttf';
- //const FONT = 'uploads/voteimg/simsunb.ttf';
- //const FONT_TWO = 'uploads/voteimg/fz-v4.0.ttf';
- //const BORDER = 'uploads/voteimg/border.png';
- protected static $canvas = null;
- protected static $image = null;
- protected static $backgroundWidth = 750;
- protected static $backgroundHeight = 1334;
- public static function CreatJpeg($file = '')
- {
- $file = $file ? $file : self::VOTEIMG;
- $imagesize = getimagesize($file);
- $type = image_type_to_extension($imagesize[2], true);
- switch ($type) {
- case '.png':
- $canvas = imagecreatefrompng($file);
- break;
- case '.jpeg':
- $canvas = imagecreatefromjpeg($file);
- break;
- case '.jpg':
- $canvas = imagecreatefromjpeg($file);
- break;
- case '.gif':
- $canvas = imagecreatefromgif($file);
- break;
- }
- return [$canvas, $imagesize];
- }
- public static function ReatetrueColor($w = 0, $h = 0)
- {
- return imagecreatetruecolor($w ? $w : self::$backgroundWidth, $h ? $h : self::$backgroundHeight);
- }
- public static function foundCode1($special, $url, $backgroundImg, $ext = 'poster_code_')
- {
- vendor('phpqrcode.phpqrcode');
- $bg_path = self::BGIMG_PATH;
- //底图尺寸大小
- $bg = ImageManager::make($bg_path . 'bgimg/bg.png')->resize(750, 950);
- $thumb = $backgroundImg;
- //海报尺寸大小
- if ($thumb) {
- $thumb = ImageManager::make($thumb)->resize(690, 590);
- //水印
- $font_path = ROOT_PATH.'public/wap/first/zsff/font/';
- $title1_font_ttf = $font_path.'PingFang-SC.ttf';
- $site_name=SystemConfigService::get('site_name');
- $thumb->text($site_name, 600, 570, function (AbstractFont $text) use ($title1_font_ttf){
- $text->file($title1_font_ttf);
- $text->size(20);
- $text->color('#DDDDDD');
- $text->align('center');
- });
- $bg->insert($thumb, 'top-left', 30, 35);
- }
- $qrcodename = self::FILELINK . time() . 'qrcode.png';
- \QRcode::png($url, $qrcodename, 'L', 10, 2);
- //二维码位置
- if ($qrcodename) {
- $qr = ImageManager::make($qrcodename)->resize(150, 150);
- $bg->insert($qr, 'top-right', 45, 760);
- }
- //海报文字位置,活动分享标题
- $font_path = ROOT_PATH.'public/wap/first/zsff/font/';
- $title1_font_ttf = $font_path.'PingFang-SC.ttf';
- $bg->text($special['title'], 50, 750, function (AbstractFont $text) use($title1_font_ttf){
- $text->file($title1_font_ttf);
- $text->size(14);
- $text->color('#333333');
- $text->align('left');
- });
- $bg->text($special['title'], 50, 750, function (AbstractFont $text) use($title1_font_ttf){
- $text->file($title1_font_ttf);
- $text->size(14);
- $text->color('#333333');
- $text->align('left');
- });
- $bg->text($special['fake_sales'] . '人已学习', 625, 750, function (AbstractFont $text) use ($title1_font_ttf) {
- $text->file($title1_font_ttf);
- $text->size(14);
- $text->color('#666');
- $text->align('center');
- });
- $title2_font_ttf = $font_path.'pingfang-sc-blod.ttf';
- $bg->text(date("d"), 49, 869, function (AbstractFont $text) use($title2_font_ttf){
- $text->file($title2_font_ttf);
- $text->size(72);
- $text->color('#f30606');
- $text->align('left');
- });
- $month = array(
- '01' => '一月',
- '02' => '二月',
- '03' => '三月',
- '04' => '四月',
- '05' => '五月',
- '06' => '六月',
- '07' => '七月',
- '08' => '八月',
- '09' => '九月',
- '10' => '十月',
- '11' => '十一月',
- '12' => '十二月'
- );
- $title3_font_ttf = $font_path.'pingfang-sc-blod.ttf';
- $bg->text($month[date("m")], 128, 869, function (AbstractFont $text) use($title3_font_ttf){
- $text->file($title3_font_ttf);
- $text->size(21);
- $text->color('#000000');
- $text->align('left');
- });
- $week = date("w");
- if ($week == 0) {
- $week = 7;
- }
- $week_path = $bg_path . "bgimg/" . $week . '.png';
- $qr = ImageManager::make($week_path)->resize(114, 27);
- $bg->insert($qr, 'top-left', 53, 876);
- $filename = self::FILELINK . $ext . $special['id'] . ".png";
- $bg->save($filename);
- return $filename;
- }
- /**签到海报
- * @param $special_id
- * @param $url
- * @param $backgroundImg
- * @param string $ext
- * @return string
- */
- public static function foundSignCode($uid, $url, $sign_info, $ext = 'poster_sign_')
- {
- vendor('phpqrcode.phpqrcode');
- $bg_path = self::BGIMG_PATH;
- //底图尺寸大小
- $bg = ImageManager::make($bg_path . 'bgimg/bg.png')->resize(750, 950);
- $thumb = $sign_info['poster'];
- //海报尺寸大小
- if ($thumb) {
- $thumb = ImageManager::make($thumb)->resize(690, 590);
- //水印
- $font_path = ROOT_PATH.'public/wap/first/zsff/font/';
- $title1_font_ttf = $font_path.'PingFang-SC.ttf';
- $site_name=SystemConfigService::get('site_name');
- $thumb->text($site_name, 600, 570, function (AbstractFont $text) use ($title1_font_ttf){
- $text->file($title1_font_ttf);
- $text->size(20);
- $text->color('#DDDDDD');
- $text->align('center');
- });
- $bg->insert($thumb, 'top-left', 30, 35);
- }
- $qrcodename = self::FILELINK .$ext.time() . 'qrcode.png';
- \QRcode::png($url, $qrcodename, 'L', 10, 2);
- //二维码位置
- if ($qrcodename) {
- $qr = ImageManager::make($qrcodename)->resize(150, 150);
- $bg->insert($qr, 'top-right', 45, 760);
- }
- //海报文字位置,活动分享标题
- $font_path = ROOT_PATH.'public/wap/first/zsff/font/';
- $title1_font_ttf = $font_path.'PingFang-SC.ttf';
- $bg->text($sign_info['sign_talk'], 50, 750, function (AbstractFont $text) use($title1_font_ttf){
- $text->file($title1_font_ttf);
- $text->size(14);
- $text->color('#333333');
- $text->align('left');
- });
- $bg->text('我们一起风雨兼程', 625, 750, function (AbstractFont $text) use ($title1_font_ttf) {
- $text->file($title1_font_ttf);
- $text->size(14);
- $text->color('#666');
- $text->align('center');
- });
- $title2_font_ttf = $font_path.'pingfang-sc-blod.ttf';
- $bg->text(date("d"), 49, 869, function (AbstractFont $text) use($title2_font_ttf){
- $text->file($title2_font_ttf);
- $text->size(72);
- $text->color('#f30606');
- $text->align('left');
- });
- $month = array(
- '01' => '一月',
- '02' => '二月',
- '03' => '三月',
- '04' => '四月',
- '05' => '五月',
- '06' => '六月',
- '07' => '七月',
- '08' => '八月',
- '09' => '九月',
- '10' => '十月',
- '11' => '十一月',
- '12' => '十二月'
- );
- $title3_font_ttf = $font_path.'pingfang-sc-blod.ttf';
- $bg->text($month[date("m")], 128, 869, function (AbstractFont $text) use($title3_font_ttf){
- $text->file($title3_font_ttf);
- $text->size(21);
- $text->color('#000000');
- $text->align('left');
- });
- $week = date("w");
- if ($week == 0) {
- $week = 7;
- }
- $week_path = $bg_path . "bgimg/" . $week . '.png';
- $qr = ImageManager::make($week_path)->resize(114, 27);
- $bg->insert($qr, 'top-left', 53, 876);
- $filename = self::FILELINK . $ext .$uid . ".png";
- $bg->save($filename);
- return $filename;
- }
- public static function startPosterSpeclialIng($special_id, $backgroundImg, $url)
- {
- vendor('phpqrcode.phpqrcode');
- $qrcodename = time() . '_show_qrcode.png';
- \QRcode::png($url, $qrcodename, 'L', 10, 2);
- $image = self::ReatetrueColor();
- //放背景
- list($canvas, $borderRes) = self::CreatJpeg($backgroundImg);
- imagecopyresampled($image, $canvas, 0, 0, 0, 0, imagesx($canvas), imagesy($canvas), imagesx($canvas), imagesy($canvas));
- list($code, $codeRes) = self::CreatJpeg($qrcodename);
- imagecopyresampled($image, $code, 505, 1090, 0, 0, 156, 156, (int)$codeRes[0], (int)$codeRes[1]);
- $FileService = new FileService();
- $FileService->create_dir(self::FILELINK);
- $filename = self::FILELINK . 'poster_' . $special_id . '.jpg';
- imagejpeg($image, $filename, 70);
- imagedestroy($image);
- $FileService->unlink_file($qrcodename);
- return $filename;
- }
- public static function startPosterIng($backgroundImgList, $uid)
- {
- if (!is_array($backgroundImgList)) return false;
- //放二维码
- $path = 'public/uploads/routine/' . $uid . '.jpg';
- if (!file_exists($path)) file_put_contents($path, RoutineCode::getCode($uid));
- $link = [];
- foreach ($backgroundImgList as $item) {
- if ($item['pic']) {
- $image = self::ReatetrueColor();
- //放背景
- list($canvas, $borderRes) = self::CreatJpeg(ROOT_PATH . $item['pic']);
- $color = imagecolorallocate($canvas, 0, 0, 0);
- imagefill($canvas, 0, 0, $color);
- imagecopyresampled($image, $canvas, 0, 0, 0, 0, imagesx($canvas), imagesy($canvas), imagesx($canvas), imagesy($canvas));
- list($code, $codeRes) = self::CreatJpeg($path);
- imagecopyresampled($image, $code, 170, 545, 0, 0, 220, 220, (int)$codeRes[0], (int)$codeRes[1]);
- $res = pathinfo($item['pic']);
- $save_file = self::FILELINK . $res['filename'] . 'haibao_.jpg';
- imagejpeg($image, $save_file, 70);
- imagedestroy($image);
- $link[] = $save_file;
- }
- }
- return $link;
- }
- public static function startPainting($userinfo = [], $merinfo = [], $mer_id)
- {
- $image = self::ReatetrueColor();
- //放背景
- list($canvas, $res) = self::CreatJpeg();
- $color = imagecolorallocate($canvas, 0, 0, 0);
- imagefill($canvas, 0, 0, $color);
- imagecopyresampled($image, $canvas, 0, 0, 0, 0, imagesx($canvas), imagesy($canvas), imagesx($canvas), imagesy($canvas));
- //放边框
- list($border, $borderRes) = self::CreatJpeg(self::BORDER);
- imagecopyresampled($image, $border, 10, 380, 0, 0, 450, 483, (int)$borderRes[0], (int)$borderRes[1]);
- //放二维码
- $path = 'public/uploads/routine/' . $userinfo['uid'] . '.jpg';
- if (!file_exists($path)) file_put_contents($path, RoutineCode::getCode($userinfo['uid']));
- list($code, $codeRes) = self::CreatJpeg($path);
- imagecopyresampled($image, $code, 150, 950, 0, 0, 180, 180, (int)$codeRes[0], (int)$codeRes[1]);
- //放头部
- list($heade, $headeRes) = self::CreatJpeg(self::FILELINK . 'heade.png');
- imagecopyresampled($image, $heade, 10, 50, 0, 0, 446, 281, (int)$headeRes[0], (int)$headeRes[1]);
- //放头像
- //下载头像为jpg头像
- if (!$userinfo['avatar_name'] && !file_exists($userinfo['avatar_name'])) {
- $avatar_name = time() . '.jpg';
- FileService::down_remote_file($userinfo['avatar'], self::FILELINK, $avatar_name);
- $link_image = self::FILELINK . 'user' . time() . '.png';
- self::CutChart(self::FILELINK . $avatar_name, $link_image);
- //删除刚下载的图片
- if (file_exists(self::FILELINK . $avatar_name)) unlink(self::FILELINK . $avatar_name);
- } else {
- $link_image = $userinfo['avatar_name'];
- }
- //放置头像
- list($avatar_c, $avatarRes) = self::CreatJpeg($link_image);
- imagecopyresampled($image, $avatar_c, 85, 58, 0, 0, 95, 95, (int)$avatarRes[0], (int)$avatarRes[1]);
- $text = [
- [
- 'fontSize' => 20,
- 'fontColor' => '231,180,52',
- 'left' => 70,
- 'top' => 200,
- 'text' => self::getUtf8Str($userinfo['nickname']),
- 'fontPath' => self::FONT,
- 'angle' => 0,
- ],
- [
- 'fontSize' => 19,
- 'fontColor' => '255,255,255',
- 'left' => 70,
- 'top' => 235,
- 'text' => self::getUtf8Str('吃货值:' . $userinfo['integral']),
- 'fontPath' => self::FONT,
- 'angle' => 0,
- ],
- [
- 'fontSize' => 19,
- 'fontColor' => '255,255,255',
- 'left' => 70,
- 'top' => 265,
- 'text' => self::getUtf8Str('吃货头衔:' . $userinfo['grade_name']),
- 'fontPath' => self::FONT,
- 'angle' => 0,
- ],
- [
- 'fontSize' => 19,
- 'fontColor' => '255,255,255',
- 'left' => 70,
- 'top' => 295,
- 'text' => self::getUtf8Str('吃货折扣:' . $userinfo['discount_num'] . '折'),
- 'fontPath' => self::FONT,
- 'angle' => 0,
- ],
- [
- 'fontSize' => 20,
- 'fontColor' => '255,255,255',
- 'left' => 20,
- 'top' => 800,
- 'text' => self::getUtf8Str($userinfo['mer_name']),
- 'fontPath' => self::FONT,
- 'angle' => 0,
- ],
- [
- 'fontSize' => 15,
- 'fontColor' => '255,255,255',
- 'left' => 20,
- 'top' => 838,
- 'text' => self::getUtf8Str($userinfo['details_address']),
- 'fontPath' => self::FONT,
- 'angle' => 0,
- ],
- [
- 'fontSize' => 15,
- 'fontColor' => '255,255,255',
- 'left' => 159,
- 'top' => 900,
- 'text' => self::getUtf8Str('(扫码获取吃货值)'),
- 'fontPath' => self::FONT,
- 'angle' => 0,
- ]
- ];
- $images = [
- [
- 'url' => isset($merinfo[0]) ? $merinfo[0] : '',
- 'left' => 15,
- 'top' => 390,
- 'right' => 0,
- 'stream' => 0,
- 'bottom' => 0,
- 'width' => 218,
- 'height' => 155,
- 'opacity' => 100,
- ],
- [
- 'url' => isset($merinfo[1]) ? $merinfo[1] : '',
- 'left' => 235,
- 'top' => 390,
- 'right' => 0,
- 'stream' => 0,
- 'bottom' => 0,
- 'width' => 218,
- 'height' => 155,
- 'opacity' => 100,
- ],
- [
- 'url' => isset($merinfo[2]) ? $merinfo[2] : '',
- 'left' => 15,
- 'top' => 548,
- 'right' => 0,
- 'stream' => 0,
- 'bottom' => 0,
- 'width' => 218,
- 'height' => 155,
- 'opacity' => 100,
- ],
- [
- 'url' => isset($merinfo[3]) ? $merinfo[3] : '',
- 'left' => 235,
- 'top' => 548,
- 'right' => 0,
- 'bottom' => 0,
- 'width' => 218,
- 'height' => 155,
- 'opacity' => 100,
- ]
- ];
- foreach ($images as $item) {
- if ($item['url']) {
- list($mer, $Res) = self::CreatJpeg(ROOT_PATH . $item['url']);
- if ($mer && $Res) imagecopyresampled($image, $mer, $item['left'], $item['top'], $item['right'], $item['bottom'], $item['width'], $item['height'], (int)$Res[0], (int)$Res[1]);
- }
- }
- foreach ($text as $key => $val) {
- list($R, $G, $B) = explode(',', $val['fontColor']);
- $fontColor = imagecolorallocate($image, $R, $G, $B);
- $val['left'] = $val['left'] < 0 ? self::$backgroundWidth - abs($val['left']) : $val['left'];
- $val['top'] = $val['top'] < 0 ? self::$backgroundHeight - abs($val['top']) : $val['top'];
- imagettftext($image, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
- }
- $save_file = self::FILELINK . 'haibao_' . time() . '.jpg';
- imagejpeg($image, $save_file, 70);
- imagedestroy($image);
- return ['file' => $save_file, 'avatar_name' => $link_image];
- }
- public static function getUtf8Str($str, $utf8len = 28, $chaet = 'UTF-8', $file = '...')
- {
- if (mb_strlen($str, $chaet) > $utf8len) {
- $str = mb_substr($str, 0, $utf8len, $chaet) . $file;
- }
- return $str;
- }
- public static function CutChart($imgpath, $savefilename)
- {
- $ext = pathinfo($imgpath);
- $src_img = null;
- switch ($ext['extension']) {
- case 'jpg':
- $src_img = imagecreatefromjpeg($imgpath);
- break;
- case 'png':
- $src_img = imagecreatefrompng($imgpath);
- break;
- case 'gif':
- $src_img = imagecreatefromgif($imgpath);
- break;
- }
- $wh = getimagesize($imgpath);
- $w = $wh[0];
- $h = $wh[1];
- $w = min($w, $h);
- $h = $w;
- $img = imagecreatetruecolor($w, $h);
- //这一句一定要有
- imagesavealpha($img, true);
- //拾取一个完全透明的颜色,最后一个参数127为全透明
- $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
- imagefill($img, 0, 0, $bg);
- $r = $w / 2; //圆半径
- $y_x = $r; //圆心X坐标
- $y_y = $r; //圆心Y坐标
- for ($x = 0; $x < $w; $x++) {
- for ($y = 0; $y < $h; $y++) {
- $rgbColor = imagecolorat($src_img, $x, $y);
- if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
- imagesetpixel($img, $x, $y, $rgbColor);
- }
- }
- }
- imagepng($img, $savefilename, 0);
- imagedestroy($img);
- }
- /*
- * 生成画笔
- * */
- public static function brush($one, $two, $three)
- {
- if (self::$canvas === null) return false;
- return imagecolorallocate(self::$canvas, $one, $two, $three);
- }
- /*
- * 粉丝介绍字体拆分为数组排列
- * */
- public static function getContentArray($str, $len = 4, $strlen = 24, $chaet = 'UTF-8')
- {
- $array = [];
- for ($i = 0; $i < $len; $i++) {
- array_push($array, '');
- }
- if (mb_strlen($str, $chaet) > $strlen) {
- for ($i = 0; $i < $len; $i++) {
- $lin = mb_substr($str, $i * $strlen, $strlen, $chaet);
- if ($lin) $array[$i] = $lin;
- }
- } else {
- $array[0] = $str;
- }
- return $array;
- }
- }
|