<?php /** * @desc 图片合成代码逻辑示例 * 需要用到TP框架、文字处理函数,微信登录判断等,仅作自我记录参考 * @usage $MakeShareImg = new MakeShareImg($name,$subname,$content,$mimg); $shareImg = $MakeShareImg->getShareImg(); * @author Deron */ class MakeShareImg { //用户名 private $username; //地铁站名 private $subname; //内容 private $content; //内容组 private $contentArr; //头像 private $avatar; //画布 private $canvas; //分享图片 private $shareimg; public $wxLogin; /** * @desc 构造函数 * @param string $username 用户名称 * @param string $subname 地铁站名称 * @param string $content 内容 * @param string $avatar 头像地址 */ public function __construct($username,$subname,$content,$avatar) { $this->username = $username; $this->subname = $subname; $this->content = $content; $this->avatar = $avatar; } /** * @desc 获取分享图片路径 */ public function getShareImg() { //初始化数据 $this->init(); //合成图片 if($this->wxLogin) {$this->meregimgdefault();} else $this->mergeimg(); //返回合成后的图片 return $this->shareimg; } /** * @desc 初始化 */ public function init() { //处理画布 $dir = 'xxxxxxxxxxxxxxxxx'; switch($this->subname) { case 'xxx1': if($this->wxLogin) { $this->canvas = $dir . 'canvas_jjk_default.png'; } else { $this->canvas = $dir . 'canvas_jjk.png'; } break; case 'xxx2': if($this->wxLogin) { $this->canvas = $dir . 'canvas_cc_default.png'; } else { $this->canvas = $dir . 'canvas_cc.png'; } break; case 'xxx3': if($this->wxLogin) { $this->canvas = $dir . 'canvas_mn_default.png'; } else { $this->canvas = $dir . 'canvas_mn.png'; } break; case 'xxx4': if($this->wxLogin) { $this->canvas = $dir . 'canvas_zc_default.png'; } else { $this->canvas = $dir . 'canvas_zc.png'; } break; case 'xxx5': if($this->wxLogin) { $this->canvas = $dir . 'canvas_flm_default.png'; } else { $this->canvas = $dir . 'canvas_flm.png'; } break; case 'xxx6': if($this->wxLogin) { $this->canvas = $dir . 'canvas_zl_default.png'; } else { $this->canvas = $dir . 'canvas_zl.png'; } break; default: $this->canvas = ''; } //处理内容 $this->contentArr[0] = csubstr($this->content,0,15,false); $this->contentArr[1] = csubstr($this->content,15,15,false); $this->contentArr[2] = csubstr($this->content,30,15,false); //排除截取字符串方法的BUG if($this->contentArr[0] == $this->contentArr[1] && $this->contentArr[0] == $this->contentArr[2]) { $this->contentArr[1] = ''; $this->contentArr[2] = ''; } } private function meregimgdefault() { //获取图像信息 $canvasInfo = $this->getImageInfo($this->canvas); //建立图像 $canvasCreateFun = "imagecreatefrom" . $canvasInfo['type']; $canvasImage = $canvasCreateFun($this->canvas); //载入字体 $font = "./msyh.ttf"; //创建颜色,用于文字字体的白和阴影的黑 $ufcolor = imagecolorallocate($canvasImage,255,255,255); //用户名 $cfcolor = imagecolorallocate($canvasImage,91,44,46); //计算文字位置 $fsize = 16; $fangle = 0; $utop = 85; $uleft = 540; $ctop = 180; $cleft = 540; //在图片中插入文字,imagettftext (image,size,angle, x, y,color,fontfile,text) 图像,字形尺寸,字形角度,x坐标,y坐标,颜色,字体文件名称,字符串 if($this->username) imagettftext($canvasImage,$fsize,$fangle,$uleft,$utop,$ufcolor,$font,$this->username); if($this->contentArr[0]) imagettftext($canvasImage,$fsize,$fangle,$cleft,$ctop-5,$cfcolor,$font,$this->contentArr[0]); if($this->contentArr[1]) imagettftext($canvasImage,$fsize,$fangle,$cleft,$ctop+35,$cfcolor,$font,$this->contentArr[1]); if($this->contentArr[2]) imagettftext($canvasImage,$fsize,$fangle,$cleft,$ctop+70,$cfcolor,$font,$this->contentArr[2]); //设定图像的保持透明色 imagealphablending($canvasImage, false); imagesavealpha($canvasImage,true); //输出图像 $ImageFun = 'Image' . $canvasInfo['type']; //保存图片路径 $savepath = 'xxxxxxxxxxxxxxxxx'; //生成的图像名称 $savename = 'default_flm_img.' .$canvasInfo['type']; //保存图像 $ImageFun($canvasImage, $savepath.$savename); //销毁对象 imagedestroy($canvasImage); $this->shareimg = $savepath.$savename; } /** * @desc 合成图片 */ private function mergeimg() { //获取图像信息 $canvasInfo = $this->getImageInfo($this->canvas); //建立图像 $canvasCreateFun = "imagecreatefrom" . $canvasInfo['type']; $canvasImage = $canvasCreateFun($this->canvas); //载入字体 $font = "./msyh.ttf"; //创建颜色,用于文字字体的白和阴影的黑 $ufcolor = imagecolorallocate($canvasImage,255,255,255); //用户名 $cfcolor = imagecolorallocate($canvasImage,91,44,46); //计算文字位置 $fsize = 16; $fangle = 0; $utop = 85; $uleft = 540; $ctop = 180; $cleft = 540; //在图片中插入文字,imagettftext (image,size,angle, x, y,color,fontfile,text) 图像,字形尺寸,字形角度,x坐标,y坐标,颜色,字体文件名称,字符串 if($this->username) imagettftext($canvasImage,$fsize,$fangle,$uleft,$utop,$ufcolor,$font,$this->username); if($this->contentArr[0]) imagettftext($canvasImage,$fsize,$fangle,$cleft,$ctop-5,$cfcolor,$font,$this->contentArr[0]); if($this->contentArr[1]) imagettftext($canvasImage,$fsize,$fangle,$cleft,$ctop+35,$cfcolor,$font,$this->contentArr[1]); if($this->contentArr[2]) imagettftext($canvasImage,$fsize,$fangle,$cleft,$ctop+70,$cfcolor,$font,$this->contentArr[2]); //获取头像图像信息 $avatarInfo = $this->getImageInfo($this->avatar); //判断头像是否符合标准 if($avatarInfo["width"] > $canvasInfo["width"] || $avatarInfo["height"] > $canvasInfo["height"]) exit("图片大小不符合"); //建立头像图像 $avatarCreateFun = "imagecreatefrom" . $avatarInfo['type']; $avatarImage = $avatarCreateFun($this->avatar); $aalpha = 90; $posX = 40; $posY = 50; //生成混合图像 imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct ) //源图像,添加的图像,源图像x坐标,源图像y坐标,添加的图片的x左边,添加的图片的y坐标,添加的图片的宽度,添加的图片的高度,角度 imagecopymerge($canvasImage, $avatarImage, $posX, $posY, 0, 0, $avatarInfo['width'], $avatarInfo['height'], $aalpha); //设定图像的保持透明色 imagealphablending($canvasImage, false); imagesavealpha($canvasImage,true); //输出图像 $ImageFun = 'Image' . $canvasInfo['type']; //保存图片路径 $savepath = 'xxxxxxxxxxxxxxxxx'; //生成的图像名称 $savename = substr(substr($this->avatar,21),0,-4). '.' .$canvasInfo['type']; //保存图像 $ImageFun($canvasImage, $savepath.$savename); //销毁对象 imagedestroy($canvasImage); imagedestroy($avatarImage); $this->shareimg = $savepath.$savename; } /** * @desc 获取图片信息 */ privatefunction getImageInfo($img) { $imageInfo = getimagesize($img); if ($imageInfo !== false) { $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1)); $imageSize = filesize($img); $info = array( "width" => $imageInfo[0], "height" => $imageInfo[1], "type" => $imageType, "size" => $imageSize, "mime" => $imageInfo['mime'] ); return $info; } else { return false; } } } ?>