A method of randomly generating watermark image function in PHP

Source: Internet
Author: User
This article mainly for you to introduce the PHP generated random watermark image of the relevant data, with a certain reference value, interested in small partners can refer to

Based on the PHP GD graphics library, generate a picture yourself. Only the first knowledge of GD library, example learning.

First, the demand

The layout of the site is similar to the style of the class Web course list, each course is a banner map, the following is the title plus introduction. Because the number of courses is not designed specifically for all courses, it is necessary to generate the images yourself according to certain rules (this is intended to be solved with P layout, but p+img is not very banner in the responsive layout).

Generated by:

Second, tools & materials

1.PHP Open GD Graphics library extension
2. Prepare multiple small watermark maps
3. Get the background color RGB value of the pre-generated picture

Third, the Code

The process of generating the picture, detailed comments are made in the code.

Class generaterandomimage{/** @var integer Image Width */public $imgWidth = 272;  /** @var integer Image Height */public $imgHeight = 162;  /** @var produce different background colors depending on the type, leaving a type of blue, purple, yellow, green, gray, earthy yellow */public $type = ';  /** @var the text to be displayed on the picture */public $text = ';  /** @var The font size of text on an integer image */Public $fontSize = 16;    Public function __construct ($type, $text) {$this->type = $type;  $this->text = $text; }/** * Create random images * @author bignerd * @since 2017-03-21t14:49:41+0800 * * * Public Function createimg () {/** @v    AR creates an empty palette of the specified picture size $image = Imagecreate ($this->imgwidth, $this->imgheight);    $rgb = $this->getbackground ($this->type);    /** @var Create a background color for the image */$backgroundColor = Imagecolorallocate ($image, $rgb [' R '], $rgb [' G '], $rgb [' B ']);    /** @var Create white font for text */$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 picture */$angle = 0;//angle 0 imagettftext ($image, $this->fontsize, $angle, $x, $y, $textColor, $font, $this->t    EXT);    /** @var watermark Picture path **/$waterImgPath = $this->randwaterimage ();    /** @var Get picture information, return value $waterinfo[2] is a picture type constant */$waterInfo = getimagesize ($waterImgPath); /** @var Converts a picture type constant to a true type, such as png/$waterType = Image_type_to_extension ($waterInfo [2], false);//Gets the file type $createImageFu    NC = ' Imagecreatefrom '. $waterType;    /** @var Create a copy of a watermark picture $createImageFunc to dynamically generate a pre-called create picture function based on the picture type */$mask = $createImageFunc ($waterImgPath); $posX = $this->imgwidth-$waterInfo [0];//watermark Picture, the x-coordinate of the position in the target picture $posY = $this->imgheight-$waterInfo [1];//watermark picture, in the target diagram    The y-coordinate of the position in the Slice/** HTTP request response type set to Image/png for direct display as Picture */Header ("Content-type:image/png");    /** watermark picture copied to the created image */imagecopy ($image, $mask, $posX, $posY, 0, 0, $waterInfo [0], $waterInfo [1]); Imagepng ($image);//input image to browser or file Imagedestroy ($image);//Destroy picture}/** * Image background color RGB value * @author Bignerd * @siNCE 2017-03-21t14:50:16+0800 */Public Function Getbackground () {$background = [' 1 ' =>[' r ' =>0, ' g ' = =      , ' 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 Picture Path * @author bignerd * @since 2017-03-21t14:51:00+0800 * @return Path */Public function Randwater Image () {$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,38).    PNG ';  return $targetFolder; }} $image = new Generaterandomimage (1, "Live MySQL Data schema"); $image->createimg ();

This allows us to display the image directly in the page using the .

Note: The process encountered a problem: if the watermark picture is a transparent PNG image, that will be copied to the image image, will be displayed as a white background, and we set the image background can not be transparent fusion, so the random watermark image also need to do the same color processing.

Iv. Summary

This small example uses simple steps to generate a picture, which is displayed directly in the browser, or it can be given a second parameter, the path, to save the image Imagepng . So learn some of the examples in the GD library method, you can create a picture, add a text watermark to the picture, or a picture watermark.

The above is the whole content of this article, I hope that everyone's study has helped.


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.