代码:
public function mergePic(){
$ground = '/Public/merge/beijing.png';
$img = [
'url'=>'/Public/merge/qrcode.png',
'x'=>100,
'y'=>100
];
$qr = [
'url'=>'/Public/merge/qr.jpg',
'x'=>150,
'y'=>1400
];
$text = [
'size'=>20,
'text'=>'123456'
];
$this->merge($ground,$img,$qr,$text);
} /**
* @param $ground string 背景
* @param $img array 图片
* @param $qr array 二维码
* @param $text array 文字
*/
public function merge($ground,$img=[],$qr=[],$text=[]){
$types = [
"image/jpg" => 'imagecreatefromjpeg',
"image/jpeg" => 'imagecreatefromjpeg',
"image/png" => 'imagecreatefrompng',
"image/pjpeg" => 'imagecreatefromjpeg',
"image/gif" => 'imagecreatefromgif',
"image/bmp" => 'imagecreatefromwbmp',
"image/x-png" => 'imagecreatefromjpeg'
];
$groundMime = getimagesize(getcwd().$ground);
$grounds = $types[$groundMime['mime']](getcwd().$ground);//获取图片资源
// $fileName = "/Public/merge/".time().".png";//保存图片目录
$fileName = "/Public/merge/123.png";//保存图片目录
if($img){
$imgMime = getimagesize(getcwd().$img['url']);
$imgs = $types[$imgMime['mime']](getcwd().$img['url']);//获取图片资源
$imgsW = imagesx($imgs);//图片宽
$imgsH = imagesy($imgs);//图片高
imagecopy($grounds, $imgs, $img['x'], $img['y'], 0, 0, $imgsW, $imgsH);//核心函数:复制图片资源到另一图片资源中
} if($qr){
$qrMime = getimagesize(getcwd().$qr['url']);
$qrs = $types[$qrMime['mime']](getcwd().$qr['url']);//获取图片资源
$qrsW = imagesx($qrs);//图片宽
$qrsH = imagesy($qrs);//图片高
imagecopy($grounds, $qrs, $qr['x'], $qr['y'], 0, 0, $qrsW, $qrsH);//核心函数:复制图片资源到另一图片资源中
} if($text){
$size = $text['size'];//字体大小
$font = "./Public/merge/yuanti.ttf";//字体
$text = $text['text'];//显示的文字
$grey = imagecolorallocate($grounds,0,0,0);//设置字体颜色
imagettftext($grounds,$size,0,100,100,$grey,$font,$text);//将ttf文字写到图片中
} imagepng($grounds,getcwd().$fileName); //保存
imagedestroy($grounds);
imagedestroy($imgs);
imagedestroy($qrs);//销毁图片资源
}
参考: