PHP 合成图片并在图片上加文字

/**
     * Info:  PHP把一张图片作为背景和另一张图片合成新图片
     */
	public function createImage(){
		$path_1 = 'XXXXX/attachment/images/20200801/4d8e641215b9ed593298ff6e853bd4d4.jpg'; //背景图片
		$path_2 = 'XXXXXXXX/attachment/wxampcode/4a498c4ee5b1353434fa1d04add9dba1.jpg';

		$image_1 = imagecreatefromjpeg($path_1);
		$image_2 = imagecreatefromjpeg($path_2);
		
		$image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
		
		$color = imagecolorallocate($image_3, 255, 255, 255);

		//获取图片的属性,第一个宽度,第二个高度,类型1=>gif,2=>jpeg,3=>png
		list($width,$height,$type) = getimagesize($path_1);
		//设置居中图片的X轴坐标位置
		$x = ($width)/3.1;
		//设置居中图片的Y轴坐标位置
		$y = ($height)/3.1;

		imagefill($image_3, 0, 0, $color);
		imageColorTransparent($image_3, $color);
		imagecopyresampled($image_3,$image_1,0,0,0,0,imagesx($image_1),
		imagesy($image_1),imagesx($image_1),imagesy($image_1));
		//图片在背景上的位置 $x横坐标,$y纵坐标
		imagecopymerge($image_3,$image_2, $x,$y,0,0,imagesx($image_2),imagesy($image_2), 100);
		//将画布保存到指定的文件
		imagejpeg($image_3, './new.jpg');
	}
//php gd库合成图片并在图片上加文字
public function ceshi(){
			$bgimg = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fdingyue.nosdn.127.net%2FmQIwcrN7Dq78tBWPT44I3oL5yv9lsL%3Dyu%3DvzUK%3DJzA0yr1530541107276compressflag.jpg&refer=http%3A%2F%2Fdingyue.nosdn.127.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641632933&t=a91cffe026acdbb99716738c22870c4f";//背景图
			$bg_info = getimagesize($bgimg);

			//获取图片的后缀
			$bg_type = image_type_to_extension($bg_info[2], false);
			//拼接图片资源句柄函数
			$func = 'imagecreatefrom' . $bg_type;
			//创建图片资源句柄
			$bg_image = $func($bgimg);

			//图1处理
			$tu1 = "http://XXXXXXX/attachment/wxampcode/a35b1399bca9de06c534aaaa2ba95ef4.jpg";
			$tu1_info = getimagesize($tu1);
			$tu1_type = image_type_to_extension($tu1_info[2], false);
			//创建图片资源句柄
			$tu1_func = 'imagecreatefrom' . $tu1_type;
			$tu1_image = $tu1_func($tu1);
			//合并背景图+图1
			imagecopy($bg_image, $tu1_image, 320, 800, 0, 0, 470, 400);  //图1 200距左边距 50距顶边距 301 299图1大小  背景图 0 0


			// //图2处理  二维码
			// $tu2 = "https://XXXXXX/attachment/images/20200801/4d8e641215b9ed593298ff6e853bd4d4.jpg";
			// $tu2_mark = getimagesize($tu2);
			// $tu2_type = image_type_to_extension($tu2_mark[2], false);
			// //创建图片资源句柄
			// $tu2_func = 'imagecreatefrom' . $tu2_type;
			// $tu2_image = $tu2_func($tu2);
			// //合并背景图+二维码
			// imagecopy($bg_image, $tu2_image, 583, 1193, 0, 0, 258, 261);


			//文字
			$font = "./Candice.ttf"; //字体文件
			$content = ['你好11111111111111111111111111111111111111111111111111111', '11111111111111111111111111111111111111小码农'];
			$font_lposition = [10, 10]; //距左边距
			$font_tposition = [920, 320]; //距上边距
			if (count($content) > 0) {
			for ($i = 0; $i < count($content); $i++) {
				imagettftext($bg_image, 25, 0, $font_lposition[$i], $font_tposition[$i], 21, $font, $content[$i]);
			}
			}

			//图片输出函数拼接
			$outFunc = 'image' . $bg_type;

			//保存图片
			$img_name = "new111.jpg";
			$outFunc($bg_image, $img_name);
			//销毁内存
			imagedestroy($bg_image);
			imagedestroy($tu1_image);
			// imagedestroy($tu2_image);

	}

你可能感兴趣的:(PHP,API,php)