1024programmer PHP Play with PHP’s GD library Wang Zebin

Play with PHP’s GD library Wang Zebin

The GD library in Php mainly provides a set of Php graphics processing functions. It is very powerful and can produce dazzling effects. In Web2.0 applications, you will encounter it more or less. So when you apply for a Web2.0 website PHP engineer, you will often be asked about the GD library. In fact, the GD library functions in php are relatively simple, and you generally know their functions

The GD library in Php mainly provides a set of Php graphics processing functions. It is very powerful and can produce dazzling effects. In Web2.0 applications, we will encounter it more or less. So when you apply for a Web2.0websitephp engineer, you will often be asked about the GD library. In fact, the GD library functions in php are relatively simple. You only need to have a general understanding of their functions. I have provided a few small examples to help you remember and understand.

The source code of the following examples can be downloaded at the following address:http://wangzebin.blog.51cto.com/attachment/200902/653300_1235091601.zip.

1. Chinese character verification code

Usually on websites, verification codes like to use a combination of numbers or characters, but for some valuable website services, more stringent verification codes may be required. For example, 163mailbox malicious registration verification and QQnumber application verification all use Chinese character authentication to varying degrees. Using php‘s GD library, you can easily do it with a few lines of code. If you want to output Chinese characters or special characters, you need to provide TTF font library.

The effect is as follows:

The code is as follows:

<?php

//Define output as image type

header(“content-type:image/gif”);

//Create image

$picture = imagecreate(150,40);

//Define black and white colors

$cl_black = imagecolorallocate($picture,0,0,0);

$cl_white = imagecolorallocate($picture,255,255,255);

//Specify font library

$lib_fOnt= “c://WINDOWS//fonts//simhei.ttf”;

//Define output font string, charactersutf-8encoding

$str_output = chr(0xE4).chr(0xB8).chr(0xAD);

//Write TTF Text to image

imagettftext($picture,20,-10,40,25,$cl_white,$lib_font,$str_output);

//Define output font string, charactersutf-8encoding

$str_output = chr(0xE5).chr(0x9B).chr(0xBD);

//WriteTTF text into the picture

imagettftext($picture,20,10,80,30,$cl_white,$lib_font,$str_output);

//Create GIF Graphic

imagegif($picture);

//End graphics and release memory space

imagedestroy($picture);

?>

2. Picture thumbnail

The image size conversion function is very commonly used and is used by many websites. For example, on common SNS websites, users can upload pictures and set their own avatars. Since the images provided by users are of different sizes, the website needs to process them into a uniform size when users upload them. Using the GD library of php, you can easily do it with just a few lines of code. The effect is as follows:

The code is as follows:

<?php

//$srcFileOriginal file,$dstW, $dstH are the width and height of the small image.

function makethumb($srcFile,$dstW,$dstH){

$data_pic = GetImageSize($srcFile,&$info);

switch ($data_pic[2]) {

case 1: //Picture type, 1isGIFpicture

$old_pic = @ImageCreateFromGIF($srcFile);

break;

case 2: //Picture type, 2isJPGpicture

$old_pic = @imagecreatefromjpeg($srcFile);

break;

case 3: //Picture type, 3isPNGpicture

$old_pic = @ImageCreateFromPNG($srcFile);

break;

}

//Create a reduced image

$srcW = ImageSX($old_pic);

$srcH = ImageSY($old_pic);

$new_pic = ImageCreate($dstW,$dstH);

ImageCopyResized($new_pic,$old_pic,0,0,0,0,$dstW,$dstH,$srcW,$srcH);

//Output locked small picture

header(“content-type:image/gif”);

ImageJpeg($new_pic);

imagedestroy($new_pic);

imagedestroy($new_old);

}

makethumb(“1.jpg”,40,50);

?>

3. AsciiPictures

People often post some character pictures in textBBS, which feels cool. Here we provide one that uses phpGD Interested students can study the examples generated by the library. The effect is as follows:

Due to the length of the code, I will not post it again. You can download it directly from the link in this article.

In addition, it is relatively easy to watermark pictures and embed one picture into another picture. You can try it.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/play-with-phps-gd-library-wang-zebin-4/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索