当前位置:网站首页>PHP image synthesis technology
PHP image synthesis technology
2022-07-28 16:26:00 【Sun Fendou】
PHP Image synthesis technology
Use scenarios : Product poster and user QR code synthesis
Don't talk much, just go to the code
There is a kind of thinking in this method , It is subject to opening a picture and adding pictures and text to a picture , File stream from low to high
/** * @param $data The text content contained in the QR code * @param $filename Save the file name of QR code output ,*.png * @param bool $picPath The path of QR code output * @param bool $logo Included in the QR code LOGO Picture path * @param string $bgColor QR code background color * @param string $color QR code point color * @param int $size The size of the QR code * @param string $level QR code error correction level :L、M、Q、H * @param int $padding The spacing of the QR code frame * @param bool $saveandprint Whether to save to a file and output directly in the browser ,true: Save and export at the same time ,false: Save only files * @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"); // Introduce the toolkit
// The following notes the code to save the QR code image to the local , If you want to save the picture , use $fileName Replace the second parameter false
$qrcode = new \QRcode();
$path = $picPath ? $picPath : "./uploads/".date('Ymd'); // Picture output path
if (!file_exists($path)) {
mkdir($path,0777,true);
}
$logo = !empty($logo) && file_exists($logo) ? $logo : false;
// Add LOGO
if (empty($logo) || $logo === false) {
// It doesn't contain LOGO
if ($filename == false) {
$qrcode->png($data, $filename, $level, $size, $padding, $saveandprint, $bgColor, $color); // Output directly to the browser , Not included LOGO
} else {
$filename = $path . '/' . $filename; // Synthesis path
$qrcode->png($data, $filename, $level, $size, $padding, $saveandprint, $bgColor, $color); // Output directly to the browser , Not included LOGO
}
} else {
// contain LOGO
if ($filename == false) {
//$filename=tempnam('','').'.png';// Generate temporary files
die(' Parameter error ');
} else {
// Generate qr code , Save to file
$filename = $path . '/' . $filename; // Synthesis path
}
$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"); // Output to browser
//imagepng($QR);
} else {
imagepng($QR, $filename);
}
}
}
return $filename;
}
/** * Crop the picture into a circular picture * @param string The image Url Address $url * @param string Saved new url Address $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;
}
边栏推荐
- Darknet training yolov4 record
- Stm32cube infrared remote control: input capture
- 我在上海偶遇数字凤凰#坐标徐汇美罗城
- A good start
- R语言使用fs包的file_delete函数删除指定文件夹下的指定文件、举一反三、dir_delete函数、link_delete函数可以用来删除文件夹和超链接
- Numpy ndarray learning < I > Foundation
- Wei Jianjun couldn't catch up with Li Shufu by riding a BMW
- js 双向链表 01
- 2021 肯特面试题2
- 为什么学编程的人大多数都去了深圳和北京?
猜你喜欢

SCI scientific paper writing Growth Camp (full version)

2-channel Di high-speed pulse counter, 1-channel encoder to Modbus TCP wired wireless module ibf161

资本「断供」两年,我只能把公司卖了

2021 Kent interview question 2

JS linked list 01

JS array (summary)

Telecommuting can be easily realized in only three steps

JS queue

Rosen's QT journey 101 models and views in QT quick

2021 Yahong pen test question 2
随机推荐
跳表的实现
One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
LwIP development | realize TCP server through socket
mysql查询 limit 1000,10 和limit 10 速度一样快吗?如果我要分页,我该怎么办?
Detailed explanation of QT qstring
2021 Yahong pen test question 2
2021 亚鸿笔试题2
Practical development tutorial of software problem repair tracking system (Part 1)
正大杯黑客马拉松数据解析竞赛
5 亿用户,比微信还早四年……这个运营了 15 年的 APP 即将永久停服
JS priority queue
兆骑科创创新创业大赛人才引进平台,双创赛事高层次人才引进
2021 Kent interview question 2
李宏毅《机器学习》丨5. Tips for neural network design(神经网络设计技巧)
为什么学编程的人大多数都去了深圳和北京?
RF module wireless transceiver rf63u chip application data transmission and infrastructure network
2021-10-21 notes
Automatically pack compressed backup download and delete bat script commands
Redis series 4: sentinel (sentinel mode) with high availability
Notes on October 22, 2021