```php
$config=['background'=>'海報背景圖',
'image'=>[
['src' => '圖片地址1', 'left' => '距左邊距', 'top' => '距上邊距', 'opacity' => '透明度 0.1-1', 'width' =>'寬度', 'height' => '高度']
,
['src' => '圖片地址2', 'left' => '距左邊距', 'top' => '距上邊距', 'opacity' => '透明度 0.1-1', 'width' =>'寬度', 'height' => '高度']
],
'text'=>[
['text'=>'文字內容1', 'left' => '距左邊距', 'top' => '距上邊距','fontSize'=>'字號','transform'=>'字體角度','fontPath'=>'文字字體路徑'],
['text'=>'文字內容2', 'left' => '距左邊距', 'top' => '距上邊距','fontColor'=>'字體顏色','transform'=>'字體角度','fontPath'=>'文字字體路徑']
]];
/**
* 創建海報
* @param array $config 生成海報的配置
* @param string $filename 生成后保存地址
* @return bool|string
*/
function createPoster($config = array(), $filename = "")
{
//如果要看報什么錯,可以先注釋調這個header
ini_set('default_socket_timeout', 1);
if (empty($filename)) header("content-type: image/png");
$imageDefault = [
'left' => 0,
'top' => 0,
'right' => 0,
'bottom' => 0,
'width' => 100,
'height' => 100,
'opacity' => 100
];
$textDefault = [
'text' => '',
'left' => 0,
'top' => 0,
'fontSize' => 32, //字號
'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 = $backgroundInfo[0]; //背景寬度
$backgroundHeight = $backgroundInfo[1]; //背景高度
$imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
$color = imagecolorallocate($imageRes, 0, 0, 0);
//imageColorTransparent($imageRes, $color); //顏色透明
imagefill($imageRes, 0, 0, $color);
imagecopyresampled($imageRes, $background, 0, 0, 0, 0, $backgroundInfo[0], $backgroundInfo[1], imagesx($background), imagesy($background));
//處理了圖片
if (!empty($config['image'])) {
foreach ($config['image'] as $key => $val) {
$val = array_merge($imageDefault, $val);
if (strpos($val['src'], 'http://thirdwx.qlogo.cn') === false) {
$info = getimagesize($val['src']);
$function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
if (!empty($val['stream'])) { //如果傳的是字符串圖像流
$info = getimagesizefromstring($val['src']);
$function = 'imagecreatefromstring';
}
$res = $function($val['src']);
$resWidth = $info[0];
$resHeight = $info[1];
//建立畫板 ,縮放圖片至指定尺寸
$canvas = imagecreatetruecolor($val['width'], $val['height']);
imageColorTransparent($canvas, $color);
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);
} else {
$res = imagecreatefromjpeg($val['src']);
$resWidth = 132;
$resHeight = 132;
$canvas = imagecreatetruecolor($val['width'], $val['height']);
imageColorTransparent($canvas, $color);
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'] * 100);//左,上,右,下,寬度,高度,透明度
}
}
//處理文字
if (!empty($config['text'])) {
foreach ($config['text'] as $key => $val) {
$val['fontPath'] = '/static/index/simhei.ttf';
$val = array_merge($textDefault, $val);
if (strpos('rgb', $val['color']) === false) {
$rgb = self::hex2rgb($val['color']);
$fontColor = imagecolorallocate($imageRes, $rgb['r'], $rgb['g'], $rgb['b']);
} else {
$val['color'] = str_replace('rgb(', '', $val['color']);
$val['color'] = str_replace(')', '', $val['color']);
list($R, $G, $B) = explode(',', $val['color']);
$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['font_size'], -$val['transform'], $val['left'], $val['top'] + $val['font_size'], $fontColor, $val['fontPath'], $val['text']);
}
}
//生成圖片
if (!empty($filename)) {
$res = imagepng($imageRes, $filename); //保存到本地
imagedestroy($imageRes);
if (!$res) return false;
return $filename;
} else {
imagepng($imageRes); //在瀏覽器上顯示
imagedestroy($imageRes);
}
}
//直接調用即可使用
createPoster($config,'文件保存地址');
```
> 如果您的PHP沒有GD庫,請看下面安裝GD庫
liunx安裝教程
[安裝教程](https://www.php.cn/php-weizijiaocheng-329608.html)
Windows安裝教程
把你PHP目錄下的ext文件夾里的php_gd2.dll復制到系統目錄下,然后修改你的PHP.ini文件,找到以下位置
;extension=php_gd2.dll
把前面的;去掉....
重啟你的網頁服務器apache或者IIS就可以了