第一步,以下代码扔到functions.php function get_qr($url,$path,$qrpic){ set_time_limit (10); //设置十秒超时$destination_folder = $path?$path.'/':''; $localname = $destination_folder .$qrpic; $file = fopen ($url, "rb"); //fopen函数的r+模式: 读
第一步,以下代码扔到functions.php
function get_qr($url,$path,$qrpic){
set_time_limit (10); //设置十秒超时
$destination_folder = $path?$path.'/':'';
$localname = $destination_folder .$qrpic;
$file = fopen ($url, "rb"); //fopen函数的r+模式: 读写方式打开 文件指针指向文件头
if ($file) {
$newf = fopen ($localname, "wb"); // w+,读写方式打开 文件指针指向文件头 如果文件不存在则尝试创建之
if ($newf)
while(!feof($file)) {
fwrite( $newf, fread($file, 1024 * 2 ), 1024 * 2 ); //写入文件,fread控制文件最大的大小,这里是2M
}
}
if ($file) {
fclose($file); //关闭fopen打开的文件
}
if ($newf) {
fclose($newf);
}
} 第二步,在网站根目录建立一个叫qrcode的新文件夹,确保有写入权限,
第三步,以下代码放到需要输出二维码图片的地方,如single.php
<?php
$localqr = ABSPATH .'qrcode/'.get_the_id().'.jpg';
if (!file_exists($localqr)) {//如果图片已经存在,则不会再次保存
get_qr( "http://chart.googleapis.com/chart?cht=qr&chs=100x100&choe=UTF-8&chld=L|4&chl=".get_permalink() ,"qrcode", get_the_id().".jpg");//图片名以文章id命名
}
?>
<img src="<?php echo home_url( '' ); ? alt="缓存远程二维码图片到本地" >/qrcode/<?php the_id()?>.jpg" width="100" style="max-width:90%" alt="QR 码"/>
<!--默认二维码图片大小是100*100,如果要改,需要改4个地方,已经用蓝色标注-->
fopen函数
fopen不能直接获取https打头的url
the_permalink和get_permalink函数
前者是display直接输出结果,后者是return返回值,在自定义函数中使用此类变量需要使用后者
例如the_id和get_the_id用法也是如此
fopen-qr is coded by xiaohudie. Any posts here is one hundred percent original, so please keep my link so as not to hurt this pretty girl
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号