PHP simple graphics processing, PHP simple graphics processing _php tutorial

Source: Internet
Author: User

Simple graphics processing in PHP, simple graphics processing in PHP


Simple graphics Processing in PHP

The 134 of the Ling Hao

1. Loading GD library

The GD library is an open library of dynamically created images and source code that can be downloaded from the official website http://www.boutell.com/gd. Currently, the GD library supports a variety of image formats, such as GIF, PNG, JPEG, Wbmp, and XBM, for image processing.

The GD library is installed by default in PHP 5, but to activate the GD library, you must modify the php.ini file. Put the "; extension=php_gd2.dll" option in the file before the semicolon ";" Delete, save the modified file and restart the Apache server to take effect.

2. Create a simple image

The use of the GD2 function library enables the processing of various graphic images. Creating a canvas is the first step in creating an image using the GD2 library, and no matter what image you create, you first need to create a canvas, and everything else will be done on this canvas. Create a canvas in the GD2 library, which can be implemented through the Imagecreate () function.

Use the Imagecreate () function to create a canvas with a width of 200 pixels, a height of 60 pixels, and set the canvas color RGB (225,66,159), and finally output a GIF-formatted image with the following code:

$im = Imagecreate (200,60); Create a canvas
$white = Imagecolorallocate ($im, 225,66,159); Set the background color of the canvas to light green
Imagegif ($im); Output image
?>

3. Use the GD2 function to add text to a photo

The GD Library in PHP supports Chinese, but it must be passed in UTF-8 format, if the imagestring () function is used to draw the Chinese string directly, it is garbled because GD2 can only receive UTF-8 encoding format for Chinese, and the English font is used by default. Therefore, in order to output Chinese strings, the Chinese string must be transcoded and the font used for the medium characters should be set. Otherwise, the output can only be garbled.

Use the Imagettftext () function to output the text "This is a test" to the image with the following code:

Header ("Content-type:image/jpeg"); Defining the output as an image type
$im =imagecreatefromjpeg ("images/photo.jpg"); Loading photos
$textcolor =imagecolorallocate ($im, 56,73,136);//Set font color to blue, value to RGB color value
$FNT = "C:/windows/fonts/simhei.ttf"; Defining fonts
$motto =iconv ("gb2312", "Utf-8", "This is a Test"); Defining output font strings
Imagettftext ($im, 220,0,480,340, $textcolor, $fnt, $motto); Write TTF text to the diagram
Imagejpeg ($im); Create a JPEG graphic
Imagedestroy ($im); End graph, free memory space
?>

4.PHP Generate Verification Code

Create a checks.php file use the GD2 function in the file to create a 4-bit verification code and save the generated verification code to the session:

!--? php
session_start ();
Header ("Content-type:image/png");//Set format for creating images
$image _width=70;//Set image width
$image _height=18;//Set Image height
Srand (Microtime () *100000); Set the seed for random number
for ($i =0; $i <4; $i + +) {//loop outputs a 4-bit random number
$new _number.=dechex (rand (0,15));
}
$_session[check_checks]= $new _number;//Writes the obtained random number verification code to the SESSION variable

$num _image=imagecreate ($image _width,$ Image_height); Create a canvas
Imagecolorallocate ($num _image,255,255,255);//Set the color of the canvas
for ($i =0; $i $font =mt_rand (3,5);//Set random font
$x =mt_rand (1,8) + $image _width* $i/4;// Sets the x-coordinate where the random character is located
$y =mt_rand (1, $image _HEIGHT/4);//sets the y-coordinate of the location of the random character
$color =imagecolorallocate ($num _image,mt_ Rand (0,100), Mt_rand (0,150), Mt_rand (0,200)); Sets the color of the character
Imagestring ($num _image, $font, $x, $y, $_session[check_checks][$i], $color);//horizontal output character
}
Imagepng ( $num _image); Generate image in PNG format
Imagedestroy ($num _image);//Release image resource
?

Create a user-signed form and call checks.php to output the contents of the image in the form:

Session_Start ();
if ($_post["Submit"]!= "") {
$checks =$_post["checks"];
if ($checks = = "") {
echo "";
}
if ($checks ==$_session[check_checks]) {
echo "";
}else{
echo "";
}
}
?>



Application of Rand function







http://www.bkjia.com/PHPjc/1064067.html www.bkjia.com true http://www.bkjia.com/PHPjc/1064067.html techarticle PHP simple graphics processing , PHP simple graphics processing php simple graphics processing meter should be 134 Linghao 1. Load GD Library GD Library is an open dynamic image creation, source code Public Library of Functions ...

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