zoukankan      html  css  js  c++  java
  • 将头像、文字和二维码三部分,合成一张宣传图片

    这里是thinkphp框架用到的图片合成,逻辑都是一样的,不用框架纯php的话,只是文件的保存位置没有那么多的规定,逻辑什么的还是一样的。

    作者:悦~  地址:https://www.cnblogs.com/nuanai/

    一、二维码生成在https://www.cnblogs.com/nuanai/p/10501946.html博客中写到过的,在这就不详细的说了    作者:悦~  地址:https://www.cnblogs.com/nuanai/

    二、首先将合成图片所用到的内容先形成数组,包括各元素所在的位置、颜色、大小等,然后将数组传到合成图片用到的方法中

    注意:createPoster(内容,图片名称)方法的传值,如果不传图片名称的话,直接在网页中显示,如果有图片名称得话保存在本地,可以减少每次图片的生成时间,前端直接调用保存在本地的图片就可以了   作者:悦~  地址:https://www.cnblogs.com/nuanai/

    	public function Xian(){
    		$tid = 2;  //扫描二维码传入的id值
    		//二维码生成
    		$url = "http://www.baidu.com";
    		$level=3;
    		$size=4;
    		Vendor('phpqrcode.phpqrcode');
    		$errorCorrectionLevel = intval($level) ;//容错级别
    		$matrixPointSize = intval($size);//生成图片大小
    		$object = new QRcode(); //调用二维码插件
    		$path = "Public/ER/" . $tid . ".png"; //本地文件存储路径
    		$object->png($url, $path, $errorCorrectionLevel, $matrixPointSize, 2);
    		//二维码生成
    		
    		//图片所需要的内容
    		$headimgurl = "https://image.so.com/view?q=%E6%A8%B1%E8%8A%B1%E5%9B%BE%E7%89%87&listsrc=sobox&listsign=15d7d9ebd0cd621a7e74c66a2b57aff2&src=360pic_strong&correct=%E6%A8%B1%E8%8A%B1%E5%9B%BE%E7%89%87&ancestor=list&cmsid=a7ec45ccb1b3e723a083d57001e3165b&cmran=0&cmras=6&cn=0&gn=0&kn=28&offspring=85bc14812e545b0c30263ea459328a2b&multiple=0&gsrc=1&currsn=0&jdx=17";
    		//头像可以通过数据库查找替换
    		$nickname = "你好";  //昵称可以通过数据库查找替换
    		$erid = 2; //这个是保存的图片的名字,可以通过数据库进行替换
    		
    		$header = array(
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
    'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
    'Accept-Encoding: gzip, deflate',);
    		$url=$headimgurl;
    		$curl = curl_init();
    		curl_setopt($curl, CURLOPT_URL, $url);
    		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    		curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    		curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    		$data = curl_exec($curl);
    		$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);curl_close($curl);
    		if ($code == 200) {
    			//把URL格式的图片转成base64_encode格式的!
    			$imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
    		}
    		
    		$img_content=$imgBase64Code;  //图片内容
    		if (preg_match('/^(data:s*image/(w+);base64,)/', $img_content, $result))
    		{
    			$type = $result[2];  //得到图片类型png?jpg?gif?
    			$new_file = 'Public/ER/'.$erid.'.'.$type;  //图片的路径找到图片
    			if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content))))
    			{  
    				$config = array(
    					    'text'=>array(
    					        array(
    					            'text'=>$nickname,  //微信用户昵称
    					            'left'=>750,
    					            'top'=>310,
    					            'fontPath'=>'Public/bootstrap/fonts/simhei.ttf', //字体文件
    					            'fontSize'=>44, //字号
    					            'fontColor'=>'0,147,82', //字体颜色
    					            'angle'=>0,
    					        )
    					    ),
    					    'image'=>array(
    					        array(
    								'url'=>'Public/ER/'.$erid.'.png',//用户的生成的二维码
    					            'left'=>422,
    					            'top'=>-200,
    					            'stream'=>0,//图片资源是否是字符串图像流
    					            'right'=>0,
    					            'bottom'=>0,
    					            'width'=>500,
    					            'height'=>500,
    					            'opacity'=>100
    					        ),
    					        array(
    					            'url'=>$new_file,  //用户头像
    					            'left'=>280,
    					            'top'=>255,
    					            'right'=>0,
    								'stream'=>0,
    					            'bottom'=>0,
    					            'width'=>200,
    					            'height'=>200,
    					            'opacity'=>100
    					        ),
    					    ),
    					    'background'=>'Public/img/hai.jpg',  //合成用的背景图
    					    
    					);
    					$filename = 'Public/ER/'.$erid.'.jpg';
    					$filurl = $erid.'.jpg';
    					$this->createPoster($config,$filename);  //调用方法传值,1.合成图片用到的昵称、背景图、二维码 
    					$this->assign("filurl",$filurl);  //注入前端显示
    					$this->show();
    
    			 }
    		}
    
    	}
    

      

    三、其次下面是图片合成用到的方法   作者:悦~  地址:https://www.cnblogs.com/nuanai/

    /**
    	 * 生成宣传海报
    	 * @param  array 	参数,包括图片和文字
    	 * @param  string 	$filename 生成海报文件名,不传此参数则不生成文件,直接输出图片
    	 * @return [type] [description]
    	 */
    	function createPoster($config=array(),$filename=""){
    	    //如果要看报什么错,可以先注释调这个header 
    		if(empty($filename)) header("content-type: image/png");
    	 
    	    $imageDefault = array(
    	        'left'=>0,
    	        'top'=>0,
    	        'right'=>0,
    	        'bottom'=>0,
    	        'width'=>100,
    	        'height'=>100,
    	        'opacity'=>100
    	    );
    	    $textDefault =  array(
    	        'text'=>'',
    	        'left'=>0,
    	        'top'=>0,
    	        'fontSize'=>40,             //字号
    	        'fontColor'=>'255,255,255', //字体颜色
    	        'angle'=>0,
    	    );
    	 
    	    $background = $config['background'];//海报最底层得背景  
    	    //背景方法
    	    $backgroundInfo = getimagesize($background);
    	    $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
    	    $background = $backgroundFun($background);
    	 
    	    $backgroundWidth = imagesx($background);    //背景宽度
    	    $backgroundHeight = imagesy($background);   //背景高度
    	 
    	    $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight); 
    	    $color = imagecolorallocate($imageRes, 0, 0, 0);  
    	    imagefill($imageRes, 0, 0, $color);  
    	 
    	    //imageColorTransparent($imageRes, $color);    //颜色透明
    	 
    	    imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));  
    	 
    	    //处理了图片
    	    if(!empty($config['image'])){
    	        foreach ($config['image'] as $key => $val) {
    	            $val = array_merge($imageDefault,$val);
    	 
    	            $info = getimagesize($val['url']);
    	            $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
    				if($val['stream']){		//如果传的是字符串图像流
    					$info = getimagesizefromstring($val['url']);
    					$function = 'imagecreatefromstring';
    				}
    	            $res = $function($val['url']);
    	            $resWidth = $info[0];
    	            $resHeight = $info[1];
    	            //建立画板 ,缩放图片至指定尺寸
    	            $canvas=imagecreatetruecolor($val['width'], $val['height']); 
    	            imagefill($canvas, 0, 0, $color);
    	            //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h) 
    	            imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight); 
    	            $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
    	            $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
    	            //放置图像
    	            imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度 
    	        }
    	    }
    	 
    	    //处理文字
    	    if(!empty($config['text'])){
    	        foreach ($config['text'] as $key => $val) {
    	            $val = array_merge($textDefault,$val);
    	            list($R,$G,$B) = explode(',', $val['fontColor']);
    	            $fontColor = imagecolorallocate($imageRes, $R, $G, $B); 
    	            $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
    	            $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
    	            imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']); 
    	        }
    	    }
    	
    	    //生成图片  
    	    if(!empty($filename)){
    	        $res = imagejpeg ($imageRes,$filename,90); //保存到本地
    			imagedestroy($imageRes);
    			if(!$res) return false;
    			return $filename;
    	    }else{
    	        imagejpeg ($imageRes);			//在浏览器上显示
    			imagedestroy($imageRes);
    	    }  
    	}
    

      

    四、最后是在前端显示就可以了   作者:悦~  地址:https://www.cnblogs.com/nuanai/

    用图片标签或者背景图属性这要看自己的习惯了 

  • 相关阅读:
    Java8新特性2 lambda表达式
    Java8新特性1
    多线程与高并发(2)Volatile+CAS
    多线程与高并发 Synchronize
    《重构:改善既有代码的设计》读书笔记5 简化函数调用
    《重构:改善既有代码的设计》读书笔记4 简化条件表达式
    mysql语法大全使用篇
    《重构:改善既有代码的设计》读书笔记3 重新组织数据
    《重构:改善既有代码的设计》读书笔记2 在对象之间搬移特性
    《重构:改善既有代码的设计》读书笔记1——重新组织函数
  • 原文地址:https://www.cnblogs.com/nuanai/p/10684564.html
Copyright © 2011-2022 走看看