PHP Method Sharing of Generating Promotion Posters

  • 2021-09-24 21:47:41
  • OfStack

There is often a need to determine the user recommendation relationship after generating a promotion poster, including the specified 2D code, and sharing it with others to scan the code.

After careful analysis, The necessary elements for promoting posters are poster background map and 2D code, both of which are easy to generate. However, it is difficult to combine them into one picture and save them locally for sharing. In H5, you can draw them with the help of canvas to complete the function similar to screenshots, but there are many limitations in small programs. Then we generate posters directly in the background and call them directly in the foreground.

Pre-preparation:

1. Poster background picture, background picture 1 is stored on the server and read locally by the program;
2. Promote 2-D code, which can be 2-D code picture link or string image stream. If you generate 2-D code by yourself, see: Using phpqrcode to generate 2-D code for details.

The method is as follows:


/**

 Generate posters 
@param array  Parameter , Include pictures and text 
@param string $filename  Generate poster file name , No file will be generated if this parameter is not passed , Output pictures directly 
@return [type] [description]
*/
function createPoster($config=array(),$filename=""){
// If you want to read what's wrong with the newspaper, you can annotate this first header
if(empty($filename)) header("content-type: image/png");
$imageDefault = array(
'left'=>0,
'top'=>0,
'right'=>0,
'bottom'=>0,
'width'=>100,
'height'=>100,
'opacity'=>100
);
$textDefault = array(
'text'=>'',
'left'=>0,
'top'=>0,
'fontSize'=>32, // Size 
'fontColor'=>'255,255,255', // Font color 
'angle'=>0,
);
$background = $config['background'];// Background at the bottom of the poster 
// Background method 
$backgroundInfo = getimagesize($background);
$backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
$background = $backgroundFun($background);
$backgroundWidth = imagesx($background); // Background width 
$backgroundHeight = imagesy($background); // Background height 
$imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
$color = imagecolorallocate($imageRes, 0, 0, 0);
imagefill($imageRes, 0, 0, $color);
// imageColorTransparent($imageRes, $color); // Color transparency 
imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
// The picture was processed 
if(!empty($config['image'])){
foreach ($config['image'] as $key => $val) {
$val = array_merge($imageDefault,$val);
$info = getimagesize($val['url']);
$function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
if($val['stream']){ // If it is a string image stream, 
$info = getimagesizefromstring($val['url']);
$function = 'imagecreatefromstring';
}
$res = $function($val['url']);
$resWidth = $info[0];
$resHeight = $info[1];
// Create a drawing board   Scales the picture to the specified size 
$canvas=imagecreatetruecolor($val['width'], $val['height']);
imagefill($canvas, 0, 0, $color);
// Key functions, parameters (target resource, source, starting coordinates of target resource x,y,  Start coordinates of the source resource x,y, Width and height of target resource w,h, Width and height of source resources w,h ) 
imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
$val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
$val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
// Place an image 
imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);// Left, top, right, bottom, width, height, transparency 
}
}
// Text processing 
if(!empty($config['text'])){
foreach ($config['text'] as $key => $val) {
$val = array_merge($textDefault,$val);
list($R,$G,$B) = explode(',', $val['fontColor']);
$fontColor = imagecolorallocate($imageRes, $R, $G, $B);
$val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
$val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
}
}
// Generate a picture 
if(!empty($filename)){
$res = imagejpeg ($imageRes,$filename,90); // Save to Local 
imagedestroy($imageRes);
if(!$res) return false;
return $filename;
}else{
imagejpeg ($imageRes); // Display on the browser 
imagedestroy($imageRes);
}
}

Use Example 1: Generate a poster with a 2-D code


//2.  In the generated 2 Add dimension code logo( Generate a picture file ) 
function scerweima1($url=''){ 
require_once 'phpqrcode.php'; 
$value = $url; //2 Dimension code content  
$errorCorrectionLevel = 'H'; // Fault tolerance level  
$matrixPointSize = 6; // Generate picture size  
// Generate 2 Dimension code picture  
$filename = 'qrcode/'.microtime().'.png'; 
QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2); 
$logo = 'qrcode/logo.jpg'; // Ready logo Picture  
$QR = $filename; // Original that has been generated 2 Dimensional code graph  
if (file_exists($logo)) { 
$QR = imagecreatefromstring(file_get_contents($QR)); // Target image connection resource.  
$logo = imagecreatefromstring(file_get_contents($logo)); // Source image connection resource.  
$QR_width = imagesx($QR); //2 Dimension code picture width  
$QR_height = imagesy($QR); //2 Dimension code picture height  
$logo_width = imagesx($logo); //logo Picture width  
$logo_height = imagesy($logo); //logo Picture height  
$logo_qr_width = $QR_width / 4; // After combination logo Width of ( Account for 2 Dimensional code 1/5) 
$scale = $logo_width/$logo_qr_width; //logo Width scaling ratio of ( Intrinsic width / Width after combination ) 
$logo_qr_height = $logo_height/$scale; // After combination logo The height of  
$from_width = ($QR_width - $logo_qr_width) / 2; // After combination logo Coordinate point where the upper left corner is located  
// Reassemble and resize pictures  
/*

imagecopyresampled()  Will 1 Image ( Source image ) In 1 Block square area copied to another 1 In the images,  
*/ 
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height); 
} 
// Output picture  
imagepng($QR, 'qrcode.png'); 
imagedestroy($QR); 
imagedestroy($logo); 
return '<img src="qrcode.png" alt=" Use WeChat to scan payment ">'; 
} 
// Call to view results  
echo scerweima1('https://www.baidu.com');

Using Example 2: Generating Posters with Images, Nicknames, and 2D Codes


$config = array(
'text'=>array(
array(
'text'=>' Nickname ',
'left'=>182,
'top'=>105,
'fontPath'=>'qrcode/simhei.ttf', // Font file 
'fontSize'=>18, // Size 
'fontColor'=>'255,0,0', // Font color 
'angle'=>0,
)
),
'image'=>array(
array(
'url'=>'qrcode/qrcode.png', // Picture resource path 
'left'=>130,
'top'=>-140,
'stream'=>0, // Whether the picture resource is a string image stream 
'right'=>0,
'bottom'=>0,
'width'=>150,
'height'=>150,
'opacity'=>100
),
array(
'url'=>'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83eofD96opK97RXwM179G9IJytIgqXod8jH9icFf6Cia6sJ0fxeILLMLf0dVviaF3SnibxtrFaVO3c8Ria2w/0',
'left'=>120,
'top'=>70,
'right'=>0,
'stream'=>0,
'bottom'=>0,
'width'=>55,
'height'=>55,
'opacity'=>100
),
),
'background'=>'qrcode/bjim.jpg',
);
$filename = 'qrcode/'.time().'.jpg';
//echo createPoster($config,$filename);
echo createPoster($config);


Related articles: