Generating QR Code on a Background Image with Text Using PHP

This article demonstrates how to use PHP to generate a QR code, overlay it onto a background image, add custom text, and save the resulting composite image, providing complete source code and usage instructions for backend developers.

php Courses
php Courses
php Courses
Generating QR Code on a Background Image with Text Using PHP

This guide explains how to create a QR code, place it on a background picture, render custom text onto the same image, and finally output the merged result using PHP. It includes a full class implementation with methods for QR generation, text rendering, image compositing, and file saving.

Key steps:

Construct the base image from a JPEG source.

Generate a QR code image with configurable error correction level, size, and margin.

Overlay the QR code onto the base image at a specified position.

Render custom text with chosen font, size, and color, centered horizontally.

Save the final image as a JPEG file.

Below is the complete PHP class used in the tutorial:

class codeImg {
    private $date,$img,$main,$width,$height,$target,$white;

    public function constr($source) {
        $this->date   = '' . date('Ymd') . '/';
        $this->img    = $this->date . md5($source) . '.jpg';
        $this->main   = imagecreatefromjpeg($source);
        $this->width  = imagesx($this->main);
        $this->height = imagesy($this->main);
        $this->target = imagecreatetruecolor($this->width, $this->height);
        $this->white  = imagecolorallocate($this->target, 255, 255, 255);
        imagefill($this->target, 0, 0, $this->white);
        imagecopyresampled($this->target, $this->main, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
    }

    /**
     * Generate QR code
     * @param string $website   URL to encode
     * @param string $filename  Output file name (optional)
     * @param string $level     Error correction level (L, M, Q, H)
     * @param int    $size      Module size (default 4)
     * @param int    $margin    Margin size (default 2)
     */
    public function qrencode($website, $filename = false, $level = "L", $size = 4, $margin = 2) {
        include "./phpqrcode/qrlib.php";
        QRcode::png($website, $filename, $level, $size, $margin);
    }

    /**
     * Add text to the image
     */
    public function generateFont($source, $text1, $textwidth, $textherght, $fontSize = 18, $cate1 = 255, $cate2 = 250, $cate3 = 250, $font = './font/fangsong_GB2312.ttf') {
        $this->constr($source);
        $fontColor = imagecolorallocate($this->target, $cate1, $cate2, $cate3);
        $fontBox   = imagettfbbox($fontSize, 0, $font, $text1);
        imagettftext($this->target, $fontSize, 0, $textwidth, $textherght, $fontColor, $font, $text1);
        $this->createImg();
        return $this->img;
    }

    /**
     * Merge QR code onto background image
     */
    public function generateImg($source, $codeurl, $sourcewidth, $sourceheight, $codewidth = 100, $codeheight = 100) {
        $this->constr($source);
        $child1 = imagecreatefrompng($codeurl);
        $codewidth  = $codewidth  > 0 ? $codewidth  : imagesx($child1);
        $codeheight = $codeheight > 0 ? $codeheight : imagesy($child1);
        imagecopyresampled($this->target, $child1, $sourcewidth, $sourceheight, 0, 0, $codewidth, $codeheight, imagesx($child1), imagesy($child1));
        imagedestroy($child1);
        $this->createImg();
        return $this->img;
    }

    private function createImg() {
        @mkdir('./' . $this->date);
        imagejpeg($this->target, './' . $this->img, 95);
        imagedestroy($this->main);
        imagedestroy($this->target);
    }
}

// Example usage
$source   = "./img/1000.jpg";
$codeImg  = new codeImg($source);
$website  = "http://www.baidu.com";
$codeurl  = "./temp/code.png";
$codeImg->qrencode($website, $codeurl);
$text         = "开始了开始了hahhah呵呵";
$textwidth    = 100;
$textherght   = 50;
$generateFont = $codeImg->generateFont($source, $text, $textwidth, $textherght);
$sourcewidth  = 200;
$sourceheight = 150;
$generateImg  = $codeImg->generateImg($generateFont, $codeurl, $sourcewidth, $sourceheight);
echo "<img src='" . $generateImg . "'>";

The example demonstrates initializing the class with a background image, creating a QR code for a URL, adding a text label, positioning the QR code on the background, and finally outputting the composed image in an HTML <img> tag.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Graphicsimage-processingqr-code
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.