php在已有图片上写字(水平居中)

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为加过文字后的图片(地址加文件名)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值