Php object-oriented and process-oriented methods: adding text watermarks to images _ php instance

Source: Internet
Author: User
When using many programming languages, you can only use object-oriented or process-oriented programming methods. In PHP, You can freely choose or mix images. The following two methods are used to add watermarks to images: Object-oriented and process-oriented, for more information, see the process-oriented method currently used by most PHP programmers, because parsing WEB pages is very "procedural" (from one tag to another ). It is natural to Process Code during the embedded process in HTML, so PHP programmers usually use this method.

If you are new to PHP, writing code in a process-oriented style is probably your only choice. However, if you often go to PHP forums and newsgroups, you should see articles about "objects. You may also see tutorials on How to Write object-oriented PHP code. Or you may have downloaded some ready-made class libraries and tried to instantiate the objects and use the class methods-although you may not really understand why these classes work, or why do we need to use object-oriented methods to implement functions.

Should we use the "Object-Oriented" style or the "process-oriented" style? Both parties have their own supporters. Discussions like "objects are inefficient" or "objects are great" are also heard. This article does not try to determine which of the two methods has absolute advantages, but to find out the advantages and disadvantages of each method.

1. Object-oriented implementation using php to add watermarks to images

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 ();

2: process-oriented writing: Add watermarks to images using php:

// 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 );

The above code example introduces php object-oriented and process-oriented methods to add text watermarks to images.

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.