Use php to add a text watermark to an image-implementation of the object-oriented and process-oriented methods 1: write a text watermark to the process to define the image watermark src001.png; get the image information $ infogetimagesize ($ src ); get the image extension $ typeimage_type_to_extension ($ info [2], false ); dynamically import images into memory $ fu using php to add text watermarks to images-implementation of object-oriented and process-oriented methods
1: Process-oriented compiling method
// Specify the image path $ src = '001.png '; // Obtain the image information $ info = getimagesize ($ src ); // Obtain the image extension $ type = image_type_to_extension ($ info [2], false); // dynamically import the image into the Memory $ fun = "imagecreatefrom {$ type }"; $ image = effecfun('001.png '); // specify the font color $ col = imagecolorallocatealpha ($ image, 255,255,255, 50); // specify the font content $ content = 'helloworld '; // add the image text imagestring ($ image, 5, 20, 30, $ content, $ col); // specify the input type header ('content-type :'. $ info ['Mime ']); // dynamically output the image to the browser $ func = "image {$ type}"; $ func ($ image ); // destroy the image imagedestroy ($ image );
2: Object-oriented implementation
Class Image_class {private $ image; private $ info;/*** @ param $ src: The image path * loads the image to the 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: fontsize * @ param $ x: the x position of the font in the image * @ param $ y: the y position of the font in the image * @ param $ color: the color of the font is an array containing rgba * @ param $ text: content to be added * operate on images in memory, add text watermarks to images */public function fontMark ($ fontsize, $ x, $ y, $ color, $ text) {$ col = imagecolorallocatealpha ($ this-> image, $ color [0], $ color [1], $ color [2], $ color [3]); imagestring ($ this-> image, $ fontsize, $ x, $ y, $ text, $ col);}/** output image to the browser */public function show () {header ('content-type :'. $ this-> info ['Mime ']); $ fun = 'image '. $ this-> info ['type']; $ fun ($ this-> image);}/*** destroy image */function _ destruct () {imagedestroy ($ this-> image) ;}// call the class $ obj = new Image_class('001.png '); $ obj-> fontMark (20, 20, 30, array (255,255,255, 60), 'Hello'); $ obj-> show ();