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(); } }