IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> php海报生成 -> 正文阅读

[PHP知识库]php海报生成

代码

composer require endroid/qr-code
<?php
namespace App\Service;

use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;

class ImageService{

	public static function create($config = [], $filename = ''){
		$imageDefault = array(
			'left'    => 0,
			'top'     => 0,
			'width'   => 100,
			'height'  => 100,
			'opacity' => 100,
			'circle'  => 0
		);
		$qrcodeDefault = [
			'text'   => 'qrcode content',
			'size'   => 100,
			'margin' => 2,
			'left'   => 0,
			'top'    => 0,
			'color'  => '0,0,0'
		];
		$textDefault =  array(
			'text'      => '',
			'left'      => 0,
			'top'       => 0,
			'align'		=> '',
			'font-size' => 32,//字号
			'color'     => '0,0,0', //字体颜色
			'angle'     => 0,
			'font-path'  => RUNTIME_PATH . '/font/SourceHanSansCN-Regular.otf' //字体
		);

		$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['qrcode'])){
			foreach ($config['qrcode'] as $item){
				$item = array_merge($qrcodeDefault, $item);
				list($red,$green,$blue) = explode(',', $item['color']);
				$qrcode = Builder::create()
					->writer(new PngWriter())
					->writerOptions([])
					->data($item['text'])
					->encoding(new Encoding('UTF-8'))
					->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
					->size($item['size'])
					->foregroundColor(new Color(intval($red), intval($green), intval($blue)))
					->margin($item['margin'])
					->roundBlockSizeMode(new RoundBlockSizeModeMargin())
					->build();
				$baseImage = imagecreatefromstring($qrcode->getString());
				imagecopymerge($imageRes, $baseImage, $item['left'],$item['top'],0,0, $item['size'], $item['size'],100);
			}
		}
		//处理了图片
		if(!empty($config['image'])){
			foreach ($config['image'] as $key => $val) {
				$val = array_merge($imageDefault,$val);

				$info = getimagesize($val['url']);
				if($val['circle']){		//如果传的是字符串图像流
					$canvas = self::imageRadius($val['url'], $val['width']/2);
				}else{
					$function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
					$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'], 0, 0, $val['width'], $val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
				imagedestroy($canvas);
			}
		}

		//处理文字
		if(!empty($config['text'])){
			foreach ($config['text'] as $key => $val) {
				$val = array_merge($textDefault,$val);
				list($R,$G,$B) = explode(',', $val['color']);
				$color = imagecolorallocate($imageRes, $R, $G, $B);
				$arr = imagettfbbox($val['font-size'],0,$val['font-path'],$val['text']);
				$text_height = $arr[3] - $arr[5];
				if ($val['align'] == 'center'){
					$text_width = $arr[2]-$arr[0];
					$val['left'] = ($backgroundWidth - $text_width)/2;
				}else{
					$val['left'] = $val['left'] < 0 ? $backgroundWidth- abs($val['left']):$val['left'];
				}
				$val['top'] = $val['top'] < 0 ? $backgroundHeight- abs($val['top']) : $val['top'];
				$val['top'] += $text_height;
				imagettftext($imageRes,$val['font-size'],$val['angle'],$val['left'],$val['top'],$color,$val['font-path'],$val['text']);
			}
		}
		
		//生成图片
		if(!empty($filename)){
			$res = imagepng($imageRes, RUNTIME_PATH.'/'. $filename,90); //保存到本地
			imagedestroy($imageRes);
			if(!$res) return false;
			return $filename;
		}else{
			ob_start();
			imagepng($imageRes);//在浏览器上显示
			$string = strval(ob_get_contents());
			ob_end_clean();
			return 'data:image/png;base64,'.base64_encode($string);

		}
	}


	/**
	 * 生成圆形
	 * @param $image 图片地址
	 * @param null $radius 半径
	 * @return false|\GdImage|resource
	 */
	public static function imageRadius($image, $radius = null){
		/**
		 * 处理圆形图
		 * @param $image 图片地址
		 * @return string
		 */
		$logo = imagecreatefromstring(file_get_contents($image));//源图象连接资源。
		$height = $width = min(imagesx($logo), imagesy($logo));
		if ($radius && $width / 2 < $radius){
			$new_w	= $new_h = $radius * 2;
			$new	= imagecreatetruecolor($new_w, $new_h);
			imagecopyresized($new, $logo,0, 0,0, 0, $new_w, $new_h, $width, $height);
			$width  = $height = $new_w;
			$logo	= $new;
		}

		//创建一个和二维码图片一样大小的真彩色画布
		$canvas = imagecreatetruecolor($width, $height);
		$color = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
		imagesavealpha($canvas, true);
		imagealphablending($canvas, false);
		imagefill($canvas, 0, 0, $color);
		imageColorTransparent($canvas, $color);
		$r = $width / 2;   //半径
		for ($x = 0; $x < $width; $x++) {
			for ($y = 0; $y < $height; $y++) {
				$rgb_color = imagecolorat($logo, $x, $y);
				if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r) < ($r * $r)))) {
					imagesetpixel($canvas, $x, $y, $rgb_color);
				}
			}
		}
		imagedestroy($logo);

		return $canvas;
	}
}


用法

$config = array(
			'image'=>array(
				array(
					'url'     => 'https://img.w7.cc/07/cb/0e/e6/02/33/59/b7/42/e2/5b/d4/a5/04/09/c0.png',//图片资源
					'left'    => 280,
					'top'     => 445,
					'width'   => 190,
					'height'  => 190,
					'opacity' => 100,
					'circle'  => 1
				)
			),
			'qrcode' => [
				[
					'text'   => 'qrcode content',
					'size'   => 176,
					'margin' => 2,
					'left'   => 98,
					'top'    => 406,
				]
			],
			'text' => [
				[
					'text'      => '一言网络科技有限公司',
					'font-size' => 16,
					'align'		=> 'center',
					'top'       => 355,
				]
			],
			'background'=> '/shop_poster_bg.png'//背景图
		);
		$src = App\Model\Service\ImageService::create($config);

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-08-24 15:19:43  更:2021-08-24 15:20:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 22:25:31-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码