$msg = 'AAAAAAAA';//文字描述
$bg = __DIR__.'/bg.png';//背景图片
$target = __DIR__.'/target.png';//要合成的图片
$dirname = __DIR__.'/composite/'.$this->create_token();//生成一个随机数

$background_image = imagecreatefromstring(file_get_contents($bg));
$target_image = imagecreatefromstring(file_get_contents($target));
list($target_width,$target_height,$target_type) = getimagesize($target);
list($background_width,$background_height,$background_type) = getimagesize($bg);
//防止透明色变黑色
imagesavealpha($background_image, true);
//将target图片放入背景图片上,尺寸为523*605
imagecopyresampled($background_image,$target_image,13,12,0,0,523,605,$target_width,$target_height);
//生成文字
$font = __DIR__.'/composite/public/font.ttf';
$black = imagecolorallocate($background_image,0,0,0);
$fontSize = 20;
imagettftext($background_image,$fontSize,0,33,705,$black,$font,'活动最终解释权');
imagettftext($background_image,$fontSize,0,33,735,$black,$font,'归商家所有');
$fontSize = 10;
imagettftext($background_image,$fontSize,0,329,690,$black,$font,'兑换码');
$fontSize = 25;
imagettftext($background_image,$fontSize,0,329,730,$black,$font,$msg);
imagettftext($background_image,$fontSize,0,330,730,$black,$font,$msg);
//保存图片
$header = 'image/png';
switch ($background_type){
case 1:
$dirname = $dirname.'.gif';
$header = 'image/gif';
imagegif($background_image,$dirname);
break;
case 2:
$dirname = $dirname.'.jpg';
$header = 'image/jpg';
imagejpeg($background_image,$dirname);
break;
case 3:
$dirname = $dirname.'.png';
$header = 'image/png';
imagepng($background_image,$dirname);
break;
}
imagedestroy($target_image);
imagedestroy($background_image);
Http::header("Content-Type: $header");
$data = file_get_contents($dirname);
unlink($dirname);//删除图片文件
//$data就是图片数据
return $this->send($data);