PHP enables random watermark image generation,

Source: Internet
Author: User

PHP enables random watermark image generation,

Based on the GD graphics library of PHP, you can generate an image yourself. Only the GD library of first-level knowledge is available for instance learning.

I. Requirements

The layout of the website uses a style similar to the MOOC course list. Each course is a banner diagram. The following figure shows the title and description. Because the number of courses is too large to have a special banner designed for all courses, you need to follow certain rules to generate pictures by yourself (this is intended to be solved using div layout, but div + img is not well controlled in the responsive layout ).

Generated:

Ii. Tools & Materials

1. Enable GD Graphics Library Extension in PHP
2. Prepare multiple small watermark Images
3. Obtain the background color RGB value of the pre-generated image.

Iii. Code

In the process of generating images, the Code provides detailed comments.

Class GenerateRandomImage {/** @ var integer image width */public $ imgWidth = 272;/** @ var integer image height */public $ imgHeight = 162; /** @ var generate different background colors based on different types, currently, the following types are left: blue, purple, yellow, green, gray, and earth yellow */public $ type = ''; /** @ var the text to be displayed on the image */public $ text = '';/** @ var integer the font size of the text on the image */public $ fontSize = 16; public function _ construct ($ type, $ text) {$ this-> type = $ type; $ this-> text = $ text ;} /*** create and generate a random image * @ author bignerd * @ since 2017-03-21T14: 49: 41 + 0800 */public function createImg () {/** @ var create an air conditioner color palette with the specified image size $ image = imagecreate ($ this-> imgWidth, $ this-> imgHeight ); $ rgb = $ this-> getBackground ($ this-> type);/** @ var creates a background color for the image */$ backgroundColor = imagecolorallocate ($ image, $ rgb ['R'], $ rgb ['G'], $ rgb ['B']); /** @ var create a text white font */$ textColor = imagecolorallocate ($ image, 255,255,255 ); /** @ var font file path */$ font = $ _ SERVER ['document _ root']. '/public/font/simhei. ttf'; $ x = 18; // text start position x coordinate $ y = 50; // text start position y coordinate/** text write image */$ angle = 0; // angle 0 imagettftext ($ image, $ this-> fontSize, $ angle, $ x, $ y, $ textColor, $ font, $ this-> text ); /** @ var watermark image path **/$ waterImgPath = $ this-> randWaterImage ();/** @ var to obtain image information, the returned value $ waterInfo [2] is an image type constant */$ waterInfo = getimagesize ($ waterImgPath);/** @ var converts an image type constant to a real type, for example, png */$ waterType = image_type_to_extension ($ waterInfo [2], false); // obtain the file type $ createImageFunc = 'imagecreatefrom '. $ waterType; /** @ var create a watermark image copy $ createImageFunc dynamically generates a pre-called image creation function based on the image type */$ mask = $ createImageFunc ($ waterImgPath ); $ posX = $ this-> imgWidth-$ waterInfo [0]; // watermark image, x coordinate of the position in the target image $ posY = $ this-> imgHeight-$ waterInfo [1]; // watermark image, y coordinate of the position in the target image/** set the http Request Response Type to image/png for direct display as image */header ("Content-Type: image/png "); /** copy the watermark image to the created image */imagecopy ($ image, $ mask, $ posX, $ posY, 0, 0, $ waterInfo [0], $ waterInfo [1]); imagepng ($ image); // enter the image to the browser or file imagedestroy ($ image ); // destroy the image}/*** rgb value of the image background color * @ author bignerd * @ since 2017-03-21T14: 50: 16 + 0800 */public function getBackground () {$ background = ['1' => ['R' => 0, 'G' => 160, 'B' => 233], '2' => ['R' => 198, 'G' => 0, 'B' => 110], '3' => ['R' => 237, 'G' => 109, 'B' => 0], '4' => ['R' => 33, 'G' => 148, 'B' => 75], '5' => ['R' => 63, 'G' => 58, 'B' => 57], '6' => ['R' => 202, 'G' => 162, 'B' => 101],]; return $ background [$ this-> type];}/*** random watermark image path * @ author bignerd * @ since 2017-03-21T14: 51: 00 + 0800 * @ return path */public function randWaterImage () {$ folder = ['1' => 'product', '2' => 'team ', '3' => 'Architecture ', '4' => 'developer', '5' => 'test', '6' => 'engineer ']; $ targetFolder = $ _ SERVER ['document _ root']. '/public/images/role /'. $ folder [$ this-> type]. '/'.rand(1,382.16.'.png'; return $ targetFolder ;}}$ image = new GenerateRandomImage (1, "Awesome MySql Data Architecture"); $ image-> createImg ();

In this way, you can use the On the page to directly display the image.

Note:A problem encountered during the process: If the watermark image is a transparent png image, it will be displayed as a white background when the watermark image is copied to the image, and it cannot be transparently integrated with our set image background, therefore, the same color must be applied to random watermark images.

Iv. Summary

This small example uses a simple step to generate an image and display it directly in the browser. You can also add a second parameter to imagepng, that is, the path, to save the image. You can create an image, add a text watermark, or image watermark to the image by learning the methods in several GD libraries in the example.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.