php在已有图片上写字
/**
* @param $bg 图片资源文件
* @param $text 书写的内容
* @param $font_size 字体大小
* @param $top 距图片顶部的距离
* @param $c_r rgb颜色值
* @param $c_g
* @param $c_b
*/
public function stringImg($bg, $text, $font_size, $top, $c_r, $c_g, $c_b) {
$font_file = __DIR__ . '/simhei.ttf'; //字体文件
$font_width = ImageFontWidth($font_size);
$font_height = ImageFontHeight($font_size);
$text_color = imagecolorexact($bg, $c_r, $c_g, $c_b);
//$text_color = imagecolorexact($bg, 255, 0, 0);
$string = mb_convert_encoding($text, 'html-entities', 'UTF-8');
//取得 str 2 img 后的宽度
$temp = imagecreatetruecolor($font_height, $font_width);
$res = imagefttext($temp, $font_size, 0, (imagesx($bg) - $font_width) / 2, $top + $font_height, $text_color, $font_file, $string);
$strImgWidth = $res[2] - $res[0];
imagefttext($bg, $font_size, 0, (imagesx($bg) - $strImgWidth) / 2, $top + $font_height, $text_color, $font_file, $string);
}
$new_pics = imagecreatefrompng($new_pic); //图片资源
Image::stringImg($new_pics, $app_name, 30, 398, 53, 53, 53);
imagepng($new_pics,$new_pic);
$new_pic为加过文字后的图片(地址加文件名)