#网站资源推广生成小程序二维码推广海报,原封面图片被拉伸变形,寻找猿员优化源码,让图片自适应变化环境。
状况图例:


源码如下:(优化部分请用红框区别)
/**
* 生成推广海报
*/
public function posterUrl()
{
if ($this->request->isAjax()) {
$type = $this->request->param('type');
$path = 'uploads/poster';
$filename = 'spread_agent_' . $type . '_' . $this->admin_uid . '.png';
#获取自定义海报数据
$setting = [
'url' => $this->request->domain() . '/static/common/images/poster_agent.jpg',
'bom_text' => '现在永远是好的创业时机',
'bom_text_size' => '16',
'bom_text_color' => 'rgb(0,0,0)'
];
$config = PosterConfig::where(['admin_id' => $this->admin_uid, 'type' => 3])->find();
if (!empty($config)) {
$setting = json_decode($config->setting, true);
}
$setting['bom_text_color'] = coverToRGB($setting['bom_text_color']);
$width = 640;
$height = 1138;
$poster = new PosterMaker($width, $height, [255, 255, 255]);
// 获取图片的实际尺寸
list($originalWidth, $originalHeight) = getimagesize($setting['url']);
$scaleWidth = $width / $originalWidth;
$scaleHeight = $height / $originalHeight;
$scale = min($scaleWidth, $scaleHeight); // 选择一个更小的比例,以确保图片完全适应海报
$newWidth = $originalWidth * $scale; // 新宽度
$newHeight = $originalHeight * $scale; // 新高度
// 计算图片在海报上的位置
$xPos = ($width - $newWidth) / 2;
$yPos = ($height - $newHeight) / 2;
$poster->addImg($setting['url'], [0, 0], [640, 1138], 0);
switch ($type) {
case 'wechat':
list($result, $qrcode) = $this->qrcodeUrl($type);
if (!$result) {
return callback(404, $qrcode);
}
$poster->addImg($qrcode, [(($width / 2) - (280 / 2)), $height * 0.67], [280, 300]);
break;
case 'h5':
$text_link = config('setting.account_domain') . '/#/pages/share/jump?fid=' . $this->admin_uid;
$poster->addQrCode($text_link, [(($width / 2) - (280 / 2)), $height * 0.67], [300, 300]);
break;
case 'douyin':
list($result, $qrcode) = $this->qrcodeUrl($type);
if (!$result) {
return callback(404, $qrcode);
}
$poster->addImg($qrcode, [(($width / 2) - (280 / 2)), $height * 0.67], [300, 300]);
break;
}
$poster->addText($setting['bom_text'], $setting['bom_text_size'] * 1.5, [0, $height * 0.98], $setting['bom_text_color']);
$content = $poster->render($filename, 1); // 保持为图片
$storage_type = config('setting.upload_storage');
if ($storage_type == 'local') {
list($res, $info) = $this->savePoster($content, $path, $filename, true);
if (!$res) {
return callback(404, $info);
}
return callback(200, 'success', '', $this->request->domain() . '/' . $path . '/' . $info);
} elseif ($storage_type == 'aliyun') {
$oss = new Alioss();
$result = $oss->pudata($filename, $content, $path);
if ($result['status'] !== 200) {
return callback(404, $result['msg']);
}
return callback(200, 'success', '', $result['data']['url']);
} elseif ($storage_type == 'qcloud') {
$oss = new Qcloud();
$result = $oss->pudata($filename, $content, $path);
if ($result['status'] !== 200) {
return callback(404, $result['msg']);
}
return callback(200, 'success', '', $result['data']['url']);
} elseif ($storage_type == 'qiniu') {
$qiniu = new Qiniu;
$url = $qiniu->uploadData($content, $path . '/' . $filename);
if (!$url) {
return callback(404, '生成失败');
}