Vous êtes sur la page 1sur 2

ENDMEMO

Home » PHP » Draw Picture :: PHP Tutorials Home ::


PHP Tutorial
PHP Array Functions
PHP Drawing Example • array_diff • array_intersect
• array_pop • array_push
The GD library is used to draw pictures in PHP. To enable GD, uncomment
• array_rand • array_search
extension=php_gd2.dll in the configuration file "php.ini".
• array_splice • sort
Let's have an example: • count • in_array
1 <?PHP • shuffle • array_flip
2 $width=500; • array_key_exists • array_keys
3 $height=300;
4 $im = imagecreatetruecolor($width,$height); • array_values • array_merge
5 $gray = imagecolorallocate($im, 102, 102, 102); • asort & arsort • ksort
6 $white = imagecolorallocate($im, 255, 255, 255);
7 $black = imagecolorallocate($im, 0, 0, 0);
PHP String Functions
8 $blue = imagecolorallocate($im, 0, 0, 255);
9 $red = imagecolorallocate($im, 255, 0, 0); • echo • explode
10 $green = imagecolorallocate($im, 0, 255, 0);
11 imagefill($im,0,0,$white); • concatenation • strcmp
12 • substr • preg_match
13 $x_ori = 40;
14 $y_ori = 60; • strpos • str_replace
15 $y_bottom = 60;
• strrev • strrpos
16 $xlen = $width - $x_ori - $x_ori * 1.5;
17 $ylen = $height - $y_ori - $y_bottom; • strtr • substr_replace
18
• preg_replace • preg_split
19 imageline($im,$x_ori,$y_ori+$ylen,$x_ori+$xlen,$y_ori+$ylen,$black); //x axis b
20 imageline($im,$x_ori,$y_ori,$x_ori,$y_ori + $ylen,$black); //y axis left • ereg • ereg_replace
21 imageline($im,$x_ori,$y_ori,$x_ori+$xlen,$y_ori,$black); //x axis top
22 imageline($im,$x_ori+$xlen,$y_ori,$x_ori+$xlen,$y_ori+$ylen,$black); //y axis r
• htmlspecialchars • print,sprintf
23
24 imagestring($im,2,$x_ori + $xlen/2 -10,$y_ori + $ylen + 20,"width", $black); PHP Loops and Conditions
25 imagestringup($im,2,$x_ori-20,$y_ori + $ylen/2 + 20,"height", $black);
• for loop • if else
26
27 $arry = array(1,50,100,150,200); • not equal • while
28
29 for ($i=0;$i<5;$i++)
• foreach • continue & break
30 {
31 $y = $y_ori + ($arry[$i] - $arry[0]) * $ylen/($arry[4]-$arry[0]); PHP Math Functions
32
• abs • exp
33 $style=array($white,$white,$gray,$white,$white);
34 imagesetstyle($im,$style); • floor & ceil • fmod
35 imageline($im,$x_ori+2,$y,$x_ori + $xlen -2,$y,IMG_COLOR_STYLED);
36 imagestring($im,2,$x_ori + $xlen + 5,$y-7,number_format($arry[$i]), $black);
• log • max & min
37 } • pow • sqrt
38
39 imagejpeg($im,"gd.jpg",65); • sin • cos
40 imagedestroy($im); • tan • round
41 ?>
PHP Filesystem Functions
• copy • delete, unlink
The picture looks like follows:
• download url • read file
• write file • dirname
• scandir • mkdir
• is_file • file_exists

PHP Data Types


• string • number
• array • associative array
• variables • date & time

PHP General Topics


• ajax • cookie
• comments • constants
• die • heredoc
• xml parse • mysql connect
• global variables • form validation
• header url • draw images
• pass by reference • sessions
• clone • object, class
• mail • regular expression

To print the picture on the screen instead of saving it as a file:


1 <?PHP
2 header('Content-type: image/jpeg');
3 imagejpeg($im);
4 ?>

Draw line:
1 imageline($im,x1,y1,x2,y2,color);

Draw dashed line:


1 $style=array($white,$white,$gray,$white,$white); //dashed style
2 imagesetstyle($im,$style);
3 imageline($im,$x_ori+2,$y,$x_ori + $xlen -2,$y,IMG_COLOR_STYLED);

Draw rectangle:
1 imagerectangle($im,x1,y1,x2,y2,color);

Draw text:
1 imagestring($im,font,x,y,$string,color); //draw horizontal text
2 imagestringup($im,font,x,y,$string,color); //draw vertical text

Draw circle and ellipse:


1 imageellipse($im,x,y,width,height,color);
2 imagefilledellipse($im,x,y,width,height,color);

Draw polygon:
1 <?PHP
2 $arr=array(15,10,5,20,30,15);
3 imagepolygon($im,$arr,points,color);
4 ?>

Click here for complete list of GD functions.

endmemo.com © 2016 Terms of Use

Vous aimerez peut-être aussi