1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > PHP图片与文字合成

PHP图片与文字合成

时间:2020-01-13 04:15:46

相关推荐

PHP图片与文字合成

public function coupon_image($coupon){$userName = $coupon['username'];//合成的文字$path = public_path();//这里我的查找路径方法,你用你自己的即可ob_clean();$types = array(1 => "gif", 2 => "jpeg", 3 => "png");//图片类型$bj_path = $coupon['bj_path'];//目标大图,背景图的链接list($width0, $height0, $type0) = getimagesize($bj_path);//获取图片参数$createBgImg = "imagecreatefrom" . $types[$type0];$dstImage = $createBgImg($bj_path);$nickname_color='179,94,0';//step1:昵称合并$colors = explode(',', $nickname_color);$vice_w = $vice_h = 300;//副图的画布大小$im = imagecreate($vice_w, $vice_w);//副图创建画布$white = imagecolorallocate($im, $colors[0], $colors[1], $colors[2]);imagecolortransparent($im, $white); //设置具体某种颜色为透明色$black = imagecolorallocate($im, $colors[0], $colors[1], $colors[2]);$nameSize = $coupon['nameSize'];//字体大小$fontBox = imagettfbbox($nameSize, 0, $path . '/simkai.ttf', $userName);//文字水平居中实质$name_x = ceil(($width0 - $fontBox[2]) / 2) -10;//水平居中坐标$dst_y = $coupon['dst_y'];//距离顶部距离imagettftext($im, $nameSize, 0, 10, 100, $black, $path . '/simkai.ttf', $userName); //字体设置部分linux和windows的路径可能不同imagettftext($im, $nameSize, 0, 11, 100, $black, $path . '/simkai.ttf', $userName); //字体加粗 向右偏移一个像素imagecopyresampled($dstImage, $im, $name_x, $dst_y, 0, 0, 300, 300, $vice_w, $vice_h);// 保存合成图片$dir = $path . '/wechatpic/' . date('Y-m-d');if (!file_exists($dir)) {mkdir($dir, 0777, true);}$endImgUrl = "{$dir}/" . uniqid() . ".jpg";//图片保存路径$save = "image" . $types[$type0];$save($dstImage, $endImgUrl);//保存文件//删除图片if(isset($coupon['del'])){!empty($bj_path) && unlink($bj_path);}return $endImgUrl;}

原图:

合成完:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。