: This article mainly introduces how to use php to add text watermarks to images-implementation of object-oriented and process-oriented methods. if you are interested in PHP tutorials, refer. 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 * Operation memory image, add text watermark to image */publicfunction 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 */publicfunction 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 ();
The above describes how to use php to add text watermarks to images-implementation of object-oriented and process-oriented methods, including content, and hope to be helpful to PHP tutorials.