• tp5.0 FAST 生成海报图


    推销部门的海报,大致类容都差不多,经常会修改一些简介,诊疗范围,擅长病种等等,刚开始想到的是利用扩展截图然后去下载,网上的例子倒是不多,目前有两个例子

    1. zyan/url2pic-sdk
      CSDN地址
      可以composer安装,也可调用接口
      composer安装的时候有冲突,捣鼓了一阵发现需要安装一些别的扩展,就放弃,不想安装太多扩展
      就开始调用API 但是返回来的图片好多css都没加载完就截图完成了,他应该是网站加载完就截图了,没有等适配就截图了,所以布局的时候需要考虑进去
    2. PhantomJS
      这个也是composer安装的时候需要额外安装一些扩展,实用性有待参考

    后来才想起来海报图,之前一直就想的是生成网页,然后截图,下载就行。
    下面是原图
    在这里插入图片描述
    正如图片所示,需要修改的地方还蛮多的,名称的位置,名称可能两位数,三位数,四位数,也需要判断去调整left的位置,名称下的标签也是一样,根据字数调整文字的大小等等,还有成就需要去掉标签载换行,然后添加上对应的五角星。后台:简介这里做了一个开关,打开的时候必须填写简介内容,下方的诊疗范围/擅长范围,有两个板块,只能显示一个,所以做了一个开关控制,单选选择这两个小板块,
    简介这里由于文字不定,所以稍微计算了一下文字的高度,来自适应的增加/减去背景的高度,下方的诊疗范围也是一样
    代码垃圾,后期会优化

    	protected $summary_height = ''; // 简介范围文字的高度
    	protected $dat_height = ''; // 诊疗范围文字的高度
    	protected $good_at_height = ''; // 擅长范围文字的高度
    	protected $brown_height = 1500; // 下半部分 棕色部分“一行文字”所需最低高度
    	protected $Pentagram = ''; // 五角星
    	protected $textRows = ''; // 文字行数
    	protected $textRowsHight = 0; // 文字行数 高
    	protected $bat_ga_textRows = ''; // 诊疗/擅长行数
    	protected $bat_ga_image_top = 0; // 诊疗/擅长 上半部分
    	protected $bat_ga_image_down = 65; // 诊疗/擅长 下半部分 背景高度
    
    /**
    	 * 生成海报图
    	 * @param $docname // 医生名称
    	 * @param $doclabel    // 医生职称
    	 * @param $docimg   // 医生图片
    	 * @param $docachieve   // 医生成就
    	 * @param $syswitch // 简介开关
    	 * @param $summary  // 简介内容
    	 * @param $gaswitch // 诊疗/擅长开关
    	 * @param $gadata  // 诊疗/擅长 选择
    	 * @param $gac      // 诊疗/擅长内容
    	 */
    	public function posterImage($docname,$doclabel,$docimg,$docachieve,$syswitch = 0,$summary,$gaswitch = 0,$gadata = 0,$gac)
    	{
    		$summary = $this->tagecontent($summary,mb_strlen($summary,"utf-8"));
    		$gac = $this->tagecontent($gac,mb_strlen($gac,"utf-8"));
    		// 转换 成就 值
    		// 正则匹配获取p标签里的值,并重新赋值
    		$matches = '';
    		// preg_match 匹配出第一条符合条件的数据后停止继续操作
    		// preg_match_all 匹配出全部
    		preg_match_all('/(.*?)(?=<\/p>)/im', $docachieve, $matches);
    		unset($matches[0]);
    		foreach($matches[1] as $v){
    			$search = array(" "," ","\t","\n","\r");
    			$v = str_replace($search, '', $v);
    			$val = trim(strip_tags($v));
    			if(!empty($val)){
    				$bt_arrays[] = $val;
    			}
    		}
    		// 过滤数组中的元素。
    		$bt_arrays = array_filter($bt_arrays);
    		// 数组转字符串并换行
    		$docachieve = implode("\n",$bt_arrays);
    		// 统计五角星
    		$this->Pentagram = count($bt_arrays);
    		// 获得职位长度,调整位置
    		$length_label = mb_strlen($doclabel,"utf-8");
    		$label_left = 100;
    		$label_fontSize = 35;
    		switch ($length_label){
    			case $length_label == 4;
    				$label_left = 150;
    				break;
    			case $length_label == 5;
    				$label_left = 120;
    				break;
    			case $length_label == 6;
    				$label_left = 95;
    				break;
    			case $length_label == 7;
    				$label_left = 70;
    				break;
    			case $length_label == 8;
    				$label_left = 50;
    				break;
    			case $length_label == 9;
    				$label_left = 38;
    				$label_fontSize = 33;
    				break;
    			case $length_label == 10;
    				$label_left = 39;
    				$label_fontSize = 29;
    				break;
    			case $length_label == 11;
    				$label_left = 39;
    				$label_fontSize = 27;
    				break;
    			default:
    				$label_left = 100;
    		};
    		// 字体文件,可以去百度下载,计算机里面也有,可以复制到项目里去用,路径要用绝对路径
    		$fontPath = ROOT_PATH.'/public/assets/fonts/5c8a05a7b3b641552549287.otf';
    		$config = [
    			'text' => [
    				[
    					// 医生名称
    					'text'      => $docname,
    					'left'      => 55,				//文字距离左边框多远(负数是从右往左)
    					'top'       => 600,			    //文字距离上边框多远(负数是从下往上)
    					'fontPath'  => $fontPath,       //字体文件
    					'fontSize'  => 90,              //字号
    					'fontColor' => '115,66,34',     //字体颜色
    					'angle'     => 0,				//倾斜角度
    					'spacing'   => 25,              //字间距调整设置
    				],
    				[
    					// 医生标签
    					'text'      => $doclabel,
    					'left'      => $label_left,				//文字距离左边框多远(负数是从右往左)
    					'top'       => 700,			//文字距离上边框多远(负数是从下往上)
    					'fontPath'  => $fontPath,       //字体文件
    					'fontSize'  => $label_fontSize,              //字号
    					'fontColor' => '255,255,255',      //字体颜色
    					'angle'     => 0,				//倾斜角度
    				],
    			],
    			'achieve' =>[
    				  // 医生成就
    					'text'      => $docachieve,
    					'left'      => 62,				//文字距离左边框多远(负数是从右往左)
    					'top'       => 720,			//文字距离上边框多远(负数是从下往上)
    					'fontPath'  => $fontPath,		//字体文件
    					'fontSize'  => 23,				//字号
    					'fontColor' => '0,0,0',		//字体颜色
    					'angle'     => 0,				//倾斜角度
    					'width'     => 500,
    
    			],
    			'image' => [
    				[//海报上部分图
    					'url'       => "../public/assets/XXXX/13_01.png", //图片地址
    					'stream'    => 0,//图片资源是否是字符串图像流
    					'left'      => 0,//图片距离左边框多远(负数是从右往左)
    					'top'       => 0    ,//图片距离上边框多远(负数是从下往上)
    					'right'     => 0,
    					'bottom'    => 0,
    					'width'     => 950,//图片宽度(不要超过背景图的尺寸)
    					'height'    => 1063,//图片高度(不要超过背景图的尺寸)
    					'opacity'   => 100,//透明度
    					'radius'    => 0,//图片圆角角度
    				],
    				[// 医生图片
    					'url'       => ROOT_PATH.'/public/'.$docimg,
    					'stream'    => 0,
    					'left'      => 520,
    					'top'       => 123,
    					'right'     => 0,
    					'bottom'    => 0,
    					'width'     => 430,
    					'height'    => 940,
    					'opacity'   => 100,
    					'radius'    => 15,
    				],
    				[
    					// 底部
    					'url'       => "../public/assets/XXXX/17_02.png",
    					'stream'    => 0,
    					'left'      => 0,
    					'top'       => -1,
    					'right'     => 0,
    					'bottom'    => 0,
    					'width'     => 950,
    					'height'    => 290,
    					'opacity'   => 100,
    				],
    			],
    			'background'    => '../public/assets/XXXX/14.png', //背景图
    			'pentagram'     => [
    				'url'       => '../public/assets/XXXX/wujiaoxing.png', // 五角星
    				'stream'    => 0,
    				'left'      => 35,
    				'top'       => 748,
    				'right'     => 0,
    				'bottom'    => 0,
    				'width'     => 25,
    				'height'    => 25,
    				'opacity'   => 100,
    			],
    		];
    
    		if($syswitch == 1){
    			$sy_title_img = [
    				// 医生简介头部
    				'url'       => "../public/assets/XXXX/18_02.png",
    				'stream'    => 0,
    				'left'      => 0,
    				'top'       => 1090,
    				'right'     => 0,
    				'bottom'    => 0,
    				'width'     => 950,
    				'height'    => 52,
    				'opacity'   => 100,
    				'radius'    => 15,
    			];
    
    			$sy_text = [
    				// 简介
    				'text' => $this->getbrtext(1060, 22, 0, $fontPath, $summary,1), // 文字换行
    				'left' => 40,                //文字距离左边框多远(负数是从右往左)
    				'top' => 1190,            //文字距离上边框多远(负数是从下往上)
    				'fontPath' => $fontPath,        //字体文件
    				'fontSize' => 18,                //字号
    				'fontColor' => '255,255,255',        //字体颜色
    				'angle' => 0,                //倾斜角度
    			];
    			array_push($config['image'],$sy_title_img);
    			array_push($config['text'],$sy_text);
    
    			// 背景最低高度 + 文字高度
    			$this->brown_height =  $this->brown_height + $this->summary_height;
    
    			if ($gaswitch == 1){
    				// 诊疗/擅长 头部 距离简介文字的高度
    				$this->bat_ga_image_top = 1190 + $this->summary_height + $this->textRows;
    				$in_text = [
    					// 擅长
    					'text' => $this->getbrtext(990, 22, 0, $fontPath, $gac,2), // 文字换行
    					'left' => 80,                //文字距离左边框多远(负数是从右往左)
    					'top' => $this->bat_ga_image_top + 105,//文字距离上边框多远(负数是从下往上)105是富裕高度
    					'fontPath' => $fontPath,        //字体文件
    					'fontSize' => 18,                //字号
    					'fontColor' => '98,98,98',        //字体颜色
    					'angle' => 0,                //倾斜角度
    				];
    				array_push($config['text'],$in_text);
    				// 诊疗信息下半部分
    				$in_image_down =
    					[
    						// 诊疗/擅长 信息下半部分
    						'url'       => "../public/assets/XXXX/121_03.png",
    						'stream'    => 0,
    						'left'      => 0,
    						'top'       => $this->bat_ga_image_top + 50,// 50是富裕高度
    						'right'     => 0,
    						'bottom'    => 0,
    						'width'     => 950,
    						'height'    => 65,
    						'opacity'   => 100,
    						'radius'    => 15,
    					];
    				if ($gadata == 1){
    					// 默认的高度是 65
    					$in_dat = [
    						// 诊疗信息头部
    						'url'       => "../public/assets/XXXX/13_02.png",
    						'stream'    => 0,
    						'left'      => 0,
    						'top'       => $this->bat_ga_image_top,
    						'right'     => 0,
    						'bottom'    => 0,
    						'width'     => 950,
    						'height'    => 52,
    						'opacity'   => 100,
    						'radius'    => 15,
    					];
    					// 背景图片的高 加上文字的高度 加上20 的富裕高度
    					$in_image_down['height'] = 65 + $this->dat_height + 20;
    					$this->bat_ga_image_down = $in_image_down['height'];
    					array_push($config['image'],$in_dat);
    				}else{
    					$in_ga = [
    						// 擅长信息头部
    						'url'       => "../public/assets/XXXX/14_02.jpg",
    						'stream'    => 0,
    						'left'      => 0,
    						'top'       => $this->bat_ga_image_top,
    						'right'     => 0,
    						'bottom'    => 0,
    						'width'     => 950,
    						'height'    => 52,
    						'opacity'   => 100,
    						'radius'    => 15,
    					];
    					// 背景图片的高 加上文字的高度 加上20 的富裕高度
    					$in_image_down['height'] = 65 + $this->dat_height + 20;
    					$this->bat_ga_image_down = $in_image_down['height'];
    					array_push($config['image'],$in_ga);
    					// 擅长信息头部图片不规整 调整一下
    					$in_image_down['left'] = 2;
    				}
    				array_push($config['image'],$in_image_down);
    				$this->brown_height = $this->brown_height + $this->bat_ga_image_down + 50;
    			}
    		}else{
    			if ($gaswitch == 1){
    				// 诊疗/擅长 头部 距离简介文字的高度
    				$in_text = [
    					// 擅长
    					'text' => $this->getbrtext(990, 22, 0, $fontPath, $gac,2), // 文字换行
    					'left' => 80,                //文字距离左边框多远(负数是从右往左)
    					'top' => 1090 + 105,            //文字距离上边框多远(负数是从下往上)
    					'fontPath' => $fontPath,        //字体文件
    					'fontSize' => 18,                //字号
    					'fontColor' => '98,98,98',        //字体颜色
    					'angle' => 0,                //倾斜角度
    				];
    				array_push($config['text'],$in_text);
    				// 诊疗信息下半部分 默认的高度是 $bat_ga_image_down 316
    				$in_image_down =
    					[
    						// 诊疗/擅长 信息下半部分
    						'url'       => "../public/assets/XXXX/121_03.png",
    						'stream'    => 0,
    						'left'      => 0,
    						'top'       => 1090 + 50,
    						'right'     => 0,
    						'bottom'    => 0,
    						'width'     => 950,
    						'height'    => 65,
    						'opacity'   => 100,
    						'radius'    => 15,
    					];
    				if ($gadata == 1){
    					// 默认的高度是 65
    					$in_dat = [
    						// 诊疗信息头部
    						'url'       => "../public/assets/XXXX/13_02.png",
    						'stream'    => 0,
    						'left'      => 0,
    						'top'       => 1090,
    						'right'     => 0,
    						'bottom'    => 0,
    						'width'     => 950,
    						'height'    => 52,
    						'opacity'   => 100,
    						'radius'    => 15,
    					];
    					// 背景图片的高 加上文字的高度 加上20 的富裕高度
    					$in_image_down['height'] = 65 + $this->dat_height + 20;
    					$this->bat_ga_image_down = $in_image_down['height'];
    					$this->brown_height = $this->brown_height + $this->dat_height + 50;
    					array_push($config['image'],$in_dat);
    
    				}else{
    					$in_ga = [
    						// 擅长信息头部
    						'url'       => "../public/assets/XXXX/14_02.jpg",
    						'stream'    => 0,
    						'left'      => 0,
    						'top'       => 1090,
    						'right'     => 0,
    						'bottom'    => 0,
    						'width'     => 950,
    						'height'    => 52,
    						'opacity'   => 100,
    						'radius'    => 15,
    					];
    					// 背景图片的高 加上文字的高度 加上20 的富裕高度
    					$in_image_down['height'] = 65 + $this->dat_height + 20;
    					$this->bat_ga_image_down = $in_image_down['height'];
    					$this->brown_height = $this->brown_height + $this->dat_height + 50;
    					array_push($config['image'],$in_ga);
    					// 图片不规整 调整一下
    					$in_image_down['left'] = 2;
    				}
    				array_push($config['image'],$in_image_down);
    			}
    		}
    		$filename = '/promote/'.time().'.jpg';
    		$file_path = ROOT_PATH.'/public'.$filename;
    		$file_dir = 'promote/';
    		// 判断是否有这个文件夹  没有的话就创建一个
    		if(!is_dir($file_dir)){
    			// 创建文件夹
    			mkdir($file_dir);
    		}
    		// 这里调用上面生成海报的根方法,将组装的参数和设置的图片名称传过去
    		$url = $this->createPoster($config,$file_path,$syswitch,$gaswitch,$gadata);
    		// 这里会直接输出海报图,在调试的时候开启使用
    //        echo "";
    		return $filename;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365

    生成海报图

    /**
    	 * 生成宣传海报
    	 * @param array  参数,包括图片和文字
    	 * @param string  $filename 生成海报文件名,不传此参数则不生成文件,直接输出图片
    	 * @return [type] [description]
    	 */
    	public function createPoster($config=[],$filename="",$syswitch,$gaswitch,$gadata)
    	{
    		//如果要看报什么错,可以先注释调这个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'=>32,       //字号
    			'fontColor'=>'255,255,255', //字体颜色
    			'angle'=>0,
    		);
    		$background = $config['background'];//海报最底层得背景
    		// 背景方法
    		// getimagesize() 函数用于获取图像大小及相关信息,成功返回一个数组,失败则返回 FALSE 并产生一条 E_WARNING 级的错误信息。
    		$backgroundInfo = getimagesize($background);
    		//  根据指定的图像类型返回对应的后缀名。
    		$backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
    		$background = $backgroundFun($background);
    		$backgroundWidth = imagesx($background);  // 返回原图背景宽度
    		$backgroundHeight = imagesy($background);  // 返回原图背景高度
    		if ($syswitch == 1){
    			$backgroundHeight = $this->brown_height;
    		}else{
    			if ($gaswitch == 1){
    				if ($gadata == 1){
    					$backgroundHeight = $this->brown_height;
    				}else{
    					$backgroundHeight = $this->brown_height;
    				}
    			}
    		}
    		// PHP中的内置函数,用于创建新的true-color图像。此函数返回给定尺寸的空白图像。
    		$imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
    		// PHP中的内置函数,用于设置图像的颜色。此函数返回以RGB格式给出的颜色。
    		$color = imagecolorallocate($imageRes, 0, 0, 0);
    		// PHP中的内置函数,用于用给定的颜色填充图像。此函数以给定的坐标(图像的左上角为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']);
    					// imagecreatefromstring 从字符串中的图像流新建一图像
    					$function = 'imagecreatefromstring';
    				}
    				$res = $function($val['url']);
    				$resWidth = $info[0];
    				$resHeight = $info[1];
    
    				//建立画板 ,缩放图片至指定尺寸
    				// PHP中的内置函数,用于创建新的true-color图像。此函数返回给定尺寸的空白图像。
    				$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'];
    				//2.上色
    				$color = imagecolorallocate($canvas,229,231,230);
    				//3.设置透明
    				imagecolortransparent($canvas,$color);
    
    				// 放置图像
    				// PHP中的内置函数,用于将图像复制并合并为单个图像。如果成功,则此函数返回True;如果失败,则返回False。
    				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'];
    				// PHP中的内置函数,用于使用TrueType字体将文本写入图像。
    				// imagettftextSp
    				// imagettftext
    				imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
    			}
    		}
    		
    		// 处理成就 单独设置
    		if(!empty($config['achieve'])){
    			for ($ac = 0;$ac < count($config['achieve']["text"]);$ac++) {
    				$text = $config['achieve']["text"];
    				list($R,$G,$B) = explode(',',$config['achieve']['fontColor']);
    				$fontColor = imagecolorallocate($imageRes, $R, $G, $B);
    				$config['achieve']['top'] = $config['achieve']['top'] + 50;
    				// PHP中的内置函数,用于使用TrueType字体将文本写入图像。
    				// imagettftextSp
    				// imagettftext
    				imagettftext($imageRes,$config['achieve']['fontSize'],$config['achieve']['angle'],$config['achieve']['left'],$config['achieve']['top'],$fontColor,$config['achieve']['fontPath'],$text[$ac]);
    			}
    		}
    		
    		// 成就文字前五角星
    		if (!empty($config['pentagram'])){
    			// $this->Pentagram
    			for ($i = 0;$i < $this->Pentagram * 2;$i++){
    				$iniptm = $config['pentagram'];
    				$inipentagram = $iniptm['url'];
    				// getimagesize() 函数用于获取图像大小及相关信息,成功返回一个数组,失败则返回 FALSE 并产生一条 E_WARNING 级的错误信息。
    				$morepentagram = getimagesize($inipentagram);
    				// 根据指定的图像类型返回对应的后缀名。
    				$morefunction = 'imagecreatefrom'.image_type_to_extension($morepentagram[2], false);
    				if($iniptm['stream']){   //如果传的是字符串图像流
    					// 从字符串中获取图像尺寸信息。
    					$inipentagram = getimagesizefromstring($iniptm['url']);
    					// imagecreatefromstring 从字符串中的图像流新建一图像
    					$morefunction = 'imagecreatefromstring';
    				}
    				$inipentagram = $morefunction($inipentagram);
    				$morewidth = $morepentagram[0]; // 返回当前图片宽度
    				$moreheight = $morepentagram[1]; // 返回当前图片高度
    				// 创建新的true-color图像。此函数返回给定尺寸的空白图像。
    				$moreimage = imageCreatetruecolor($iniptm['width'], $iniptm['height']);
    				// 用给定的颜色填充图像。此函数以给定的坐标(图像的左上角为0、0)开始执行填充。
    				imagefill($moreimage, 0, 0, $color);
    				// 关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
    				// 通过重采样复制和调整图像的一部分
    				imagecopyresampled($moreimage,$inipentagram, 0, 0, 0, 0, $iniptm['width'], $iniptm['height'],$morewidth,$moreheight);
    				$iniptm['left'] = $iniptm['left']<0?$backgroundWidth- abs($iniptm['left']) - $iniptm['width']:$iniptm['left'];
    				$iniptm['top'] = $iniptm['top']<0?$backgroundHeight- abs($iniptm['top']) - $iniptm['height']:$iniptm['top'];
    				//2.上色
    				$color = imagecolorallocate($moreimage,229,231,230);
    				//3.设置透明
    				imagecolortransparent($moreimage,$color);
    				// 放置图像
    				// PHP中的内置函数,用于将图像复制并合并为单个图像。如果成功,则此函数返回True;如果失败,则返回False。
    				// imagecopy
    				// imagecopymerge($imageRes,$moreimage, $iniptm['left'],$iniptm['top'],$iniptm['right'],$iniptm['bottom'],$iniptm['width'],$iniptm['height'],$iniptm['opacity']);//左,上,右,下,宽度,高度,透明度
    				imagecopy($imageRes,$moreimage, $iniptm['left'],$iniptm['top'] + $i++ * 24.5,$iniptm['right'],$iniptm['bottom'],$iniptm['width'],$iniptm['height']);//左,上,右,下,宽度,高度,透明度
    			}
    		}
    		//生成图片
    		if(!empty($filename)){
    			// imagegif():以 GIF 格式将图像输出到浏览器或文件
    			// imagejpeg():以 JPEG 格式将图像输出到浏览器或文件
    			// imagepng():以 PNG 格式将图像输出到浏览器或文件
    			// imagewbmp():以 WBMP 格式将图像输出到浏览器或文件
    			$res = imagejpeg ($imageRes,$filename,90); //保存到本地
    			// 销毁图像资源
    			imagedestroy($imageRes);
    			if(!$res) return false;
    			return $filename;
    		}else{
    			imagejpeg ($imageRes);     //在浏览器上显示
    			imagedestroy($imageRes);
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177

    其他方法

    	// 因为文字表情无法显示,提供一个过滤表情的方法
    	// 过滤掉emoji表情,在参数组装时调用,过滤掉表情
    	function filterEmoji($str)
    	{
    		$str = preg_replace_callback('/./u', function (array $match) {
    			return strlen($match[0]) >= 4 ? '' : $match[0];
    		}, $str);
    		return $str;
    	}
    	// 文字换行
    	public function getbrtext($width = 200, $size = 16, $angle = 0, $font = '../public/assets/fonts/5c8a05a7b3b641552549287.otf', $text = '',$type = '') {
    		$hang = 1;
    		//判断是否超出范围
    		$str = "";
    		for ($i = 0; $i < mb_strlen($text); $i++) {
    			$letter[] = mb_substr($text, $i, 1);
    
    		}
    		foreach ($letter as $l) {
    			$teststr = $str . " " . $l;
    			$testbox = imagettfbbox($size, $angle, $font, $teststr);
    			// 判断拼接后的字符串是否超过预设的宽度。超出宽度添加换行
    			if (($testbox[2] > $width) && ($str !== "")) {
    				$str .= "\n";
    				$hang++;
    			}
    			$str .= $l;
    		}
    		if ($type == 1){
    			// 获取简介文本高度
    			// 行数
    			$this->textRows = $hang;
    			// 文字高
    			$this->summary_height = $this->text_height($str,2,$hang);;
    
    		}elseif ($type == 2){
    			$this->bat_ga_textRows = $hang;
    			// 获取诊疗文本高度
    			$this->dat_height = $this->text_height($str,2,$hang);
    		}else{
    			// 获取擅长文本高度
    			$this->good_at_height = $this->text_height($str,2,$hang);
    		}
    		//返回
    		return $str;
    	}
    	// 获取文本的高度
    	public function text_height ($text,$type,$hang) {
    		$size = 12;//font height
    		$box = $this->get_bbox(22,0,ROOT_PATH.'/public/assets/fonts/5c8a05a7b3b641552549287.otf',$text);
    		if ($type == 1){
    			$height = $box[3] - $box[5];
    		}else{
    			$width = abs($box[4] - $box[0]) + 10;
    			$height = (35 * $hang);
    		}
    		return $height;
    	}
    
    	// 用于计算TrueType文本的边框(以像素为单位)。
    	public function get_bbox($size = 16,$angle = 0,$font = ROOT_PATH.'/public/assets/fonts/5c8a05a7b3b641552549287.otf',$text = ''){
    		/*
    		 * imagettfbbox() 返回一个含有 8 个单元的数组表示了文本外框的四个角:
    		 * 0 左下角 X 位置
    		 * 1 左下角 Y 位置
    		 * 2 右下角 X 位置
    		 * 3 右下角 Y 位置
    		 * 4 右上角 X 位置
    		 * 5 右上角 Y 位置
    		 * 6 左上角 X 位置
    		 * 7 左上角 Y 位置
    		*/
    		return imagettfbbox($size, $angle, $font, $text);
    	}
    	// 字体间距
    	public function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0)
    	{
    		if ($spacing == 0)
    		{
    			imagettftext($image, $size, $angle, $x, $y, $color, $font, $text);
    		}
    		else
    		{
    			$temp_x = $x;
    			for ($i = 0; $i < strlen($text); $i++)
    			{
    				$bbox = imagettftext($image, $size, $angle, $temp_x, $y, $color, $font, $text[$i]);
    				$temp_x += $spacing + ($bbox[2] - $bbox[0]);
    			}
    		}
    	}
    
    	/*
    	 *去除html标签,并截取指定个字符
    	 * $contents 内容
    	 * $number 字数
    	 */
    	public function tagecontent($contents,$number='85'){
    
    		$tagecontent = strip_tags($contents);
    		$pattern = '/\s/';//去除空白
    		$content = preg_replace($pattern, '', $tagecontent);
    		$list = mb_substr($content,0,$number, "UTF-8");
    		return $list;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105

    将就看
    最后在后台下载图片,
    在操作列添加下载按钮

    buttons: [
                                    {
                                        name: 'ajax',
                                        title: __('发送Ajax'),
                                        classname: 'btn btn-xs btn-primary btn-magic btn-ajax',
                                        icon: 'fa fa-cloud-download',
                                        confirm: '确认发送下载海报图请求?',
                                        url: 'Poster/downloadImg/downloadImg/ids',
                                        success: function (data, ret) {
    
                                            var a = document.createElement('a');          // 创建一个a节点插入的document
                                            var event = new MouseEvent('click')           // 模拟鼠标click点击事件
                                            a.download = 'beautifulGirl'                  // 设置a节点的download属性值
                                            a.href = data;                                 // 将图片的src赋值给a节点的href
                                            a.dispatchEvent(event)
                                            //如果需要阻止成功提示,则必须使用return false;
                                            //return false;
                                        },
                                        error: function (data, ret) {
                                            console.log(data, ret);
                                            Layer.alert(ret.msg);
                                            return false;
                                        }
                                    },
                                ],
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    /**
    	 * 下载图片
    	 */
    	public function downloadImg($ids){
    		if (!$this->request->isAjax()){
    			$this->error('错误的信息');
    		}
    		$row = $this->model->get(['id' => $ids]);
    		if($row['poster_image']){
    			$poster_image = $this->request->domain() . $row['poster_image'];
    			$this->success("请求成功", null,$poster_image);
    		}else{
    			$this->error(__('No Results were found'));
    		}
    
    
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    修改的时候判断是否有修改数据,先获取原数据,在使用 array_diff_assoc 对比提交的数据
    array_diff:比较两个数组的值,并返回差集
    array_diff_assoc:比较两个数组的键名和键值,并返回差集

  • 相关阅读:
    解构 赋值
    代码演示傅里叶合成演示
    chk文件怎么恢复?chk文件恢复软件哪个好?
    打印机连接网络后怎么安装驱动?
    【LeetCode】《LeetCode 101》第十三章:链表
    详解vue中中localStorage的使用方法
    【电源专题】分流监控器(电流感测放大器)
    基于Dockerfile搭建LNMP
    如何在Vue中进行路由管理?如何使用Vue Router?
    C++数据结构之链表栈
  • 原文地址:https://blog.csdn.net/weixin_43018356/article/details/126439571