diff --git a/01.jpg b/01.jpg new file mode 100644 index 0000000..fbd8243 Binary files /dev/null and b/01.jpg differ diff --git a/01.ttf b/01.ttf new file mode 100644 index 0000000..4aaa8d4 Binary files /dev/null and b/01.ttf differ diff --git a/02.jpg b/02.jpg new file mode 100644 index 0000000..f8e9b6d Binary files /dev/null and b/02.jpg differ diff --git a/02.ttf b/02.ttf new file mode 100644 index 0000000..1a19b2b Binary files /dev/null and b/02.ttf differ diff --git a/Img.php b/Img.php new file mode 100644 index 0000000..8f66248 --- /dev/null +++ b/Img.php @@ -0,0 +1,174 @@ +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); + } + + +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..803d797 --- /dev/null +++ b/index.php @@ -0,0 +1,25 @@ +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();