1.创建一个CodeImgController控制器
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use DB;
class CodeImgController extends BaseController
{
private $date,$img,$main,$width,$height,$target,$white;
public function constr($source,$save_path)
{
$this->date = '' . date('Ymd') . '/';
$this->img = $save_path ;
$this->main = imagecreatefromjpeg($source);
$this->width = imagesx($this->main);
$this->height = imagesy($this->main);
$this->target = imagecreatetruecolor($this->width, $this->height);
$this->white = imagecolorallocate($this->target, 255, 255, 255);
imagefill($this->target, 0, 0, $this->white);
imagecopyresampled($this->target, $this->main, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
}
/**
* 生成二维码
* @param [type] $website [生成二维码地址]
* @param string $filename [生成二维路经名称]
* @param string $level [这个参数可传递的值分别是L(QR_ECLEVEL_L,7%)、M(QR_ECLEVEL_M,15%)、Q(QR_ECLEVEL_Q,25%)、H(QR_ECLEVEL_H,30%),这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比,也就是被覆盖的区域还能识别;]
* @param integer $size [控制生成图片的大小,默认为4]
* @param integer $margin [控制生成二维码的空白区域大小]
* @return [type] [description]
*/
public function qrencode($PictureContent = '',$PictureUrl = '',$PictureFormat = 'jpg')
{
// QRcode::png($website, $filename, $level, $size, 2);
if($PictureContent != '' && $PictureUrl != ''){
QrCode::format($PictureFormat)
->size(300)
->color(0,0,0)
->backgroundColor(255,255,255)
->margin(10)
->generate($PictureContent,public_path($PictureUrl));
}
}
/**
* 把二维码图片生成到背景图片上及文字
* @param string $source 背景图片
* @param string $text1 文字描述
* @param string $child1 二维码图
* @param integer $textwidth 文字横向位置
* @param integer $textherght 文字高度
* @param integer $$fontSize 字体大小
* @param integer $cate1,$cate2,$cate3 颜色表
* @param string $font 文字字体
* @return [type] [description]
*/
public function generateFont($source,$save_path, $text1, $textwidth, $textherght, $fontSize = 50, $cate1 = 255, $cate2 = 250, $cate3 = 250, $font = '/font/fangsong_GB2312.ttf')
{
$this->constr($source,$save_path);
$fontColor = imagecolorallocate($this->target, $cate1, $cate2, $cate3); //字的RGB颜色
// 计算出文字在图片中的宽度
$fontBox = imagettfbbox($fontSize, 0, $font, $text1); //文字水平居中实质
$txt_width=$fontBox[2]-$fontBox[0];
// 获取文字在图片中居中的x轴
$x = ($this->width - $txt_width) / 2;
imagettftext($this->target, $fontSize, 0, $x, $textherght, $fontColor, $font, $text1);
$this->createImg();
return $this->img;
}
/**
* [generateImg description]
* @param string $source 背景图片
* @param string $codeurl 二维码图片
* @param integer $sourcewidth 二维码横向所在位置
* @param integer $sourceheight 二维码高度位置
* @param integer $codewidth 二维码宽度
* @param integer $codeheight 二维码高度
* @return [type] [description]
*/
public function generateImg($source,$save_path, $codeurl, $sourcewidth, $sourceheight, $codewidth = 100, $codeheight = 100)
{
$this->constr($source,$save_path);
$child1 = imagecreatefrompng($codeurl);
$codewidth = $codewidth > 0 ? $codewidth :imagesx($child1);
$codeheight = $codeheight > 0 ? $codeheight : imagesy($child1);
imagecopyresampled($this->target, $child1, $sourcewidth, $sourceheight, 0, 0, $codewidth, $codeheight,imagesx($child1),imagesy($child1));
imagedestroy($child1);
$this->createImg();
return $this->img;
}
function createImg()
{
// @mkdir('./' . $this->date);
imagejpeg($this->target, './' . $this->img, 95);
imagedestroy($this->main);
imagedestroy($this->target);
}
}
2.使用
$PictureContent = $https.$_SERVER['SERVER_NAME'].'/api/v1/user/code/register/html/'.session('ru_id');
$PictureFormat = 'png';
$PictureUrl = '/images/SalesmanCode/'.session('ru_id')."_".$time.".".$PictureFormat;
// $img_url = $PBULIC->SalesmanCode($PictureContent,$PictureUrl,$PictureFormat);
//背景图片
$source = public_path()."/img/bg_qcode.jpg";
$save_path = $PictureUrl;
$codeImg = new CodeImgController($source,$save_path);
$codeurl = public_path().$save_path;
$codeImg->qrencode($PictureContent,$PictureUrl,$PictureFormat);
$generateImg = $codeImg->generateImg($source,$save_path, $codeurl, $sourcewidth = 290, $sourceheight= 390, $codewidth = 500, $codeheight = 500);
$text = $seller_info->shop_name;
$font = public_path().'/font/fangsong_GB2312.ttf';
$img_url = $codeImg->generateFont($codeurl,$save_path, $text, $textwidth=310, $textherght=1150,$fontSize = 40, $cate1 = 255, $cate2 = 255, $cate3 = 250,$font);
$salesman_code = $https.$_SERVER['SERVER_NAME'].$img_url;