This time to bring you to the PHP implementation to build a promotional poster steps, PHP implementation to generate promotional posters of the notes are what, the following is the actual case, together to see.
Often there is the need to generate promotional posters, including the specified QR code, to share someone else to scan the code to determine the user referral relationship.
Careful analysis, the necessary elements of the promotion poster is the poster background and QR code, both of which are easy to generate, but to combine the two together into a picture two-dimensional and can be saved to the local easy to share out, this is the difficulty, in the H5 can use canvas to complete similar functions, But in the small program inside a lot of limitations. Then we generate the poster directly in the background, the foreground calls directly.
Pre-Preparation:
1. Poster background, background map General storage server, program local read;
2. Promote two-dimensional code, can be two-dimensional code image link, can also be a string image stream, if you generate two-dimensional code, see: Using Phpqrcode to generate two-dimensional code.
Here's how:
/** * Generate poster * @param array parameter, including picture and text * @param string $filename generate poster file name, do not pass this parameter does not generate file, direct output picture * @return [type] [descriptio N] */function createposter ($config =array (), $filename = "") {//If you want to read a newspaper what is wrong, you can first note the header if (empty ($filename)) header (" Content-type:image/png "); $imageDefault = Array (' Left ' =>0, ' top ' =>0, ' right ' =>0, ' bottom ' =>0, ' width ' =>100, ' Heigh T ' =>100, ' opacity ' =>100); $textDefault = Array (' text ' = = ', ' Left ' =>0, ' top ' =>0, ' fontSize ' =>32,//font size ' fontcolor ' =& gt; ' 255,255,255 ',//Font Color ' angle ' =>0,); $background = $config [' Background '];//poster at the bottom of the background//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 Transparent imagecopyresampled ($imageRes, $background, 0,0,0,0,imagesx ($background), Imagesy ($background), Imagesx ($ Background), Imagesy ($background)); Processed the picture if (!empty ($config [' image '])) {foreach ($config [' image '] as $key = = $val) {$val = Array_merge ($imageDe Fault, $val); $info = getimagesize ($val [' url ']); $function = ' Imagecreatefrom '. Image_type_to_extension ($info [2], false); if ($val [' stream ']) {//If the string image stream is passed $info = getimagesizefromstring ($val [' url ']); $function = ' imagecreatefromstring '; } $res = $function ($val [' url ']); $resWidth = $info [0]; $resHeight = $info [1]; Build the artboard, zoom the image to the specified size $canvas =imagecreatetruecolor ($val [' width '], $val [' height ']; Imagefill ($canvas, 0, 0, $color); Key functions, parameters (target resource, source, start coordinate of target resource x, y, start coordinate of source resource x, y, Width of target resource w,h, Width height of source resource 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 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}//Handle text 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 Picture if (!empty ($filename)) {$res = imagejpeg ($imageRes, $filename, 90);//Save to local Imagedestroy ($imageRes); if (! $res) return false; return $filename; }else{imagejpeg ($imageRes); Display Imagedestroy ($imageRes) on the browser; }}
Use example one: generate a poster with a two-dimensional code
$config = Array ( ' image ' =>array (' url ' = = ' qrcode/qrcode.png ', //two-D code resource ' Stream ' = >0, ' left ' =>116, ' top ' =>-216, ' right ' =>0, ' Bottom ' =>0, ' width ' =>178, ' height ' =>178, ' opacity ' =>100 ) , ' background ' = ' bg/bg1.jpg ' //background map; $ filename = ' bg/'. Time (). JPG ';//echo createposter ($config, $filename); Echo Createposter ($config);
Use example two: generate posters with images, nicknames and QR codes
$config = Array (' Text ' =>array (' text ' = ' = ' nickname ', ' left ' =>182, ' top ' =>105, ' Fontpat H ' = ' qrcode/simhei.ttf ',//font file ' fontSize ' =>18,//font size ' fontcolor ' = ' 255,0,0 ',//font Color ' angle ' =>0,), ' Image ' =>array (' url ' = ' qrcode/qrcode.png ',//Picture resource path ' Lef T ' =>130, ' top ' =>-140, ' stream ' =>0,//Picture resource is the string image stream ' right ' =>0, ' bottom ' =>0, ' Width ' =>150, ' height ' =>150, ' opacity ' =>100), array (' url ' = = ' https://wx.qlogo.cn/m mopen/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);
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
thinkphp Implementing Payment (JSAPI Payment) steps
Php namespace namespace definition and import use case analysis