Use php to add text watermarks to images-implementation of object-oriented and facial processes

Source: Internet
Author: User
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 ();

Related Article

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.