当前位置:网站首页>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;
}
边栏推荐
- 2021 Kent interview question 1
- JS bidirectional linked list 01
- 2-channel Di high-speed pulse counter, 1-channel encoder to Modbus TCP wired wireless module ibf161
- [Multisim Simulation] LM339 zero crossing circuit simulation
- R language uses ggpairs function of ggally package to visualize pairwise relationship graph of grouping multivariable, set alpha parameter to change image transparency, diagonal continuous variable de
- Remote serial port server (adapter) UART to 1-wire application
- 2021 亚鸿笔试题2
- 激光测距仪非接触式地表裂缝监测仪
- 加速投资的小红书,“病急乱投医”?
- QT QString详解
猜你喜欢

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

Vm501 development kit development version single vibrating wire sensor acquisition module geotechnical engineering monitoring

js 双向链表 01

疫情红利消失,「居家健身」泡沫消散

Dynamic programming -- digital statistics DP

Redis系列4:高可用之Sentinel(哨兵模式)

JS stack

小程序中的分页查询

Ethernet to RS485 serial port counter WiFi module LED light controller ibf165

Two special functions (arrow function and method)
随机推荐
深入理解Istio流量管理的熔断配置
el-input限制只能输入规定的数
STM32F103C8T6 + 0.96“ I2C OLED显示3D_Cube
500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued
Mlx90640 infrared thermal imager temperature sensor module development notes (VIII)
I'll show you a little chat! Summary of single merchant function modules
Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
Zhengda cup hacker marathon data analysis competition
Thoughts on solving the pop-up of malicious computer advertisements
【Multisim仿真】LM339过零电路仿真
js 队列
R语言ggplot2可视化绘制线图(line plot)、使用gghighlight包突出高亮线图中满足组合判断条件的线图(satisfies both condition A and B)
js 数组(总结)
Numpy ndarray learning < I > Foundation
2021 肯特面试题3
Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation
About standard IO buffers
R语言使用GGally包的ggpairs函数可视化分组多变量的两两关系图、设置alpha参数改变图像透明度、对角线上连续变量密度图、离散变量条形图、两两关系图中包含散点图、直方图、箱图以及相关性数值
Telecommuting can be easily realized in only three steps
Fifth uncle's thinking