How PHP generates promotion posters and how php promotes posters

Source: Internet
Author: User
Tags imagejpeg

How PHP generates promotion posters and how php promotes posters

This example describes how to generate a promotion poster using PHP. We will share this with you for your reference. The details are as follows:

There is often a need to generate a promotion poster that contains the specified QR code. After sharing the QR code, you can determine the user recommendation relationship.

After careful analysis, the necessary elements of the promotion poster are the poster background image and the QR code, both of which are easy to generate, however, to combine the two into a two-dimensional graph, you must save the two to the local file for easy sharing. This is difficult. In H5, you can use canvas to draw similar functions, but there are many limitations in small programs. Then we generate a poster directly in the background and call the poster directly at the foreground.

Preparations:

1. The background image of the poster is generally stored on the server and read locally by the program;
2. Promote the QR code, which can be a QR code image link or a string image stream. If you generate a QR code yourself, see: generate a QR code using phpqrcode.

The method is as follows:

/*** Generate the poster * @ param array parameter, including the image and text * @ param string $ filename to generate the poster file name. If this parameter is not set, no file is generated, directly output the image * @ return [type] [description] */function createPoster ($ config = array (), $ filename = "") {// if you want to see what error is reported, you can comment out the 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 ('TE Xt '=> '', 'left' => 0, 'top' => 0, 'fontsize' => 32, // font size 'fontcolor' => '123 ', // font color 'angle '=> 0,); $ background = $ config ['background']; // bottom-layer background of the poster // method $ backgroundInfo = getimagesize ($ background); $ backgroundFun = 'imagecreatefrom '. image_type_to_extension ($ backgroundInfo [2], false); $ background = $ backgroundFun ($ background); $ backgroundWidth = imagesx ($ background); // The background width $ backgroundHeight = im Agesy ($ 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); // 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 the string image stream is passed $ info = getimagesizefromstring ($ val ['url']); $ function = 'imagecreatefromstring ';} $ res = $ function ($ val ['url']); $ resWidth = $ info [0]; $ resHeight = $ Info [1]; // create a canvas, scale the image to the specified size $ canvas = imagecreatetruecolor ($ val ['width'], $ val ['height']); imagefill ($ canvas, 0, 0, $ color); // key functions, parameters (Starting coordinates x, y, source resource start coordinate x, y, target resource width and height w, h, source resource width and 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 the 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 }}// process 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']) ;}// generates an image if (! Empty ($ filename) {$ res = imagejpeg ($ imageRes, $ filename, 90); // save it to the local imagedestroy ($ imageRes); if (! $ Res) return false; return $ filename;} else {imagejpeg ($ imageRes); // display imagedestroy ($ imageRes) on the browser );}}

Example 1:Generate a poster with a QR code

$ Config = array ('image' => array ('url' => 'qrcode/qrcode.png ', // QR code resource 'stream' => 0, 'left' => 116, 'top' =>-216, 'right' => 0, 'bottom '=> 0, 'width' => 178, 'height' => 178, 'opacity '=> 100), 'background' => 'bg/bg1.jpg' // background image); $ filename = 'bg/'.time().'.jpg '; // echo createPoster ($ config, $ filename); echo createPoster ($ config );

Example 2:Generate posters with images, nicknames, and QR codes

$ Config = array ('text' => 'nicknames ', 'left' => 182, 'top' => 105, 'fontpath' => 'qrcode/simhei. ttf', // font file 'fontsize' => 18, // font size 'fontcolor' => '100, 255 ', // font color 'angle '=> 0,), 'image' => array ('url' => 'qrcode/qrcode.png ', // image resource path: 'left' => 130, 'top' =>-140, 'stream' => 0, // whether the image resource is a string image stream 'right' => 0, 'bottom '=> 0, 'width' => 150, 'height' => 150, 'opacity '=> 100), array ('url' => 'https: // response, '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 );

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.