Cache remote QR code image to local

Source: Internet
Author: User
Step 1: run the following code to throw to functions. phpfunctionget_qr ($ url, $ path, $ qrpic) {set_time_limit (10); set the ten-second timeout value $ destination_folder $ path? $ Path.:; $ localname $ destination_folder. $ qrpic; $ filefopen ($ url, rb); r + mode of the fopen function: Read

Step 1: run the following code to throw the function get_qr ($ url, $ path, $ qrpic) {set_time_limit (10); // set the ten-second timeout value $ destination_folder = $ path? $ Path. '/': ''; $ localname = $ destination_folder. $ qrpic; $ file = fopen ($ url, "rb"); // r + mode of the fopen function: Read

Step 1: run the following code in functions. php:

Function get_qr ($ url, $ path, $ qrpic) {set_time_limit (10); // set the 10-second timeout value $ destination_folder = $ path? $ Path. '/': ''; $ localname = $ destination_folder. $ qrpic; $ file = fopen ($ url, "rb"); // r + mode of the fopen function: open the file pointer in read/write mode to the file header if ($ file) {$ newf = fopen ($ localname, "wb"); // w +, open the file pointer in read/write mode and point to the file header. if the file does not exist, try to create if ($ newf) while (! Feof ($ file) {fwrite ($ newf, fread ($ file, 1024*2), 1024*2); // write the file, fread controls the maximum file size, here is 2 M }}if ($ file) {fclose ($ file); // close the file opened by fopen} if ($ newf) {fclose ($ newf );}}

Step 2: create a new folder named qrcode in the root directory of the website to ensure that you have the write permission,

Step 3: place the following code in the area where you need to output the QR code image, such as single. php.

 /Qrcode/
 . Jpg "width =" 100 "height =" 100 "alt =" QR code "/>
 
The tutorial is over. Below are some of my thoughts during the code writing process,

  1. For PHPfopenFunction

    Fopen cannot directly obtain https headers.

  2. For wordpressthe_permalinkAndget_permalinkFunction

    The former isDisplayDirectly output the result, which isReturnReturn value. The latter must be used to use such variables in user-defined functions.
    For example, the usage of the_id and get_the_id is the same.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.