WordPress 使用联图网 API 生成二维码(缓存和 Logo)

时间:2019-08-29   阅读:694

昨天折腾了下,给博客加了一个二维码,使用了联图网提供的在二维码中插入一个 Logo 的接口,发现一个问题,生成速度特别慢,把图片链接放到本地 CDN 之后依然很慢,可能是获取图片加上服务器不是很给力的原因导致生成速度比较慢,于是考虑到缓存(比 Google API 一样的妥协,不过 Google 不支持插入 Logo),折腾了一下成功了~

首先在 functions.php 里放代码:

1
2
3
4
/*
*二维码
*http://www.bgbk.org
*/function Bing_qrcode_cache( $context, $size = 150, $logo = null, $cache = true, $name = 'qrcode' ){if( $logo ) $qrlogo = '&el=h&logo=' . $logo;$imgurl = 'http://qr.liantu.com/api.php?text=' . $context . '&w=' . $size . '&m=5' . $qrlogo;if( !$cache ) return $imgurl;$cacheimg = get_bloginfo( 'url' ) . '/qrcode/' . $name . '.png';if( file_exists( ABSPATH . 'qrcode/' . $name . '.png' ) ) return $cacheimg;set_time_limit( 10 );$fp = @file_get_contents( $imgurl, 'r' );if( !$fp ) return $imgurl;if( file_put_contents( ABSPATH . 'qrcode/' . $name . '.png', $fp ) ) return $cacheimg;return $imgurl;}

/* *二维码 *http://www.bgbk.org */ function Bing_qrcode_cache( $context, $size = 150, $logo = null, $cache = true, $name = 'qrcode' ){ if( $logo ) $qrlogo = '&el=h&logo=' . $logo; $imgurl = 'http://qr.liantu.com/api.php?text=' . $context . '&w=' . $size . '&m=5' . $qrlogo; if( !$cache ) return $imgurl; $cacheimg = get_bloginfo( 'url' ) . '/qrcode/' . $name . '.png'; if( file_exists( ABSPATH . 'qrcode/' . $name . '.png' ) ) return $cacheimg; set_time_limit( 10 ); $fp = @file_get_contents( $imgurl, 'r' ); if( !$fp ) return $imgurl; if( file_put_contents( ABSPATH . 'qrcode/' . $name . '.png', $fp ) ) return $cacheimg; return $imgurl; }

使用方法

1
Bing_qrcode_cache( $context, $size, $logo, $cache, $name );

Bing_qrcode_cache( $context, $size, $logo, $cache, $name );

参数

  1. $context:(字符串),(必须),填写需要生成的文本内容,例如:“生成文本内容”、“http://www.bgbk.org”,默认值:无。

  2. $size:(数字),(可选),填写二维码尺寸(像素数),默认值:150

  3. $logo:(字符串),(可选),二维码中心 Logo 的链接,默认值:null

  4. $cache:(布尔),(可选),是否缓存,默认值:true

  5. $name:(字符串),(可选),缓存后的文件名,必须在 $cache 参数为 true 的时候才有效,默认值:’qrcode’

返回值

如果没开缓存则返回生成后的图片链接。

如果开启缓存并且缓存成功或者文件已经存在则返回缓存后的本地图片链接,缓存失败则返回图片生成后的链接。

例子

1
echo Bing_qrcode_cache( 'http://www.bgbk.org', 150, 'http://www.gravatar.com/avatar/a1472e26feb0ca37e76cac67ac92cd8d', true, 'example' );

echo Bing_qrcode_cache( 'http://www.bgbk.org', 150, 'http://www.gravatar.com/avatar/a1472e26feb0ca37e76cac67ac92cd8d', true, 'example' );

上边的代码执行后会输出:http://127.0.0.1/qrcode/example.png

注意

开启缓存后必须在网站根目录建立一个名为 “qrcode” 的文件夹,否则缓存保存不上去。

打印二维码

上边的二维码函数只能根据参数获取链接,需要配合其它条件使用,如果你是 WordPress 的话可以参考我的代码:

1
2
3
4
/*
*二维码
*http://www.bgbk.org
*///输出二维码function Bing_print_qrcode( $size = 150, $get = true ){if( !( is_singular() || is_page() || is_home() || is_front_page() || is_category() || is_tag() ) ) return;//只有文章页、首页、页面、分类页和标签页才显示二维码if( is_singular() ) $name = get_the_id();//文章或页面图片命名为 idelseif( is_home() || is_front_page() ) $name = 'home';//首页则命名为 homeelseif( is_category() ) $name = 'cat-' . get_query_var( 'cat' );//分类则是 cat-分类idelseif( is_tag() ) $name = 'tag-' . get_query_var( 'tag_id' );//标签页则 tag-标签id$cache = true;//如果不使用缓存改成 false 上边命名的代码也就没用了$imgurl = '<img src="' . Bing_qrcode_cache( get_permalink(), $size, 'http://www.gravatar.com/avatar/' . md5( strtolower( trim( get_bloginfo( 'admin_email' ) ) ) ), $cache, $name ) . '" class="qrcode" width="' . $size . '" height="' . $size . '" alt="qrcode" />';//输出二维码图片代码,注意,默认中心 Logo 使用的是管理员头像if( $get ) return $imgurl;echo $imgurl;}

/* *二维码 *http://www.bgbk.org */ //输出二维码 function Bing_print_qrcode( $size = 150, $get = true ){ if( !( is_singular() || is_page() || is_home() || is_front_page() || is_category() || is_tag() ) ) return;//只有文章页、首页、页面、分类页和标签页才显示二维码 if( is_singular() ) $name = get_the_id();//文章或页面图片命名为 id elseif( is_home() || is_front_page() ) $name = 'home';//首页则命名为 home elseif( is_category() ) $name = 'cat-' . get_query_var( 'cat' );//分类则是 cat-分类id elseif( is_tag() ) $name = 'tag-' . get_query_var( 'tag_id' );//标签页则 tag-标签id $cache = true;//如果不使用缓存改成 false 上边命名的代码也就没用了 $imgurl = '<img src="' . Bing_qrcode_cache( get_permalink(), $size, 'http://www.gravatar.com/avatar/' . md5( strtolower( trim( get_bloginfo( 'admin_email' ) ) ) ), $cache, $name ) . '" class="qrcode" width="' . $size . '" height="' . $size . '" alt="qrcode" />';//输出二维码图片代码,注意,默认中心 Logo 使用的是管理员头像 if( $get ) return $imgurl; echo $imgurl; }

使用方法:

1
Bing_print_qrcode( 尺寸, true 则返回,false 则打印 );

Bing_print_qrcode( 尺寸, true 则返回,false 则打印 );


上一篇:WordPress 设置相册图集(galleries)默认的图片大小

下一篇:禁止WordPress评论里的网址自动转换为可点击的链接

网友评论