gd库为图片加文字水印和图片输出
$number = $result['certno'];//设置水印内容
$file = './Uploads/certs/' . $number . '.jpg';
if(!is_file($file)){
$src = './template/public/img/zhengshu.jpg';//定义图片地址
$info = getimagesize($src);//获取图片信息
$type = image_type_to_extension($info[2], false);//通过编号获取图片类型
$fun = "imagecreatefrom".$type;//在内存中创建一样的图像
$image = $fun($src);//图片复制到内存
$font = './msyh.ttf';//设置字体路径
$fontbd = './msyhbd.ttf';//设置字体路径
$content = $result['authname'];//设置水印内容
$website = $result['auth_domains'];//设置水印内容
//求出水印内容在使用字体下在图片的四个边框的xy坐标
$version_fontarea1 = ImageTTFBBox(25,0,$fontbd,$version);
$content_fontarea1 = ImageTTFBBox(25,0,$font,$content);
$website_fontarea1 = ImageTTFBBox(12,0,$font,$website);
$width = $info[0];//获取图片宽度
//求出内容应该设置在图片中的位置(y左边我是自己一点点试的...x坐标是让水印内容两侧距离相等)
$version_x = ($width - ($version_fontarea1[2] - $version_fontarea1[0])) / 2;
$content_x = ($width - ($content_fontarea1[2] - $content_fontarea1[0])) / 2;
$website_x = ($width - ($website_fontarea1[2] - $website_fontarea1[0])) / 2;
$color = imagecolorallocatealpha($image, 75, 75, 75, 0);//设置颜色
imagettftext($image, 11, 0, 105, 80, $color, $font, $number);
imagettftext($image, 25, 0, $version_x, 350, $color, $fontbd, $version);
imagettftext($image, 25, 0, $content_x, 520, $color, $font, $content);
imagettftext($image, 12, 0, $website_x, 560, $color, $font, $website);
//输出图片
$fun = "image".$type;
$fun($image, $file , 80);
imagedestroy($image);
}
$data = file_get_contents($file);
代码中依然存在很大问题,因为这样的做法,意味着有多少个result,就会在文件夹中生成多少张图片。没有找到gd库直接从内存中获取图片内容的方法,迫不得已用这个方法