第一次文件上传

第一次文件上传
This commit is contained in:
2021-10-25 15:47:43 +08:00
committed by GitHub
parent 78108c22f4
commit 81878deec7
6 changed files with 199 additions and 0 deletions

BIN
01.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

BIN
01.ttf Normal file

Binary file not shown.

BIN
02.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

BIN
02.ttf Normal file

Binary file not shown.

174
Img.php Normal file
View File

@@ -0,0 +1,174 @@
<?php
class Img
{
private $pic;
private $backGroundPicPath;
private $backGroundPic;
private $fontPath;
private $width; // 宽
private $height; // 高
private $type;
private $colorArray = [];
public $UserName;
public $ToUserName;
public function __construct()
{
}
/**
* 设置字体
* @param $fontPath
*/
public function SetFont($fontPath)
{
$this->fontPath = $fontPath;
}
/**
* 设置背景图
* @param $backGroundPicPath
*/
public function SetBackGroundPic($backGroundPicPath)
{
$this->backGroundPicPath = $backGroundPicPath;
}
/**
* 设置大小
* @param $width
* @param $height
*/
public function SetSize($width, $height)
{
$this->width = $width;
$this->height = $height;
}
private function CreateColor()
{
switch ($this->type) {
case 1:
$this->colorArray['black'] = imagecolorallocate($this->pic, 1, 1, 1); //颜色
$this->colorArray['blue'] = imagecolorallocatealpha($this->pic, 45, 100, 179, 0.96);
break;
case 2:
$this->colorArray['white'] = imagecolorallocatealpha($this->pic, 255, 255, 255, 41);
break;
default:
break;
}
}
private function WriteText()
{
switch ($this->type) {
case 1:
imagettftext($this->pic, 14, 0, 23, 164, $this->colorArray['black'], $this->fontPath, $this->UserName);
imagettftext($this->pic, 14, 0, 180, 330, $this->colorArray['blue'], $this->fontPath, $this->ToUserName);
break;
case 2:
imagettftext($this->pic, 60, 0, 190, 500, $this->colorArray['white'], $this->fontPath, $this->ToUserName);
break;
default:
break;
}
}
public function Start($type = 1)
{
$this->type = $type;
$this->CreatePic();
$this->ReadBackGroundPic();
$this->ImgCopy();
$this->CreateColor();
$this->WriteText();
}
/**
* Base64输出
*/
public function OutputBase64()
{
ob_start();
imagejpeg($this->pic);
$imageData = ob_get_contents();
$base64Image = base64_encode($imageData);
ob_end_clean();
$this->destroyImage();
return 'data:image/jpeg;base64,' . $base64Image;
}
/**
* http输出
*/
public function OutputHttpImage()
{
header("content-type:image/jpeg");
imagejpeg($this->pic);
$this->destroyImage();
}
/**
* 保存到本地
*/
public function OutputLocalImage($path='')
{
if (!$path) {
$path = date('YmdHis') . ".jpeg";
}
imagejpeg($this->pic, $path);
$this->destroyImage();
return $path;
}
/**
* 销毁图片
*/
private function destroyImage()
{
imagedestroy($this->pic);
}
private function ImgCopy()
{
imagecopy($this->pic, $this->backGroundPic, 0, 0, 0, 0, $this->width, $this->height);
imagedestroy($this->backGroundPic);
}
/**
*
*/
private function ReadBackGroundPic()
{
$this->backGroundPic = imagecreatefromjpeg($this->backGroundPicPath);
}
/**
*
*/
private function CreatePic()
{
$this->pic = imagecreatetruecolor($this->width, $this->height);
}
}

25
index.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
include 'Img.php';
$obj = new Img();
$obj->UserName = "";
$obj->ToUserName = "";
//$obj->SetFont('01.ttf');
//$obj->SetBackGroundPic('01.jpg');
//$obj->SetSize(393, 708);
//$obj->Start(1);
$obj->SetFont('02.ttf');
$obj->SetBackGroundPic('02.jpg');
$obj->SetSize(580, 774);
$obj->Start(2);
//$obj->OutputHttpImage();
echo $obj->OutputLocalImage();
//echo $obj->OutputBase64();