123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace app\admin\command;
- use app\common\library\Redis;
- use app\common\library\WeChatObject;
- use Symfony\Component\Cache\Simple\RedisCache;
- use think\Config;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- use EasyWeChat\Factory;
- use think\Log;
- use think\Request;
- class ChangeQRCode extends Command{
- /**
- * @var array $imgSize 图片规格
- */
- private $imgSize = [
- 1 => [
- 'size' => 143, //二维码宽度
- 'x' => 376, //水印x
- 'y' => 35 //水印y
- ],
- 2 => [
- 'size' => 147, //二维码宽度
- 'x' => 374, //水印x
- 'y' => 33 //水印y
- ],
- 3 => [
- 'size' => 135, //二维码宽度
- 'x' => 372, //水印x
- 'y' => 46 //水印y
- ],
- 4 => [
- 'size' => 179, //二维码宽度
- 'x' => 356, //水印x
- 'y' => 15 //水印y
- ],
- 5 => [
- 'size' => 179, //二维码宽度
- 'x' => 356, //水印x
- 'y' => 15 //水印y
- ],
- 6 => [
- 'size' => 179, //二维码宽度
- 'x' => 356, //水印x
- 'y' => 15 //水印y
- ]
- ];
- protected function configure()
- {
- $this
- ->setName('changeQRCode')
- ->addOption('type', 't', Option::VALUE_OPTIONAL, '处理自定义二维码错误问题', 'modify')
- ->setDescription('处理自定义二维码错误问题');
- }
- protected function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
- $type = $input->getOption('type');
- switch ($type) {
- case 'modify':
- $this->modify($input, $output);
- break;
- default:
- $output->writeln("Type: {$type} 无法识别的类型");
- }
- }
- /**
- * @param Input $input
- * @param Output $output
- */
- private function modify(Input $input, Output $output){
- $output->info("开始处理自定义二维码错误问题.");
- try{
- if(!$source = model('CustomQrcode')->where(['type'=>'2'])->select()){
- $output->info('没有要处理的自定义二维码错误数据');
- }
- foreach($source as $key => $val){
- //处理合成图片
- if(strpos($val['url'], 'mp.weixin.qq.com') === false){
- $tmp = explode('_',basename($val['url'],".png"));
- $template_id = $tmp[2];
- if(!$adminConfig = model('AdminConfig')->where(['admin_id'=>$val['admin_id']])->find()){
- $output->error(sprintf('未处理 Channel_id:%d CustomQRCode_id:%d 渠道配置信息获取失败',$val['admin_id'],$val['id']));
- continue;
- }
- if(empty($adminConfig['appid']) || empty($adminConfig['json']) ){
- $output->error(sprintf('未处理 Channel_id:%d CustomQRCode_id:%d 渠道微信没有授权',$val['admin_id'],$val['id']));
- continue;
- }
- //获取微信永久二维码
- $adminConfig = model('AdminConfig')->getAdminInfoAll($val['admin_id']);
- $wechat = new WeChatObject($adminConfig);
- $officialAccount = $wechat->getOfficialAccount();
- $result = $officialAccount->qrcode->forever($val['index']);
- if(empty($result) || isset($result['errcode'])){
- $output->error(sprintf('未处理 Channel_id:%d CustomQRCode_id:%d 获取微信二维码失败',$val['admin_id'],$val['id']));
- continue;
- }
- $wx_url = $officialAccount->qrcode->url($result['ticket']);
- $image_1 = imagecreatefrompng(ROOT_PATH . "public/assets/img/essay/qrcode_{$template_id}.png"); //底图
- $image_2 = $this->createThumbnail($wx_url,$this->imgSize[$template_id]['size'],$this->imgSize[$template_id]['size']);
- imagecopymerge($image_1, $image_2, $this->imgSize[$template_id]['x'], $this->imgSize[$template_id]['y'], 0, 0, imagesx($image_2),
- imagesy($image_2), 100);
- $imgPath = ROOT_PATH . 'public/uploads/qrcode/'; //图片保存路径
- if (!file_exists($imgPath)) { //创建文件夹
- mkdir($imgPath, 0700, true);
- clearstatcache();
- }
- $imgPath = $imgPath .$val['book_id'].'_'.$val['chapter_id'] .'_' . $template_id .'_'.$val['admin_id'].'_'.time().'.png';
- $data['url'] = cdnurl("/uploads/qrcode/" . $val['book_id'] .'_'.$val['chapter_id'] . '_' . $template_id .'_'.$val['admin_id'].'_'.time().'.png');
- if(!imagepng($image_1, $imgPath)){
- $output->error(sprintf('未处理 Channel_id:%d CustomQRCode_id:%d 拼接图片失败',$val['admin_id'],$val['id']));
- continue;
- }
- imagedestroy($image_1);
- imagedestroy($image_2);
- if(false !== model('CustomQrcode')->where(['id'=>$val['id']])->update($data)){
- $output->info(sprintf('已处理 Channel_id:%d CustomQRCode_id:%d ImagePath:%s',$val['admin_id'],$val['id'],$imgPath));
- }else{
- $output->info(sprintf('未处理 Channel_id:%d CustomQRCode_id:%d 更新数据失败 ImagePath:%s',$val['admin_id'],$val['id'],$imgPath));
- }
- }
- }
- }catch (\Exception $e){
- $output->error('处理自定义二维码出错,Error:'.$e->getMessage());
- }
- }
- /**
- * 生成保持原图纵横比的缩略图,支持.png .jpg .gif
- * 缩略图类型统一为.png格式
- * $srcFile 原图像文件名称
- * $toW 缩略图宽
- * $toH 缩略图高
- * @return bool
- */
- private function createThumbnail($srcFile, $toW, $toH)
- {
- $toW += 20;
- $toH += 20;
- $info = "";
- //返回含有4个单元的数组,0-宽,1-高,2-图像类型,3-宽高的文本描述。
- //失败返回false并产生警告。
- $data = getimagesize($srcFile, $info);
- if (!$data)
- return false;
- //将文件载入到资源变量im中
- switch ($data[2]) //1-GIF,2-JPG,3-PNG
- {
- case 1:
- if(!function_exists("imagecreatefromgif"))
- {
- echo "the GD can't support .gif, please use .jpeg or .png! <a href='javascript:history.back();'>back</a>";
- exit();
- }
- $im = imagecreatefromgif($srcFile);
- break;
- case 2:
- if(!function_exists("imagecreatefromjpeg"))
- {
- echo "the GD can't support .jpeg, please use other picture! <a href='javascript:history.back();'>back</a>";
- exit();
- }
- $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;
- }
- }
|