支持多次加入文本、图片到背景图
class MergeImage{
protected $water;
protected $text_ttf = './fonts/msyhbd.ttf';
// 构造函数 传入 背景图片
function __construct($bg) {
$this->water = new WaterMask($bg);
}
/**
* [往背景图添加图片]
*
* @param [string] $file [需要加入的图片地址]
* @param [int] $x [加入到背景图的水平坐标]
* @param [int] $y [加入到背景图的垂直坐标]
* @param integer $w [默认为 0 表示不需要缩放]
* @param integer $h [默认为 0 表示不需要缩放]
*/
public function addImage($file,$x,$y,$w=0,$h=0) {
if ($w) {
$tmp_file = tempnam(sys_get_temp_dir(), "image_");
$temp_water = new WaterMask($file);
$temp_water->zoom([$w,$h],$tmp_file);
$file = $tmp_file;
}
$this->water->imageMark($file,'', array($x, $y));
}
/**
* [往背景图添加文本]
*
* @param [string] $text [文本内容]
* @param [array] $colors [RFB颜色值数组]
* @param [int] $x [加入到背景图的水平坐标]
* @param [int] $y [加入到背景图的垂直坐标]
* @param [int] $fontsize [文字字体]
* @param [type] $text_ttf [文字用到的字体ttf ttc 地址]
*/
public function addText($text,$colors,$x,$y,$fontsize,$text_ttf) {
$this->water->color = $colors;
// 字体大小
$this->water->fontSize = $fontsize;
if ($text_ttf) {
$this->water->fontFile = $text_ttf;
} else {
// 字体文件路径
$this->water->fontFile = $this->text_ttf;
}
// 加水印
$this->water->TextMark($text, '', [$x,$y]);
}
// 预览
public function show() {
$this->water->show();
}
// 直接保存为指定地址的图片
public function save($file){
$this->water->save($file);
}
}
演示
$bg = './public/qrcode_bg.jpg';
$qrcode = './public/qrcode.png';
$qrcode_big = './public/qrcode_big.png';
$qrcode_width = 266;
$qrcode_height = 266;
$text= "01";
$obj = new MergeImage($bg);
$obj->addImage($qrcode,490,253,$qrcode_width,$qrcode_height);
$obj->addText($text,[36,162,65],35,660,180,'./fonts/msyhbd.ttc');
$obj->save($qrcode_big);