Example scripts
| Chapter 27. QR (2D-Barcode) |

## Example scripts

The following sections shows a number of example script that can be used as a base for further refinement.

### Example 1 - Store QR code to file

QR Barcode with specified version and stored to a file

Example 27.6. Storing image to a file (`qrexample03.php`)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <?php
        // Example 3 : QR Barcode with specified version and stored to a file

        // Include the library
        require_once ('jpgraph/QR/qrencoder.inc.php');

        // Data to be encoded
        $data = '01234567';
        $version = 3;  // Use QR version 3
        $fileName = 'qrexample03.png';

        // Create a new instance of the encoder and let the library
        // decide a suitable error level
        $encoder = new QREncoder($version);

        // Use the image backend
        $backend = QRCodeBackendFactory::Create($encoder, BACKEND_IMAGE);

        // Set the module size (quite big)
        $backend->SetModuleWidth(5);

        // Store the barcode in the specifed file
        $backend->Stroke($data,$fileName);
        list($version,$errorcorrection) = $backend->GetQRInfo();

        echo "QR Barcode, (<b>Version: $version-$errorcorrection</b>), image stored in file $fileName";
    ?>

### Example 2 - Specified error correction level

QR Barcode with specified version and error correction level

Example 27.7. Specified error correction level (`qrexample04.php`)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <?php
        // Example 4 : QR Barcode with specified version and error correction level

        // Include the library
        require_once ('jpgraph/QR/qrencoder.inc.php');

        // Data to be encoded
        $data = '01234567';
        $version = 12;  // Use QR version 4
        $corrlevel = QRCapacity::ErrH ; // Level H error correction (the highest possible)

        // Create a new instance of the encoder using the specified
        // QR version and error correction
        $encoder = new QREncoder($version,$corrlevel);

        // Use the image backend
        $backend = QRCodeBackendFactory::Create($encoder, BACKEND_IMAGE);

        // Set the module size
        $backend->SetModuleWidth(3);

        // Store the barcode in the specifed file
        $backend->Stroke($data);
    ?>

Figure 27.14. Specified error correction level `(`qrexample04.php`)`

### Example 3 - Manual specified encoding

QR Barcode with manually specified encodation

Example 27.8. QR Barcode with manually specified encodation (`qrexample05.php`)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    <?php
        // Example 5 : QR Barcode with manually specified encodation

        // Include the library
        require_once ('jpgraph/QR/qrencoder.inc.php');

        // Data to be encoded
        // We want the data to be encoded using alphanumeric encoding even though
        // it is only numbers
        $data = array(
            array(QREncoder::MODE_ALPHANUM,'01234567')
        );

        $version = 3;  // Use QR version 3
        $corrlevel = QRCapacity::ErrH ; // Level H error correction (the highest possible)

        // Create a new instance of the encoder using the specified
        // QR version and error correction
        $encoder = new QREncoder($version,$corrlevel);

        // Use the image backend
        $backend = QRCodeBackendFactory::Create($encoder, BACKEND_IMAGE);

        // Set the module size
        $backend->SetModuleWidth(4);

        // Store the barcode in the specifed file
        $backend->Stroke($data);
    ?>

Figure 27.15. QR Barcode with manually specified encodation `(`qrexample05.php`)`

### Example 4 - JPEG image format

QR Barcode with image in JPG format

Example 27.9. QR Barcode with image in JPG format (`qrexample06.php`)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    <?php
        // Example 6 : QR Barcode with image in JPG format

        // Include the library
        require_once ('jpgraph/QR/qrencoder.inc.php');

        $data      = 'ABCDEFGH01234567'; // Data to be encoded
        $version   = -1;  // -1 = Let the library decide version (same as default)
        $corrlevel = -1; // -1 = Let the library decide error correction level (same as default)

        // Create a new instance of the encoder using the specified
        // QR version and error correction
        $encoder = new QREncoder($version,$corrlevel);

        // Use the image backend
        $backend=QRCodeBackendFactory::Create($encoder, BACKEND_IMAGE);

        // Use JPEG format with 80% quality level
        $backend->SetImgFormat('jpeg',80);

        // Set the module size
        $backend->SetModuleWidth(4);

        // Store the barcode in the specifed file
        $backend->Stroke($data);
    ?>

Figure 27.16. QR Barcode with image in JPG format `(`qrexample06.php`)`

### Example 5 - Multiple manual encoding

QR Barcode with multiple manually specified encodation schema.

### Note

Normally there would be no need to ever use manually specified encodation)

Example 27.10. multiple manually specified encodation schema. (`qrexample07.php`)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    <?php
        // Example 7 : QR Barcode with multiple manually specified encodation schemas
        // (Note: Normally there would be no need to ever use manually specified encodation)

        // Include the library
        require_once ('jpgraph/QR/qrencoder.inc.php');

        // Data to be encoded
        $data = array(
            array(QREncoder::MODE_ALPHANUM,'01234567'),
            array(QREncoder::MODE_NUMERIC,'89012345')
        );

        // Create a new instance of the encoder (automatically determined QR version and
        // error correction level)
        $encoder = new QREncoder();

        // Use the image backend
        $backend = QRCodeBackendFactory::Create($encoder, BACKEND_IMAGE);

        // Set the module size
        $backend->SetModuleWidth(4);

        // Store the barcode in the specifed file
        $backend->Stroke($data);
    ?>

Figure 27.17. multiple manually specified encodation schema. `(`qrexample07.php`)`

### Example 6 - Reading data from file

QR Barcode with data read from file

Example 27.11. data read from file (`qrexample08.php`)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <?php
        // Example 8 : QR Barcode with data read from file

        // Include the library
        require_once ('jpgraph/jpgraph.php');
        require_once ('jpgraph/QR/qrencoder.inc.php');

        $readFromFilename = 'qr-input.txt';

        // Create a new instance of the encoder and let the library
        // decide a suitable QR version and error level
        $encoder=new QREncoder();

        // Use the image backend
        $backend=QRCodeBackendFactory::Create($encoder, BACKEND_IMAGE);

        // Set the module size (quite big)
        $backend->SetModuleWidth(5);

        // .. send the barcode back to the browser for the data in the file
        $backend->StrokeFromFile($readFromFilename);
    ?>

Figure 27.18. data read from file `(`qrexample08.php`)`

### Example 7 - Adjusting colors

QR Barcode with data read from file and different colors

Example 27.12. (`qrexample09.php`)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    <?php
        // Example 9 : QR Barcode with data read from file and different colors

        // Include the library
        require_once ('jpgraph/jpgraph.php');
        require_once ('jpgraph/QR/qrencoder.inc.php');

        $readFromFilename = 'qr-input.txt';

        // Create a new instance of the encoder and let the library
        // decide a suitable QR version and error level
        $encoder = new QREncoder();

        // Use the image backend
        $backend = QRCodeBackendFactory::Create($encoder, BACKEND_IMAGE);

        // Set the module size (quite big)
        $backend->SetModuleWidth(5);

        // Use blue and white colors instead
        $backend->SetColor('navy','white');

        // .. send the barcode back to the browser for the data in the file
        $backend->StrokeFromFile($readFromFilename);
    ?>

Figure 27.19. `(`qrexample09.php`)`

### Example 8 - Setting error correction level

Specified error correction level

Example 27.13. Specified error correction level (`qrexample10.php`)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    <?php
        // Example 10 : Specified error correction level

        // Include the library
        require_once ('jpgraph/QR/qrencoder.inc.php');

        $data      = 'ABCDEFGH01234567'; // Data to be encoded
        $version   = -1;  // -1 = Let the library decide version (same as default)
        $corrlevel = QRCapacity::ErrH; // Error correction level H (Highest possible)

        // Create a new instance of the encoder using the specified
        // QR version and error correction
        $encoder = new QREncoder($version,$corrlevel);

        // Use the image backend
        $backend = QRCodeBackendFactory::Create($encoder, BACKEND_IMAGE);

        // Set the module size
        $backend->SetModuleWidth(3);

        // Set color
        $backend->SetColor('brown','white');

        // Store the barcode in the specifed file
        $backend->Stroke($data);
    ?>

Figure 27.20. Specified error correction level `(`qrexample10.php`)`

### Example 9 - Generating ASCII output

Using the ASCII backend

Example 27.14. Using the ASCII backend (`qrexample12.php`)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    <?php
    // Include the library
    require_once ('jpgraph/QR/qrencoder.inc.php');

    // Example 11 : Generate postscript output

    $data         = 'ABCDEFGH01234567'; // Data to be encoded
    $version      = -1;  // -1 = Let the library decide version (same as default)
    $corrlevel    = QRCapacity::ErrH; // Error correction level H (Highest possible)
    $modulewidth  = 1;

    // Create a new instance of the encoder using the specified
    // QR version and error correction
    $encoder = new QREncoder($version,$corrlevel);

    // Use the image backend
    $backend = QRCodeBackendFactory::Create($encoder, BACKEND_ASCII);

    // Set the module size
    $backend->SetModuleWidth($modulewidth);

    // Store the barcode in the specifed file
    $ps_str = $backend->Stroke($data);

    echo '<pre>'.$ps_str.'</pre>';
    ?>

Figure 27.21. QR With ASCII rendering

| Up |
| Home |