首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
hyperf框架生成二维码海报图片时,如何同步阻塞执行代码?
### 问题描述 PHP Notice: getimagesize(): Read error! PHP Warning: imagecopy() expects parameter 2 to be resource, boolean given 这些错误的原因是没有生成二维码图片之前,就异步执行了海报绘制图片,所以当获取图片信息时就会出现图片信息并不存在的问题,请问该如何同步阻塞执行代码呢? ### Swoole版本,PHP版本,以及操作系统版本信息 swoole 4.5 php 7.2 ### 相关代码 /** * 获取用户邀请码 * @param $uid * @throws \Exception */ public function invite($uid) { //access——token $access_token = $this->WechatAppletService->access_token; $scene = ['scene'=>'uid:'.$uid]; $json_data = json_encode($scene); //获取微信返回的带参数二维码 $images = $this->WechatAppletService->_getQRCode($access_token, $json_data); //先保存到本地 $file_path = '/usr/local/nginx/html/tp_car/hyperf-skeleton/imgruntime/invite_runtime_'.$uid.'.png'; $f = fopen($file_path,'w'); fwrite($f,$images); fclose($f); //二维码海报保存路径 $save_path = '/usr/local/nginx/html/tp_car/hyperf-skeleton/imgruntime/tp_car_invite_'.$uid.'.jpg'; $db = ApplicationContext::getContainer()->get(Db::class); //查询用户头像地址 $avatar = $db->table('users')->where('id','=',$uid)->value('avatar'); $avatar = str_replace('https','http',$avatar); //绘制图片 //海报模板 $img1 = imagecreatefromjpeg('/usr/local/nginx/html/tp_car/hyperf-skeleton/share.jpg'); //绘制二维码 //imageResize()等比例处理图片 $newQrcode = $this->imageResize($file_path, 418, 418); //绘制到海报上 imagecopy($img1, $newQrcode, 169, 188, 0, 0, 418, 418); //绘制用户头像 //imageResize()等比例处理图片 $newUserImg = $this->imageResize($avatar,88); //绘制到海报上 imagecopy($img1, $newUserImg, 344, 355, 0, 0, 72,88); //生成新图片 imagejpeg($img1,$save_path); //七牛 $accessKey = config('qiniu.accessKey'); $secretKey = config('qiniu.secretKey'); $auth = new Auth($accessKey, $secretKey); $bucket = config('qiniu.bucket'); // 生成上传Token //覆盖上传 $keyToOverwrite = 'tp_carinvite'.$uid.'.jpg'; $token = $auth->uploadToken($bucket, $keyToOverwrite); // 构建 UploadManager 对象 $uploadMgr = new UploadManager(); //上传七牛 $uploadMgr->putFile($token,$keyToOverwrite,$save_path); } private function imageResize($path,$maxWidth,$maxHeight=0,$balance=1) { //1.获取被缩放图片的信息 $info = getimagesize($path); $width = $info[0]; $height = $info[1]; //2.根据传入的图片类型来创建对应的画布资源 switch($info[2]){ case 1://gif $srcim = imagecreatefromgif($path); break; case 2://jpeg $srcim = imagecreatefromjpeg($path); break; case 3://png $srcim = imagecreatefrompng($path); break; default: return false;//返回假 break; } //3.计算缩放后的尺寸 if($balance) { if(!$maxHeight){ //按宽等比例 $w = $maxWidth; $h = ($maxWidth/$width)*$height; }else{ //按高等比例 $w = ($maxHeight/$height)*$width; $h = $maxHeight; } } else { //非等比例 $w = $maxWidth; $h = $maxHeight; } //4.创建目标画布 $dstim = imagecreatetruecolor($w,$h); //5.开始绘画 imagecopyresampled($dstim,$srcim,0,0,0,0,$w,$h,$width,$height); //6.销毁资源 imagedestroy($srcim); //7、返回结果 return $dstim; }
发布于6年前 · 22 次浏览 · 来自
提问
弘扬正气
### 问题描述 PHP Notice: getimagesize(): Read error! PHP Warning: imagecopy() expects parameter 2 to be resource, boolean given 这些错误的原因是没有生成二维码图片之前,就异步执行了海报绘制图片,所以当获取图片信息时就会出现图片信息并不存在的问题,请问该如何同步阻塞执行代码呢? ### Swoole版本,PHP版本,以及操作系统版本信息 swoole 4.5 php 7.2 ### 相关代码 /** * 获取用户邀请码 * @param $uid * @throws \Exception */ public function invite($uid) { //access——token $access_token = $this->WechatAppletService->access_token; $scene = ['scene'=>'uid:'.$uid]; $json_data = json_encode($scene); //获取微信返回的带参数二维码 $images = $this->WechatAppletService->_getQRCode($access_token, $json_data); //先保存到本地 $file_path = '/usr/local/nginx/html/tp_car/hyperf-skeleton/imgruntime/invite_runtime_'.$uid.'.png'; $f = fopen($file_path,'w'); fwrite($f,$images); fclose($f); //二维码海报保存路径 $save_path = '/usr/local/nginx/html/tp_car/hyperf-skeleton/imgruntime/tp_car_invite_'.$uid.'.jpg'; $db = ApplicationContext::getContainer()->get(Db::class); //查询用户头像地址 $avatar = $db->table('users')->where('id','=',$uid)->value('avatar'); $avatar = str_replace('https','http',$avatar); //绘制图片 //海报模板 $img1 = imagecreatefromjpeg('/usr/local/nginx/html/tp_car/hyperf-skeleton/share.jpg'); //绘制二维码 //imageResize()等比例处理图片 $newQrcode = $this->imageResize($file_path, 418, 418); //绘制到海报上 imagecopy($img1, $newQrcode, 169, 188, 0, 0, 418, 418); //绘制用户头像 //imageResize()等比例处理图片 $newUserImg = $this->imageResize($avatar,88); //绘制到海报上 imagecopy($img1, $newUserImg, 344, 355, 0, 0, 72,88); //生成新图片 imagejpeg($img1,$save_path); //七牛 $accessKey = config('qiniu.accessKey'); $secretKey = config('qiniu.secretKey'); $auth = new Auth($accessKey, $secretKey); $bucket = config('qiniu.bucket'); // 生成上传Token //覆盖上传 $keyToOverwrite = 'tp_carinvite'.$uid.'.jpg'; $token = $auth->uploadToken($bucket, $keyToOverwrite); // 构建 UploadManager 对象 $uploadMgr = new UploadManager(); //上传七牛 $uploadMgr->putFile($token,$keyToOverwrite,$save_path); } private function imageResize($path,$maxWidth,$maxHeight=0,$balance=1) { //1.获取被缩放图片的信息 $info = getimagesize($path); $width = $info[0]; $height = $info[1]; //2.根据传入的图片类型来创建对应的画布资源 switch($info[2]){ case 1://gif $srcim = imagecreatefromgif($path); break; case 2://jpeg $srcim = imagecreatefromjpeg($path); break; case 3://png $srcim = imagecreatefrompng($path); break; default: return false;//返回假 break; } //3.计算缩放后的尺寸 if($balance) { if(!$maxHeight){ //按宽等比例 $w = $maxWidth; $h = ($maxWidth/$width)*$height; }else{ //按高等比例 $w = ($maxHeight/$height)*$width; $h = $maxHeight; } } else { //非等比例 $w = $maxWidth; $h = $maxHeight; } //4.创建目标画布 $dstim = imagecreatetruecolor($w,$h); //5.开始绘画 imagecopyresampled($dstim,$srcim,0,0,0,0,$w,$h,$width,$height); //6.销毁资源 imagedestroy($srcim); //7、返回结果 return $dstim; }
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2020-05-09
鲁飞
很明显你自己逻辑有问题吧。
赞
0
回复
2020-05-09
弘扬正气
回复
鲁飞
找不到逻辑所在点,望指教
赞
0
回复
2020-05-09
鲁飞
回复
弘扬正气
没看懂你那里异步生成了,invite中调用的imageResize,报错在imageResize中...
赞
0
回复