PHP Object-oriented and process-oriented two methods to add a text watermark to a picture, _php tutorial

Source: Internet
Author: User

PHP Object-oriented and process-oriented two methods to add a text watermark to the picture,


Most PHP programmers now use a process-oriented approach because parsing the Web page itself is very "procedural" (from one label to another). Embedding the Process code in HTML is straightforward and natural, so PHP programmers typically use this approach.

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

Should you use the "Object-oriented" style or the "process-oriented" style? Each side has its supporters. Such arguments as "objects are inefficient" or "objects are very good" are also heard. This article does not attempt to easily determine which of the two methods has the absolute advantage, but to find out the pros and cons of each method.

1: Object-oriented implementation using PHP to add a watermark method to a picture

Class Image_class {private $image;  Private $info;    /** * @param $src: Picture path * Load image 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: The x position of the font in the picture * @param $y: The y position of the font in the picture * @param $color: The color of the font is an array containing RGBA * @param $text: What you want to add * manipulate images in memory, add text watermarks to pictures */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 picture to the browser */Public function Show () {header (' Content-type: '. $this, info[' mime '); $fun = ' image '.    $this->info[' type '];  $fun ($this->image);  }/** * Destroy picture */function __destruct () {Imagedestroy ($this->image); }}//call to class $obj = New Image_class (' 001.png '); $obj->fontmark (20,20,30,array (255,255,255,60), ' hello '); $obj->show (); 

2: Process-oriented writing use PHP to add a watermark method to a picture:

Specify picture path $src = ' 001.png ';//Get picture information $info = getimagesize ($SRC);//Get picture extension $type = image_type_to_extension ($info [2],false) ;//Dynamically import the picture into memory $fun = "imagecreatefrom{$type}"; $image = $fun (' 001.png ');//Specify 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);// Specifies the input type header (' Content-type: '. $info [' MIME ']);//dynamic output image to the browser $func = "image{$type}"; $func ($image);// Destroy picture Imagedestroy ($image);

The above code example is the introduction of PHP object-oriented and process-oriented two methods to add text watermark image, I hope you like.

http://www.bkjia.com/PHPjc/1049125.html www.bkjia.com true http://www.bkjia.com/PHPjc/1049125.html techarticle PHP Object-oriented and process-oriented methods to add text watermark to the picture, most of the PHP programmers now use a process-oriented approach, because the parsing Web page itself is very "procedural" ...

  • 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.