PHP to generate promotional posters method sharing

Source: Internet
Author: User
Tags imagejpeg processing text
This article mainly for you to share is how to use PHP to generate promotional posters of the method and several sample code, the need for small partners can refer to the next!

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.

Here's how:

/** Generate a poster @param the array parameter, including the picture and text @param string $filename generate the poster file name, do not pass this parameter does not generate the file, directly output the picture @return [Type] [description]*/ function Createposter ($config =array (), $filename = "") {//If you want to read a newspaper what is wrong, you can first note this headerif (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,//font size ' fontcolor ' = ' 255,255,255 ',//Font Color ' angle ' =>0, ' $background = $config [' Background '];// Poster bottom 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)),//processing of the picture 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 a string image stream is passed $info = getimagesizefromstring ($val [' url ']); function = ' imagecreatefromstring ';} $res = $function ($val [' url ']), $resWidth = $info [0]; $resHeight = $info [1];//build artboards, scale pictures to specified dimensions $canvas= Imagecreatetruecolor ($val [' width '], $val [' height ']); Imagefill ($canvas, 0, 0, $color);//Key function, parameter (target resource, source, start coordinate of target resource X, y , the source resource's start coordinates x, Y, the target resource's width height w,h, the source resource's width height 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}}//processing 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;}}

Summary: This achieves a simple can be statistical promotion of the number of fans of the invitation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.