当前位置:网站首页>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;
}
边栏推荐
- 资本「断供」两年,我只能把公司卖了
- NTC, PT100 thermal resistance to 4-20mA temperature signal converter
- Automatic conversion and cast
- 激光测距仪非接触式地表裂缝监测仪
- Practical development tutorial of software problem repair tracking system (Part 1)
- Record Clearfix clear float
- Remember the common JS methods of projects
- Sudden! MSI CEO Jiang Shengchang fell to death
- laravel
- A program for judging the result of cyclic input
猜你喜欢

头条文章_signature

JS bidirectional linked list 01

Why do most people who learn programming go to Shenzhen and Beijing?

Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation

高精度绝对角度传感器应用高速度角度监测

I'll show you a little chat! Summary of single merchant function modules

CoDeSys realizes bubble sorting

mysql查询 limit 1000,10 和limit 10 速度一样快吗?如果我要分页,我该怎么办?

Record doc

激光测距仪非接触式地表裂缝监测仪
随机推荐
2021-04-02
Huada chip hc32f4a0 realizes RS485 communication DMA transceiver
Numpy ndarray learning < I > Foundation
SCI scientific paper writing Growth Camp (full version)
laravel
Baidu editor ueeditor, when editing too much content, the toolbar is not visible, which is not convenient for editing or uploading problems
One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
激光测距仪非接触式地表裂缝监测仪
魏建军骑宝马也追不上李书福
2021 Yahong pen test questions
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
mysql 查看事件状态语句和修改办法
PHP mb_ Substr Chinese garbled code
JS linked list 01
JS queue
Summary of for loop in JS
Darknet training yolov4 record
Roson的Qt之旅#102 ListModel
2021 亚鸿笔试题2
加速投资的小红书,“病急乱投医”?