love_php/app/Jobs/CreateViewerPoster.php

130 lines
4.6 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Jobs;
use App\Models\Live\Viewer;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Intervention\Image\ImageManagerStatic as Image;
use App\Http\Controllers\UploadController;
class CreateViewerPoster implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $viewer;
public $tries = 5;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Viewer $viewer)
{
$this->viewer = $viewer;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$path = config('app.env') == 'local' ? 'http://alpha.ufutx.net/api/official/live/wechat/oauth?from_id='.$this->viewer->id : env('APP_URL').'/api/official/live/wechat/oauth?from_id='.$this->viewer->id;
$qrcode = $this->getUrlqrcode($path, 140);
$pic = $this->assembly($this->viewer, $qrcode);
$arr = [
'qrcode' => $qrcode,
'share_poster' => $pic,
];
$arr = json_encode($arr);
Viewer::where('id', $this->viewer->id)->update(['share_poster' => $arr]);
}
public function assembly($viewer, $qrcode){
//原始海报
$poster = public_path()."/imgs/viewer_share_poster.png";
// $new_image = $this->imageAddImage($poster, $qrcode, 136, 136, 111, 240, 'bottom-left');
$new_image = $this->imageAddImage($poster, $qrcode, 167, 167, 40, 60, 'bottom-right');
// $new_image = $this->imageAddImage($poster, $viewer->avatar, 120, 120, 100, 330, 'top-right');
if ($viewer->avatar) {
$new_image = $this->imageAddImage($new_image, $viewer->avatar, 120, 120, 60, 200, 'top-right');
}
// $new_image = $this->textAddImage($new_image, $viewer->nickname,28, 116, 362, '#333333', 'left');
$new_image = $this->textAddImage($new_image, $viewer->nickname,28, 87, 239, '#FFFFFF', 'left');
// $new_image = $this->textAddImage($new_image, '邀请你一起来看直播',24, 116, 400, '#666666', 'left');
$time = time();
$file_path = storage_path()."/qrcode/".$time."viewer.png";
$new_image->save($file_path);
$pic = '';
if(file_exists($file_path)){
$pic = $this->uploadFile($file_path);
try{
unlink($file_path);
}catch(Exception $e) {
return $this->failure($e->getMessage());
exit();
}
}
return $pic;
}
public function textAddImage($image, $text, $text_size, $offset_x, $offset_y, $color='#000000', $align = 'center', $valign='top'){
Image::configure(['driver'=>'gd']);
$image = Image::make($image);
$image->text($text, $offset_x, $offset_y,function($font) use($text_size, $color, $align, $valign) {
$font->file(storage_path().'/font/wr.ttf');
$font->size($text_size);
$font->color($color);
$font->align($align);
$font->valign($valign);
});
return $image;
}
public function imageAddImage($image1, $image2, $image2_w, $image2_h, $offset_x=0, $offset_y=0, $offset='top-left'){
Image::configure(['driver'=>'gd']);
$image = Image::make($image1);
$image_v2 = Image::make($image2)->resize($image2_w, $image2_h);
$image_width = $image->width();
$image_height = $image->height();
$image_copy = $image->insert($image_v2, $offset, $offset_x, $offset_y);
return $image_copy;
}
public function uploadFile($file){
$ossClient = UploadController::getOssClient();
//生成file
$object = date('Y').date('m')."/".date('d')."/".basename($file);
$url = 'https://'.config('alioss.picture_domain').'/'.$object;
try {
//$result = $ossClient->putObject(config('alioss.buckets.picture'), $object, $file);
$result = $ossClient->uploadFile(config('alioss.buckets.picture'), $object, $file);
} catch(\OSS\Core\OssException $e) {
return $this->failure('oss_put_failure', $e->getMessage());
}
return $url;
}
public function getUrlqrcode($url, $size = 100){
$name = substr($url, stripos($url, '='));
$path = storage_path()."/qrcode/$name.png";
QrCode::format('png')->margin(1)->size($size)->generate($url, $path);
$pic = $this->uploadFile($path);
unlink($path);
return $pic;
}
}