Ubuntu: Install the gd extension of php step by encapsulating the verification code library.

Source: Internet
Author: User

Ubuntu: Install the gd extension of php step by encapsulating the verification code library.

I believe that many people directly copy a bunch of parameters to install the lamp environment. The installation may be successful or fail. If you are a newbie, you may encounter various errors. Even if the installation is successful, you may not know what the parameters are for. You just need to use the anti-formal dress.

This was also the case at the beginning, and I was totally overwhelmed. I had to study later (C language, Linux, Linux system programming, computer principles, etc.) before I could play with server configuration and understand the principles.

This article uses a classic verification code function to show you how to install extensions on demand !!!

To encapsulate a php Verification Code Class Library, the support for gd extension is required. Because my previous php was compiled and installed in a simplified manner without the support of the gd library, you must first install the php gd library.

To successfully install the gd library, you need to install many dependencies: zlib, png, jpeg, libiconv, and freetype.

1. zlib I have installed the php memcache extension before.

2. Install png

Wget http://prdownloads.sourceforge.net/libpng/libpng-1.5.4.tar.gz? Download

Music libpng-1.5.4.tar.gz \? Download libpng-1.5.4.tar.gz

Tar xf libpng-1.5.4.tar.gz

Cd libpng-1.5.4/

./Configure

Make & sudo make install

3. Install jpeg

Wget http://www.ijg.org/files/jpegsrc.v9b.tar.gz

Tar xf release src.v9b.tar.gz

Cd jpeg-9b/

./Configure

Make & sudo make install

4. Install libiconv

Wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz

Tar xf libiconv-1.15.tar.gz

Cd libiconv-1.15/

./Configure

Make & sudo make install

5. Install freeType

Wget https://download.savannah.gnu.org/releases/freetype/freetype-2.9.tar.gz

Tar xf freetype-2.9.tar.gz

Cd freetype-2.9/

./Configure

Make & sudo make install

6. Install the php gd extension.

Switch to the ext/gd directory under the php original code package

> Execute the phpize plug-in Module (/usr/local/php54/bin/phpize) first)

>. /Configure -- with-php-config =/usr/local/php54/bin/php-config-with-png-dir -- with-freetype-dir -- with-jpeg-dir- with-zlib-dir -- with-gd

> Make & sudo make install

 

Start encapsulating the php verification code library.

1 <? Php 2 3 class Captcha {4 // number of characters in the Verification Code 5 protected $ num; 6 // width 7 protected $ width; 8 // height 9 protected $ height; 10 // resource 11 protected $ im; 12 // type: 1: pure number, 2: Pure Letter 3: Mixed number and letter 13 protected $ codeType; 14 // The generated verification code string 15 protected $ codeString; 16 17 public function _ construct ($ _ num = 4, $ _ width = 150, $ _ height = 50, $ _ codeType = 3) {18 $ this-> num = $ _ num; 19 $ this-> width = $ _ width; 20 $ this-> height = $ _ heig Ht; 21 $ this-> codeType =$ _ codeType; 22 $ this-> codeString = $ this-> createCodeString (); 23} 24 25 public function _ get ($ name) {26 if ($ name = 'codestring') {27 return $ this-> codeString; 28} 29 return false; 30} 31 32 protected function createCodeString () {33 switch ($ this-> codeType) {34 case 1: 35 $ code = $ this-> createNumCode (); 36 break; 37 case 2: 38 $ code = $ this-> createCharCode (); 39 Break; 40 case 3: 41 $ code = $ this-> createNumCharCode (); 42 break; 43 default: 44 die ("no other verification code types currently, you can inherit this class to add "); 45} 46 return $ code; 47} 48 49 protected function createNumCode () {50 $ numString = join ("", range (0, 9); 51 return substr (str_shuffle ($ numString), 0, $ this-> num); 52} 53 54 protected function createCharCode () {55 $ charString = join ("", range ('A', 'z'); 56 $ charStr Ing. = strtoupper ($ charString); 57 return substr (str_shuffle ($ numString), 0, $ this-> num); 58} 59 60 protected function createNumCharCode () {61 $ numCharString = join ("", range (0, 9); 62 $ charString = join ("", range ('A', 'z ')); 63 $ charString. = strtoupper ($ charString); 64 $ mixedString = $ numCharString. $ charString; 65 return substr (str_shuffle ($ mixedString), 0, $ this-> num); 6 6} 67 68 protected function createCanvas () {69 $ this-> im = imagecreatetruecolor ($ this-> width, $ this-> height ); 70} 71 72 protected function fillCanvas ($ type) {73 switch ($ type) {74 case 1: 75 $ color = imagecolorallocate ($ this-> im, mt_rand (0,150 ), mt_rand (0,150), mt_rand (0,150); 76 break; 77 case 2: 78 $ color = imagecolorallocate ($ this-> im, mt_rand (150,255), mt_rand (150,255) ), Mt_rand (150,255); 79 break; 80 default: 81 die ("unsupported Filling Type"); 82 break; 83} 84 imagefill ($ this-> im, 0, 0, $ color); 85} 86 87 protected function drawCodeString () {88 for ($ I = 0; $ I <$ this-> num; $ I ++) {89 $ fontColor = imagecolorallocate ($ this-> im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 )); 90 $ char = $ this-> codeString [$ I]; 91 $ x = ($ I * ($ this-> width)/$ this-> num) + Mt_rand (5, 10); 92 $ y = mt_rand (0, $ this-> height/2) + mt_rand (5, 10 ); 93 imagestring ($ this-> im, 5, $ x, $ y, $ char, $ fontColor); 94} 95} 96 97 protected function renderImage () {98 header ("Content-type: image/png"); 99 imagepng ($ this-> im); 100} 101 102 protected function fillDisturb () {103 for ($ I = 0; $ I <100; $ I ++) {104 $ color = imagecolorallocate ($ this-> im, mt_rand (0,255 ), Mt_rand (0,255), mt_rand (0,255); 105 imagesetpixel ($ this-> im, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ color); 106} 107} 108 109 public function render () {110 // create a canvas/background 111 $ this-> createCanvas (); 112 // fill the canvas color 113 $ this-> fillCanvas (1); 114 // fill the interference element 115 $ this-> fillDisturb (); 116 // fill in the verification code 117 $ this-> drawCodeString (); 118 // display the verification code 119 $ this-> renderImage (); 120} 121 122 $ capt = new Captcha (); 124 // echo $ capt-> codeString. PHP_EOL; 125 $ capt-> render (); 126?>

 

Summary:

1. Review the encapsulation of the Class Library

2. Understand the principle of extended Installation

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.