Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate Random watermark Images by PHP

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces PHP how to generate random watermark images, the article is very detailed, has a certain reference value, interested friends must read it!

I. demand

The layout of the website is similar to the style of the course list of Mutu. each course is a banner diagram with a title and a brief introduction below. Because of the large number of courses, there is no special banner designed for all courses, so you need to generate your own pictures according to certain rules (originally intended to be solved with div layout, but div+img is not well controlled in responsive layout).

Second, tools & materials

1.PHP opens the extension of GD graphics library

two。 Prepare multiple small watermarks

3. Get the background color RGB value of the pre-generated picture

III. Code

The process of generating pictures is annotated in detail in the code.

Class GenerateRandomImage {/ * * @ var integer picture width * / public $imgWidth = 272; / * * @ var integer picture height * / public $imgHeight = 162; / * * @ var generates different background colors according to type. Currently, leave a type of blue, purple, × × ×, green, gray, clay × × * / public $type =''. / * * @ var text to be displayed on the picture * / public $text ='; / * * @ var integer the font size of the text on the picture * / public $fontSize = 16; public function _ _ construct ($type, $text) {$this- > type = $type; $this- > text = $text } / * create and generate random images * @ author bignerd * @ since 2017-03-21T14:49:41+0800 * / public function createImg () {/ * * @ var create an air-conditioned swatch of specified picture size $p_w_picpath = p_w_picpathcreate ($this- > imgWidth, $this- > imgHeight); $rgb = $this- > getBackground ($this- > type) / * * @ var create a background color for the picture * / $backgroundColor = p_w_picpathcolorallocate ($p_w_picpath, $rgb ['r'], $rgb ['g'], $rgb ['b']); / * * @ var create a text white font * / $textColor = p_w_picpathcolorallocate ($p_w_picpath, 255,255,255) / * * @ var font file path * / $font = $_ SERVER ['DOCUMENT_ROOT'].' / public/font/simhei.ttf'; $x = 18 angle / text start position x coordinates $y = 50 position text start position y coordinates / * * text write picture * / $text = 0 / / Angle 0 p_w_picpathttftext ($p_w_picpath, $this- > fontSize, $angle, $x, $y, $textColor, $font, $this- > text); / * * @ var watermark image path * * / $waterImgPath = $this- > randWaterImage (); / * @ var to obtain image information, the return value of $waterInfo [2] is the image type constant * / $waterInfo = getp_w_picpathsize ($waterImgPath) / * * @ var converts the image type constant to a real type, such as png * / $waterType = p_w_picpath_type_to_extension ($waterInfo [2], false); / / get the file type $createImageFunc = 'p_w_picpathcreatefrom'.$waterType / * * @ var create a copy of the watermark image $createImageFunc is the image creation function * / $mask = $createImageFunc ($waterImgPath) that is dynamically generated according to the picture type; $posX = $this- > imgWidth-$waterInfo [0]; / / the watermark image, the x coordinates of the location in the target picture $posY = $this- > imgHeight-$waterInfo [1] / / watermark image, the y coordinate of the location in the target picture / * * http request response type is set to p_w_picpath/png so that it can be directly displayed as image * / header ("Content-Type:p_w_picpath/png") / * * copy the watermark image to the created p_w_picpath * / p_w_picpathcopy ($p_w_picpath, $mask, $posX, $posY, 0,0, $waterInfo [0], $waterInfo [1]); p_w_picpathpng ($p_w_picpath); / / enter the image to the browser or file p_w_picpathdestroy ($p_w_picpath) / / destroy the picture} / * the RGB value of the background color of the picture * @ author bignerd * @ since 2017-03-21T14:50:16+0800 * / public function getBackground () {$background = ['1movie = > [' rDNA = > 0rem 'glossy = > 160 minebook = > 233],' 2camera = > ['RGB = > 1988jurisdiction baud = > 0rem camera = > 110] '3Please = > [' ritual = > 237 primacy = > 109 recordings = > 0], '4primes = > [' rDNA = > 33, 'gems = > 148 camera breads = > 75],' 5primes = > 63, 'gems = > 58,' bundles = > 57], '6primes = > [' rDNA = > 202 paramedics = > 162 beacons = > 101],]] Return $background [$this- > type] } / * * Random watermark image path * @ author bignerd * @ since 2017-03-21T14:51:00+0800 * @ return path * / public function randWaterImage () {$folder = ['1watermark = >' product','2'= > 'team','3'= >' architecture','4'= > 'developer','5'= >' test' '6percent = > 'engineer'] $targetFolder = $_ SERVER ['DOCUMENT_ROOT'].' / public/p_w_picpaths/role/'.$folder [$this- > type].'/ '.rand (1pc38).' .png'; return $targetFolder;}} $p_w_picpath = new GenerateRandomImage (1, "bearable MySql data architecture"); $pendant picpaths-> createImg ()

So we can use it directly on the page.

To display the picture directly.

The above is all the content of the article "how to generate random watermark images by PHP". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report