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 add text watermark to pictures by php

2025-03-11 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 add text watermark to the picture, the article is very detailed, has a certain reference value, interested friends must read it!

The vast majority of PHP programmers today use a process-oriented approach because parsing WEB pages is inherently "procedural" (from one tag to another). Embedding process processing code in HTML is a straightforward and natural approach, so PHP programmers usually use this approach.

If you are new to PHP, writing code in a process-oriented style is probably your only option. But if you often go to PHP forums and newsgroups, you should see articles about "objects". You may also have seen tutorials on how to write object-oriented PHP code. Or you may have downloaded some off-the-shelf class libraries and tried to instantiate objects and use class methods-although you may not really understand why these classes work. Or why you need to use object-oriented methods to implement functions.

Should you use an "object-oriented" style or a "process-oriented" style? Both sides have supporters. Comments such as "the object is inefficient" or "the object is very good" are also heard from time to time. This article does not try to easily determine which of the two methods has an absolute advantage, but to find out the advantages and disadvantages of each method.

1: object-oriented implementation using php to add watermark to pictures

Class Image_class {private $image; private $info; / * * @ param $src: picture path * load pictures into memory * / function _ _ construct ($src) {$info = getimagesize ($src); $type = image_type_to_extension ($info [2], false); $this- > info = $info; $this- > info ['type'] = $type; $fun = "imagecreatefrom". $type; $this- > image = $fun ($src) } / * * @ param $fontsize: font size * @ param $x: font x position in the picture * @ param $y: font y position in the picture * @ param $color: the font color is an array containing rgba * @ param $text: content to be added * manipulate the picture in memory, add text watermark to the picture * / public function fontMark ($fontsize,$x {$col = imagecolorallocatealpha ($this- > image,$color [0], $color [1], $color [2], $color [3]) Imagestring ($this- > image,$fontsize,$x,$y,$text,$col);} / * * output pictures to browser * / public function show () {header ('content-type:'. $this-> info ['mime']); $fun='image'. $this- > info ['type']; $fun ($this- > image);} / * destroy pictures * / function _ destruct () {imagedestroy ($this- > image);}} / / calls to a pair of classes $obj = new Image_class (' 001.png'); $obj- > fontMark (array 20,255), 'hello'); $obj- > show ()

2: process-oriented programming uses php to add watermarks to images:

/ / specify the image path $src = '001.pngfight / get the picture information $info = getimagesize ($src); / / get the picture extension $type = image_type_to_extension ($info [2], false); / / dynamically import the picture into memory $fun = "imagecreatefrom {$type}"; $image = $fun (' 001.png'); / / specify the font color $col = imagecolorallocatealpha ($image,255,255,255,50); / / specify the font content $content = 'helloworld' / / add text to the picture imagestring ($image,5,20,30,$content,$col); / / specify the input type header ('Content-type:'.$info [' mime']); / / dynamically output the picture to the browser $func = "image {$type}"; $func ($image); / / destroy the picture imagedestroy ($image); these are all the contents of the article "how to add text watermark to a picture 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