当前位置:网站首页>PHP图片合成技术
PHP图片合成技术
2022-07-28 15:22:00 【孙奋斗】
PHP图片合成技术
使用场景:商品海报与用户二维码合成
话不多说直接上代码
该方法存在一种思维,以打开某一个图片在某一个图片上添加图片添加文字为准,文件流由低到高
/** * @param $data 二维码包含的文字内容 * @param $filename 保存二维码输出的文件名称,*.png * @param bool $picPath 二维码输出的路径 * @param bool $logo 二维码中包含的LOGO图片路径 * @param string $bgColor 二维码背景色 * @param string $color 二维码点颜色 * @param int $size 二维码的大小 * @param string $level 二维码编码纠错级别:L、M、Q、H * @param int $padding 二维码边框的间距 * @param bool $saveandprint 是否保存到文件并在浏览器直接输出,true:同时保存和输出,false:只保存文件 * @return string */
function myqrcode($data, $filename, $picPath = false, $logo = false, $bgColor='#ffffff', $color='#000000', $size = 15, $level = 'L', $padding = 2, $saveandprint = true) {
vendor("phpqrcode.phpqrcode"); //引入工具包
// 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false
$qrcode = new \QRcode();
$path = $picPath ? $picPath : "./uploads/".date('Ymd'); //图片输出路径
if (!file_exists($path)) {
mkdir($path,0777,true);
}
$logo = !empty($logo) && file_exists($logo) ? $logo : false;
//在二维码上面添加LOGO
if (empty($logo) || $logo === false) {
//不包含LOGO
if ($filename == false) {
$qrcode->png($data, $filename, $level, $size, $padding, $saveandprint, $bgColor, $color); //直接输出到浏览器,不含LOGO
} else {
$filename = $path . '/' . $filename; //合成路径
$qrcode->png($data, $filename, $level, $size, $padding, $saveandprint, $bgColor, $color); //直接输出到浏览器,不含LOGO
}
} else {
//包含LOGO
if ($filename == false) {
//$filename=tempnam('','').'.png';//生成临时文件
die('参数错误');
} else {
//生成二维码,保存到文件
$filename = $path . '/' . $filename; //合成路径
}
$qrcode->png($data, $filename, $level, $size, $padding, $saveandprint, $bgColor, $color);
$QR = imagecreatefromstring(file_get_contents($filename));
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
$logo_qr_width = $QR_width / 5;
$scale = $logo_width / $logo_qr_width;
$logo_qr_height = $logo_height / $scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
if ($filename === false) {
Header("Content-type: image/png");
imagepng($QR);
} else {
if ($saveandprint === true) {
imagepng($QR, $filename);
//header("Content-type: image/png"); //输出到浏览器
//imagepng($QR);
} else {
imagepng($QR, $filename);
}
}
}
return $filename;
}
/** * 将图片裁剪为圆形的图片 * @param string 图片的Url地址 $url * @param string 保存的新的url地址 $path */
function circularImg($url, $path){
$w = 270; $h=270; // original size
$original_path= $url;
$src = imagecreatefromstring(file_get_contents($original_path));
$newpic = imagecreatetruecolor($w,$h);
imagealphablending($newpic,false);
$transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127);
$r=$w/2;
for($x=0;$x<$w;$x++)
for($y=0;$y<$h;$y++){
$c = imagecolorat($src,$x,$y);
$_x = $x - $w/2;
$_y = $y - $h/2;
if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
imagesetpixel($newpic,$x,$y,$c);
}else{
imagesetpixel($newpic,$x,$y,$transparent);
}
}
imagesavealpha($newpic, true);
imagepng($newpic, $path);
imagedestroy($newpic);
imagedestroy($src);
// unlink($url);
return $path;
}
边栏推荐
- 5 亿用户,比微信还早四年……这个运营了 15 年的 APP 即将永久停服
- Is MySQL query limit 1000,10 as fast as limit 10? If I want to page, what should I do?
- IT远程运维是什么意思?远程运维软件哪个好?
- Telecommuting can be easily realized in only three steps
- 使用js直传oss阿里云存储文件,解决大文件上传服务器限制
- SDL2 简明教程(四):用 SDL_IMAGE 库导入图片
- Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
- Rust Getting Started Guide (crite Management)
- Zhengda cup hacker marathon data analysis competition
- Two of C language programming!! Role of
猜你喜欢

RF module wireless transceiver rf63u chip application data transmission and infrastructure network

深部位移监测系统wk813应用边坡、大坝、堤防、铁路和建筑基坑开挖等深部位移测量

JS linked list 02

激光测距仪非接触式地表裂缝监测仪

动态规划 --- 数位统计DP

The video Number finds the golden key, and Tiktok imitates the latecomers

js 队列

js 链表 02

5 亿用户,比微信还早四年……这个运营了 15 年的 APP 即将永久停服

2021 Kent interview question 1
随机推荐
laravel
食品安全 | 这两类瓜果宜改善便秘 孕妇人群尤其建议
Application of optical rain gauge to rainfall detection
Dynamic programming -- digital statistics DP
Stm32cube infrared remote control: input capture
Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation
Darknet training yolov4 record
js 队列
Note: numerical accumulation animation
Automatically pack compressed backup download and delete bat script commands
JS priority queue
I came across Digital Phoenix coordinate Xuhui Meiluo city in Shanghai
解决uniapp等富文本图片宽度溢出
R language ggplot2 visually draws line plots, and uses gghighlight package to highlight the lines that meet the combination judgment conditions in the line graphs (satisfies both condition a and b)
五舅的思考
The little red book of accelerating investment, "rush to medical treatment"?
PHP mb_substr 中文乱码
魏建军骑宝马也追不上李书福
el-input限制只能输入规定的数
Let's learn the game of beating hamsters