123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/1/22
- * Time: 下午8:27
- */
- namespace app\common\service;
- use app\common\constants\AppConstants;
- use app\common\constants\ErrorCodeConstants;
- use app\common\helper\ArrayHelper;
- use app\common\library\Redis;
- use Endroid\QrCode\QrCode;
- use think\Config;
- class ImageService extends BaseService
- {
- /**
- * @var ImageService
- */
- private static $self;
- /**
- * @return $this|ImageService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * 获取二维码
- * @param $url
- * @param $width
- * @param int $margin
- * @param string $path_dir
- * @return \app\common\model\object\ReturnObject
- */
- public function getQrImage($url, $width = '164', $margin = 10, $path_dir = '')
- {
- try {
- $qrCode = new QrCode($url);
- $qrCode->setSize($width); //二维码尺寸
- $qrCode->setWriterByName('png');
- $qrCode->setEncoding('UTF-8');
- $qrCode->setMargin($margin);
- $tmpFile = AppConstants::getTmpFilePath($path_dir);
- $qrCode->writeFile($tmpFile);
- return $this->setData($tmpFile)->getReturn();
- } catch (\Exception $e) {
- return $this->setCode(ErrorCodeConstants::EXCEPTION)->setMsg($e->getMessage())->getReturn();
- }
- }
- /**
- * @param $width
- * @param $height
- * @return \app\common\model\object\ReturnObject
- */
- public function getBackGroundImage($width, $height)
- {
- try {
- $image = imagecreatetruecolor($width, $height);
- $background = imagecolorallocate($image, 255, 255, 255);
- imagefill($image,0,0,$background);
- return $this->setData($image)->getReturn();
- } catch (\Exception $e) {
- return $this->setCode(ErrorCodeConstants::EXCEPTION)->setMsg($e->getMessage())->getReturn();
- }
- }
- public function setImageText($image, $content, $font_size = 5, $x = 0, $y = 0)
- {
- try {
- $color = imagecolorallocate($image, 255, 0, 0);
- $font_file = ROOT_PATH . 'public/assets/fonts/ht.ttf';
- imagettftext($image, $font_size, 0, $x, $y, $color, $font_file, $content);
- return $this->setData($image)->getReturn();
- } catch (\Exception $e) {
- return $this->setCode(ErrorCodeConstants::EXCEPTION)->setMsg($e->getMessage())->getReturn();
- }
- }
- /**
- * 获取文字行
- * @param $line
- * @return \app\common\model\object\ReturnObject
- */
- public function getLine($line)
- {
- $list = [];
- if (mb_strlen($line) > 16) {
- $list[] = mb_substr($line, 0, 16);
- $left = mb_substr($line, 16);
- if (mb_strlen($left) > 16) {
- $list[] = mb_substr($left, 0, 10) . "....";
- } else {
- $list[] = $left;
- }
- } else {
- $list[] = $line;
- }
- return $this->setData($list)->getReturn();
- }
- /**
- * 获取推广链接图
- * @param $id
- * @param $channel_id
- * @return \app\common\model\object\ReturnObject
- */
- public function getReferralImage($id, $channel_id)
- {
- $referral = ReferralService::instance()->getReferralModel()->where('id', '=', $id)->find();
- if (!$referral) {
- return $this->setCode(ErrorCodeConstants::PERMISSION_DENY)->getReturn();
- }
- $cache = 'QRIMAGE:' . $id;
- if ($path = Redis::instance()->get($cache)) {
- return $this->setData($path)->getReturn();
- }
- $jmp_url = getCurrentDomain($channel_id, '/t/' . $id);
- $mergeImage = ImageService::instance()->getMergeImage($jmp_url, $referral->getData("share_title"));
- if ($mergeImage->code == ErrorCodeConstants::SUCCESS) {
- Redis::instance()->set($cache, $mergeImage->data, 600);
- }
- return $mergeImage;
- }
- /**
- * 生成大图
- * @param $url
- * @param $content
- * @return \app\common\model\object\ReturnObject
- */
- public function getMergeImage($url, $content)
- {
- $returnQrcode = ImageService::instance()->getQrImage($url, 110, 0);
- $qrImage = imagecreatefrompng($returnQrcode->data);
- $cover = ResourceService::instance()->getRandomCover();
- $filename = $cover->data;
- $fileInfo = pathinfo($filename);
- $extension = ArrayHelper::arrayGet($fileInfo, 'extension');
- switch ($extension) {
- case 'png':
- $coverImage = imagecreatefrompng($filename);
- break;
- case 'jpg':
- case 'jpeg':
- $coverImage = imagecreatefromjpeg($filename);
- break;
- default:
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg("只支持JPEG,PNG图片")->getReturn();
- }
- list($width, $height) = getimagesize($filename);
- $return = ImageService::instance()->getBackGroundImage(500, 469);
- $bgImage = $return->data;
- imagecopyresampled($bgImage, $qrImage, 6, 346, 0, 0, 110, 110, 110, 110);
- $color = imagecolorallocate($bgImage, 255, 0, 0);
- $font_file = ROOT_PATH . 'public/assets/fonts/weiruanyahei.ttf';
- $lines = $this->getLine($content)->data;
- foreach ($lines as $index => $line) {
- $offsetY = 365 + $index * 30;
- imagettftext($bgImage, 16, 0, 131, $offsetY, $color, $font_file, $line);
- }
- $color = imagecolorallocate($bgImage, 121, 121, 121);
- imagettftext($bgImage, 13, 0, 131, 445, $color, $font_file, "长按识别二维码查看精彩内容");
- imagecopyresampled($bgImage, $coverImage, 0, 0, 0, 0, 500, 313, $width, $height);
- $uploadPath = AppConstants::getUploadQrPath();
- imagepng($bgImage, $uploadPath, 9);
- imagedestroy($bgImage);
- imagedestroy($qrImage);
- imagedestroy($coverImage);
- $url = AppConstants::getPublicUrl($uploadPath);
- return $this->setData($url)->getReturn();
- }
- }
|