PHP之多位置文字/多位置图片合成海报

本文介绍了一个使用PHP实现的抽奖系统,该系统能够从数据库中读取特定ID的数据,并利用这些数据生成带有奖品信息的图像。具体实现包括了图像的创建、文字的添加以及多个小图像的合成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
	$id=$_GET['id'];
	$conn=mysql_connect("localhost","root","root");
	mysql_select_db("yx",$conn);
	mysql_query("set names utf8");
	$sql="select * from pigcms_lottery where id='$id'";
	$result=mysql_query($sql);
	$arr=mysql_fetch_assoc($result);
	//echo $arr['fist'];
	//echo $arr['second'];
	//echo $arr['third'];
	//echo $arr['four'];
	//echo $arr['five'];
	//echo $arr['six'];
	function hc1($content1,$content2,$content3,$content4,$content5,$content7){
		 /*打开图片*/  
		//1.配置图片路径  
		$src = "http://localhost/tpl/Wap/default/common/css/guajiang/images/bb.png";
		//2.获取图片信息  
		$info = getimagesize($src);  
		//3.通过编号获取图像类型  
		$type = image_type_to_extension($info[2],false);  
		//4.在内存中创建和图像类型一样的图像  
		$fun = "imagecreatefrom".$type;  
		//5.图片复制到内存  
		$image = $fun($src);  
		$background = imagecolorallocate($image, 255, 255, 255);                                                                  
		imagecolortransparent($image, $background);
		//imagealphablending($image, false);
		imagesavealpha($image, true);
		/*操作图片*/  
		//1.设置字体的路径  
		$font = "msyh.ttf";  
		//2.填写水印内容
		$size=15;  
		//$content1 = "";  
		//3.设置字体颜色和透明度  
		$color = imagecolorallocatealpha($image, 255, 255,255,0);  
		//4.写入文字  
		imagettftext($image, "$size", -45, 325, 80, $color, $font, $content1); 
		//$content2 = "";  
		//3.设置字体颜色和透明度  
		$color = imagecolorallocatealpha($image, 255, 255,255,0);  
		//4.写入文字  
		imagettftext($image, "$size", 90, 40, 250, $color, $font, $content2); 
		//$content3 = "";  
		//3.设置字体颜色和透明度  
		$color = imagecolorallocatealpha($image, 255, 255,255,0);  
		//4.写入文字  
		imagettftext($image,"$size", 130, 112,360, $color, $font, $content3); 
		//$content4 = "";  
		//3.设置字体颜色和透明度  
		$color = imagecolorallocatealpha($image, 255, 255,255,0);  
		//4.写入文字  
		imagettftext($image,"$size", 0, 160, 400, $color, $font, $content4); 
		//$content5 = "";  
		//3.设置字体颜色和透明度  
		$color = imagecolorallocatealpha($image, 255, 255,255,0);  
		//4.写入文字  
		imagettftext($image, "$size", 40, 310, 380, $color, $font, $content5); 
		$content6 = "";  
		//3.设置字体颜色和透明度  
		$color = imagecolorallocatealpha($image, 255, 255,255,0);  
		//4.写入文字  
		imagettftext($image, "$size", 88, 400, 265, $color, $font, $content6); 
		//$content7= "";  
		//3.设置字体颜色和透明度  
		$color = imagecolorallocatealpha($image, 255, 255,255,0);  
		//4.写入文字  
		imagettftext($image,"$size", 0, 185, 40, $color, $font, $content7); 
		$content8 = "";  
		//3.设置字体颜色和透明度  
		$color = imagecolorallocatealpha($image, 255, 255,255,0);  
		//4.写入文字  
		imagettftext($image, "$size", 50, 70, 120, $color, $font, $content8); 
		/*输出图片*/  
		//浏览器输出  
		header("Content-type:".$info['mime']);  
		$fun = "image".$type;  
		//$fun($image);  
		//保存图片  
		$fun($image,'bg_res.'.$type);
		imagepng($image, "C:\\newbb3.png");
		//imagejpeg($image, "C:\\newbb.jpg");	
		/*销毁图片*/  
		imagedestroy($image);  
	}
	echo hc1(
        $arr['fist'],
        $arr['second'],
        $arr['third'],
        $arr['four'],
        $arr['five'],
        $arr['six']
    );
	function hc2($qCodePath,$img4,$img5,$img6,$img7,$img9){
		$bigImgPath = 'C:\\newbb3.png';
		$img3='http://localhost/tpl/Wap/default/common/css/guajiang/images/zlyc.png';
		$img8='http://localhost/tpl/Wap/default/common/css/guajiang/images/jxnl.png';
		$bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
		$qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
		$qCodeImg3= imagecreatefromstring(file_get_contents($img3));
		$qCodeImg4= imagecreatefromstring(file_get_contents($img4));
		$qCodeImg5= imagecreatefromstring(file_get_contents($img5));
		$qCodeImg6= imagecreatefromstring(file_get_contents($img6));
		$qCodeImg7= imagecreatefromstring(file_get_contents($img7));
		$qCodeImg8= imagecreatefromstring(file_get_contents($img8));
		$qCodeImg9= imagecreatefromstring(file_get_contents($img9));
		$qCodeWidth='60px';
		$qCodeHight='60px';
		list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
		list($qCodeWidth3, $qCodeHight3, $qCodeType) = getimagesize($img3);
		list($qCodeWidth4, $qCodeHight4, $qCodeType) = getimagesize($img4);
		list($qCodeWidth5, $qCodeHight5, $qCodeType) = getimagesize($img5);
		list($qCodeWidth6, $qCodeHight6, $qCodeType) = getimagesize($img6);
		list($qCodeWidth7, $qCodeHight7, $qCodeType) = getimagesize($img7);
		list($qCodeWidth8, $qCodeHight8, $qCodeType8) = getimagesize($img8);
		list($qCodeWidth9, $qCodeHight9, $qCodeType9) = getimagesize($img9);
		// imagecopymerge使用注解
		imagecopymerge($bigImg, $qCodeImg, 190, 80, 0, 0, $qCodeWidth, $qCodeHight, 100);
		imagecopymerge($bigImg, $qCodeImg3, 113, 95, 0, 0, $qCodeWidth3, $qCodeHight3, 100);  
		imagecopymerge($bigImg, $qCodeImg4, 70, 190, 0, 0, $qCodeWidth4, $qCodeHight4, 100); 
		imagecopymerge($bigImg, $qCodeImg5, 90, 280, 0, 0, $qCodeWidth5, $qCodeHight5, 100);  
		imagecopymerge($bigImg, $qCodeImg6, 173, 300, 0, 0, $qCodeWidth6, $qCodeHight6, 100); 
		imagecopymerge($bigImg, $qCodeImg7, 270, 290, 0, 0, $qCodeWidth7, $qCodeHight7, 100);
		imagecopymerge($bigImg, $qCodeImg8, 300, 200, 0, 0, $qCodeWidth8, $qCodeHight8, 100);
		imagecopymerge($bigImg, $qCodeImg9, 280, 120, 0, 0, $qCodeWidth9, $qCodeHight9, 100);
		list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);
		$background = imagecolorallocate($bigImg, 255, 255, 255);                                                                  
		imagecolortransparent($bigImg, $background);
		imagesavealpha($bigImg, true); 
		switch ($bigType) {
			case 1: //gif
				header('Content-Type:image/gif');
				imagegif($bigImg);
				break;
			case 2: //jpg
				header('Content-Type:image/jpg');
				imagejpeg($bigImg);
				break;
			case 3: //jpg
				header('Content-Type:image/png');
				imagepng($bigImg);
				break;
			default:
				# code...
				break;
		}
		imagepng($bigImg, "C:\\nbb.png"); 
		imagedestroy($bigImg);	
	}
    //echo hc2(
    //"http://localhost/tpl/Wap/default/common/css/guajiang/images/hwsh.png",
    //"http://localhost/tpl/Wap/default/common/css/guajiang/images/hwsh.png",
    //"http://localhost/tpl/Wap/default/common/css/guajiang/images/hwsh.png",
    //"http://localhost/tpl/Wap/default/common/css/guajiang/images/hwsh.png",
    //"http://localhost/tpl/Wap/default/common/css/guajiang/images/hwsh.png",
    //"http://localhost/tpl/Wap/default/common/css/guajiang/images/hwsh.png"
    //);
	echo hc2($arr['bg1'],$arr['bg2'],$arr['bg4'],$arr['bg5'],$arr['bg6'],$arr['bg7']);
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值