123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace app\common\utility;
- use app\common\library\Redis;
- use think\Config;
- use Endroid\QrCode\QrCode;
- use EasyWeChat\Factory;
- use Symfony\Component\Cache\Simple\RedisCache;
- /**
- * 自定义永久二维码
- * Class DiyQRCode
- * @package app\common\utility
- */
- class DiyQRCode{
- /**
- * 二维码模板信息
- * @var array
- */
- private static $tpl_size = [
- 1 => [
- 'size' => 164, //二维码宽度
- 'x' => 376, //水印x
- 'y' => 35 //水印y
- ],
- 2 => [
- 'size' => 168, //二维码宽度
- 'x' => 374, //水印x
- 'y' => 33 //水印y
- ],
- 3 => [
- 'size' => 156, //二维码宽度
- 'x' => 372, //水印x
- 'y' => 46 //水印y
- ],
- 4 => [
- 'size' => 200, //二维码宽度
- 'x' => 356, //水印x
- 'y' => 15 //水印y
- ],
- 5 => [
- 'size' => 200, //二维码宽度
- 'x' => 356, //水印x
- 'y' => 15 //水印y
- ],
- 6 => [
- 'size' => 200, //二维码宽度
- 'x' => 356, //水印x
- 'y' => 15 //水印y
- ]
- ];
- /**
- * 跟进自定义模板创建二维码
- * @param $url
- * @param $tpl_id
- * @return string
- * @throws \Exception
- */
- public static function createTplQRCode($url,$tpl_id){
- $qrcode_save_path = ROOT_PATH.'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'qrcode'.DIRECTORY_SEPARATOR;
- $qrcode_tpl_path = ROOT_PATH . 'public'.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'img'.DIRECTORY_SEPARATOR.'essay'.DIRECTORY_SEPARATOR.'qrcode_%s.png';
- if(!$tpl_id || !array_key_exists($tpl_id,self::$tpl_size)){
- throw new \Exception('获取模板ID失败');
- }
- $tpl_img = imagecreatefrompng(sprintf($qrcode_tpl_path,$tpl_id)); //底图
- $new_img = self::createThumbnail($url, self::$tpl_size[$tpl_id]['size'], self::$tpl_size[$tpl_id]['size']);
- imagecopymerge($tpl_img,$new_img, self::$tpl_size[$tpl_id]['x'], self::$tpl_size[$tpl_id]['y'], 0, 0, imagesx($new_img), imagesy($new_img), 100);
- if (!file_exists($qrcode_save_path)) { //创建文件夹
- mkdir($qrcode_save_path, 0700, true);
- clearstatcache();
- }
- $save_path = $qrcode_save_path .md5($url.$tpl_id).'.png';
- if(!file_exists($save_path)){
- if(!$res = imagepng($tpl_img, $save_path)){
- throw new \Exception('创建自定义二维码出错');
- }
- }
- imagedestroy($tpl_img);
- imagedestroy($new_img);
- $cdn_url = cdnurl("/uploads/qrcode/" .md5($url.$tpl_id).'.png');
- return $cdn_url;
- }
- /**
- * 创建纯二维码
- * @param $url
- * @param int $size
- * @return string
- */
- public static function createQRCodeByUrl($url,$size = 430){
- $qrcode_save_path = ROOT_PATH.'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'qrcode'.DIRECTORY_SEPARATOR;
- $save_common_path = 'common'.DIRECTORY_SEPARATOR;
- $qrcode_path = $qrcode_save_path.$save_common_path.md5($url).'.png';
- if(!file_exists($qrcode_path)){
- $qrCode = new QrCode($url);
- $qrCode->setSize($size); //二维码尺寸
- $qrCode->setWriterByName('png');
- $qrCode->setEncoding('UTF-8');
- $qrCode->setMargin(10);
- if (!file_exists($qrcode_save_path.$save_common_path)) { //创建文件夹
- mkdir($qrcode_save_path.$save_common_path, 0700, true);
- clearstatcache();
- }
- $qrCode->writeFile($qrcode_path);
- }
- $cdn_url = cdnurl("/uploads/qrcode/" .$save_common_path.md5($url).'.png');
- return $cdn_url;
- }
- /**
- * 获取自定义二维码路径
- * @param $url
- * @param null $tpl_id
- * @return array|string|null
- */
- public static function getTplPath($url,$tpl_id = null){
- $qrcode_save_path = ROOT_PATH.'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'qrcode'.DIRECTORY_SEPARATOR;
- if(empty($url)){
- return null;
- }
- if($tpl_id){
- if(file_exists($qrcode_save_path .md5($url.$tpl_id).'.png')){
- return cdnurl("/uploads/qrcode/" .md5($url.$tpl_id).'.png');
- }
- }else{
- $result = [];
- $tpl_ids = array_keys(self::$tpl_size);
- foreach($tpl_ids as $id){
- if(file_exists($qrcode_save_path .md5($url.$id).'.png')){
- $result[$id] = cdnurl("/uploads/qrcode/" .md5($url.$id).'.png');
- }
- }
- return $result;
- }
- }
- /**
- * 删除自定义二维码
- * @param $url
- * @param $tpl_id
- * @return bool
- */
- public static function unlinkQRCode($url,$tpl_id = null){
- $qrcode_save_path = ROOT_PATH.'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'qrcode'.DIRECTORY_SEPARATOR;
- if(empty($url)){
- return true;
- }
- //删除二维码模板
- if($tpl_id !== null){
- if(file_exists($qrcode_save_path .md5($url.$tpl_id).'.png')){
- @unlink($qrcode_save_path .md5($url.$tpl_id).'.png');
- }
- }else{
- $tpl_ids = array_keys(self::$tpl_size);
- foreach($tpl_ids as $id){
- if(file_exists($qrcode_save_path .md5($url.$id).'.png')){
- @unlink($qrcode_save_path .md5($url.$id).'.png');
- }
- }
- }
- //删除自定义二维码
- if(file_exists($qrcode_save_path.'common'.DIRECTORY_SEPARATOR.md5($url).'png')){
- @unlink($qrcode_save_path.'common'.DIRECTORY_SEPARATOR.md5($url).'png');
- }
- return true;
- }
- /**
- * 生成保持原图纵横比的缩略图,支持.png .jpg .gif
- * 缩略图类型统一为.png格式
- * @param $srcFile 原图像文件名称
- * @param $toW 缩略图宽
- * @param $toH 缩略图高
- * @return bool
- * @throws
- */
- private static function createThumbnail($srcFile, $toW, $toH){
- $info = "";
- //返回含有4个单元的数组,0-宽,1-高,2-图像类型,3-宽高的文本描述。
- //失败返回false并产生警告。
- if(strpos($srcFile,'http') !== 0){
- $srcFile = ROOT_PATH.'public'.$srcFile;
- }
- $data = getimagesize($srcFile, $info);
- if (!$data)
- return false;
- //将文件载入到资源变量im中
- switch ($data[2]) //1-GIF,2-JPG,3-PNG
- {
- case 1:
- if(!function_exists("imagecreatefromgif")) {
- throw new \Exception('the GD can\'t support .gif, please use .jpeg or .png!');
- }
- $im = imagecreatefromgif($srcFile);
- break;
- case 2:
- if(!function_exists("imagecreatefromjpeg")) {
- throw new \Exception('the GD can\'t support .jpeg, please use other picture!');
- }
- $im = imagecreatefromjpeg($srcFile);
- break;
- case 3:
- $im = imagecreatefrompng($srcFile);
- break;
- }
- //计算缩略图的宽高
- $srcW = imagesx($im);
- $srcH = imagesy($im);
- $toWH = $toW / $toH;
- $srcWH = $srcW / $srcH;
- if ($toWH <= $srcWH) {
- $ftoW = $toW;
- $ftoH = (int)($ftoW * ($srcH / $srcW));
- }else{
- $ftoH = $toH;
- $ftoW = (int)($ftoH * ($srcW / $srcH));
- }
- if (function_exists("imagecreatetruecolor")) {
- $ni = imagecreatetruecolor($ftoW, $ftoH); //新建一个真彩色图像
- if ($ni){
- //重采样拷贝部分图像并调整大小 可保持较好的清晰度
- imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
- }else{
- //拷贝部分图像并调整大小
- $ni = imagecreate($ftoW, $ftoH);
- imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
- }
- }else{
- $ni = imagecreate($ftoW, $ftoH);
- imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
- }
- ImageDestroy($im);
- return $ni;
- }
- }
|