//利用tp5中的图像处理功能完成海报的图片合成
1.首先需要一种空白的地图,为了防止锁图 我们将白底图片放在阿里云上 访问下载到服务器上使用完成后再删除
public function headimg(){
$post = $this->request->param();
$img_url = 'https://huiquan---.com/857/mall/shop-203/servicer/0394ea68951e3299bcdfa75a097d7c11.jpg?x-oss-process=image/resize,m_fill,limit_0,w_100,h_100';
$data = file_get_contents($img_url);//获取到阿里云上的图片
file_put_contents(WEB_ROOT.'upload/composite/50.jpg', $data);//将其存到本地
$image = Image::open(WEB_ROOT.'upload/composite/50.jpg');//打开第一张图片
$image->thumb(500,750,Image::THUMB_SOUTHEAST)->save(WEB_ROOT.'upload/composite/50.jpg');//设置第一张图片的宽高
//获取到商品图片
$model_MallShopPic = new \app\model\MallShopPic;
$goodspic = $model_MallShopPic->where(['sid'=>SID,'id'=>$post['pic_id']])->find()->toArray();
$goodspic['path'] = getObject($goodspic['path'],480,480);
$dataone = file_get_contents($goodspic['path']);
file_put_contents(WEB_ROOT.'upload/composite/1.png', $dataone);
//二维码图
//生成二维码
$url = 'http://www.'.Y('site')['domain']."/index/mall/detail/?goods_id=".$post['id']; //推广链接
$QR = WEB_ROOT.'upload/composite/5.jpg'; //临时生成的二维码文件(流程结束后删除)
$QRcode = new \app\common\Qrcode;//利用Qrcode生成二维码
$errorCorrectionLevel = "H"; // 纠错级别:L、M、Q、H
$matrixPointSize = 3; //生成图片大小 :1到10
ob_clean();
$QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 2);//保存到本地后面在拿出来一起合成
//打开一张图片5.jpg就是二维码图片将其裁剪成150*150
$image = Image::open(WEB_ROOT.'upload/composite/5.jpg');
$image->thumb(150,150,Image::THUMB_SOUTHEAST)->save(WEB_ROOT.'upload/composite/5.jpg');
//合成
$image = Image::open(WEB_ROOT.'upload/composite/50.jpg');//打开背景图片
$image->water(WEB_ROOT.'upload/composite/1.png',[10,10])->water(WEB_ROOT.'upload/composite/5.jpg',[340,550])->save(WEB_ROOT.'upload/composite/100.jpg');
//判断标题是否超出字数
if(strlen($goods['title'])>54){
$goods['title'] = substr($goods['title'],0,54);
$goods['title'] .= '...';
}
//价格
$price = $goods['sku_info']['default']['price'];
$lengths = 100 + (strlen($price)*5);
$crossbar = 110 + (strlen($price)*5);
//判断店铺名称是否超出字数
$model_User = new \app\model\User;
$shopname = $model_User->where('uid',UID)->value('username');
if(strlen($shopname)>36){
$shopname = substr($shopname,0,36);
$shopname .= '...';
}
//原价
if(!empty($goods['sku_info']['default']['promotion_price'])){
$image->text($goods['title'],'upload/composite/fanzheng.TTF',18,'#000000',[8,500])
->text('¥'.$price,'upload/composite/fanzheng.TTF',18,'#FF0000',[2,545])
->text('¥'.$goods['sku_info']['default']['promotion_price'],'upload/composite/fanzheng.TTF',18,'#AAAAAA',[$lengths,545])
->text('——','upload/composite/fanzheng.TTF',18,'#AAAAAA',[$crossbar,553])
->text($shopname.'的小店','upload/composite/fanzheng.TTF',18,'#000000',[10,585])
->text('长按识别二维码,查看宝贝','upload/composite/fanzheng.TTF',13,'#AAAAAA',[10,625])
->text('价格和促销以商品详情页为准','upload/composite/fanzheng.TTF',13,'#AAAAAA',[10,645])
->save('upload/composite/100.jpg');
}else{
$image->text($goods['title'],'upload/composite/fanzheng.TTF',18,'#000000',[8,500])
->text('¥'.$price,'upload/composite/fanzheng.TTF',18,'#FF0000',[2,545])
->text($shopname.'的小店','upload/composite/fanzheng.TTF',18,'#000000',[10,585])
->text('长按识别二维码,查看宝贝','upload/composite/fanzheng.TTF',13,'#AAAAAA',[10,625])
->text('价格和促销以商品详情页为准','upload/composite/fanzheng.TTF',13,'#AAAAAA',[10,645])
->save('upload/composite/100.jpg');
}
$path = SID.'/user/'.UID.'/posters/'.$post['id'].'/'.$post['pic_id'].time().'.jpg';
$this->uploadObject(WEB_ROOT.'upload/composite/100.jpg',$path);
//保存操作
unlink(WEB_ROOT.'upload/composite/50.jpg');
unlink(WEB_ROOT.'upload/composite/1.png');
unlink(WEB_ROOT.'upload/composite/5.jpg');
unlink(WEB_ROOT.'upload/composite/100.jpg');
!file_exists($QR) or unlink($QR);//清除二维码路径
$this->showMessage('海报保存成功',1,'/usercenter/userinfo/myposter');
}else{
$this->showMessage('请登录');
}
}