SoFunction
Updated on 2025-03-09

Introduction to the example of php image enlargement and synthesis and font addition

Preface

Before, I just generated QR codes, but now I need to put the QR code in a background image and need to have text introduction. I haven't done it before, now I've made a record.

1. Get pictures

$background_path = root_path() . 'public/event/'; //Background picture address$qrcode_path = root_path() . 'public/event/qrcode/'; // QR code picture address// Background image$background_image = imagecreatefromjpeg($background_path);
// QR code picture$qrcode_image = imagecreatefrompng($qrcode_path);

Note: The image address must be the absolute address

2. Enlarge the QR code picture

//Get the attributes of the image, the first width, the second height, type 1=>gif, 2=>jpeg, 3=>pnglist($qrcode_x, $qrcode_y) = getimagesize($qrcode_path);
// Zoom in QR code image to 1200 pixels$size = 1200;
// Create a new canvas$finalQrcode = imagecreatetruecolor($size, $size);
// Put the QR code picture on the new canvasimagecopyresampled($finalQrcode, $qrcode_image, 0, 0, 0, 0, $size, $size, $qrcode_x, $qrcode_y);

Note: Picture enlargement is actually to create a new canvas of the size you need, move the previous picture to the new canvas, and control the position and size of the picture on the canvas through parameters.

3. Multiple pictures synthesis

// Synthesize the background and QR code pictures together//Get the attributes of the image, the first width, the second height, type 1=>gif, 2=>jpeg, 3=>pnglist($background_width,$background_height) = getimagesize($background_path);
// Create a new canvas to fill the background$finalImage = imageCreatetruecolor($background_width,$background_height);
// Image assignment color$color = imagecolorallocate($finalImage, 255, 255, 255);
//Set the X-axis coordinate position of the centered picture$x = ($background_width-$size)/2;
//Set the Y-axis coordinate position of the centered picture$y = 430;
// Used to fill the image with the given colorimagefill($finalImage, 0, 0, $color);
// Define the color as transparentimageColorTransparent($finalImage, $color);
// Use background to fill the canvas// Target map Source map Target X coordinate point Target Y coordinate point X coordinate point Source Y coordinate point Target width Target height Source map width Source map heightimagecopyresampled($finalImage,$background_image,0,0,0,0,$background_width,$background_height,$background_width,$background_width);
//The position of the QR code picture on the background $x horizontal coordinate, $y vertical coordinateimagecopymerge($finalImage,$finalQrcode, $x,$y,0,0,$size,$size, 100);

4. Add text and center

When the text of the picture is centered, we need to calculate the size of the picture and the size that the text needs to occupy.

There is now a composer library (stil/gd-text) that can implement this function.

composer require stil/gd-text
$text = 'Test test test test test test test test test test test test test test test test test test test test test test test test;
// Different fonts will cause Chinese characters to be written into pictures and garbled$font = root_path() . 'public/font/'; // The absolute address of the font$showY = 2480-800;
$box = new Box($finalImage);
$box->setFontFace($font);
$box->setFontColor(new Color(0, 60, 121));//Font color$box->setFontSize(160);//Font size$box->setLineHeight(2);//Road height$box->setBox(-22, $showY, 2480, 200);
$box->setTextAlign('center', 'top'); // The font is centered$box->draw($text);
Header("Content-type: image/jpeg");
//Save the canvas to the specified fileimagejpeg($finalImage, root_path() . 'public/event/qrcode/');

V. Complete code

$background_path = root_path() . 'public/event/'; //Background picture address$qrcode_path = root_path() . 'public/event/qrcode/'; // QR code picture address// Background image$background_image = imagecreatefromjpeg($background_path);
// QR code picture$qrcode_image = imagecreatefrompng($qrcode_path);
//Get the attributes of the image, the first width, the second height, type 1=>gif, 2=>jpeg, 3=>pnglist($qrcode_x, $qrcode_y) = getimagesize($qrcode_path);
// Zoom in QR code image to 1200 pixels$size = 1200;
$finalQrcode = imagecreatetruecolor($size, $size);
imagecopyresampled($finalQrcode, $qrcode_image, 0, 0, 0, 0, $size, $size, $qrcode_x, $qrcode_y);
// Synthesize the background and QR code pictures together//Get the attributes of the image, the first width, the second height, type 1=>gif, 2=>jpeg, 3=>pnglist($background_width,$background_height) = getimagesize($background_path);
$finalImage = imageCreatetruecolor($background_width,$background_height);
$color = imagecolorallocate($finalImage, 255, 255, 255);
//Set the X-axis coordinate position of the centered picture$x = ($background_width-$size)/2;
//Set the Y-axis coordinate position of the centered picture$y = 430;
imagefill($finalImage, 0, 0, $color);
imageColorTransparent($finalImage, $color);
imagecopyresampled($finalImage,$background_image,0,0,0,0,$background_width, $background_height,$background_width,$background_width);
//The position of the picture on the background $x horizontal coordinate, $y vertical coordinateimagecopymerge($finalImage,$finalQrcode, $x,$y,0,0,$size,$size, 100);
$text = 'Test test test test test test test test test test test test test test test test test test test test test test test test;
$font = root_path() . 'public/font/';
$showY = 2480-800;
$box = new Box($finalImage);
$box->setFontFace($font);
$box->setFontColor(new Color(0, 60, 121));//Font color$box->setFontSize(160);//Font size$box->setLineHeight(2);//Road height$box->setBox(-22, $showY, 2480, 200);
$box->setTextAlign('center', 'top'); // The font is centered$box->draw($text);
Header("Content-type: image/jpeg");
//Save the canvas to the specified fileimagejpeg($finalImage, root_path() . 'public/event/qrcode/');
exit();

This is the article about the example of magnification and synthesis of php image and font operation. For more related php image magnification, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!