PHP-image processing

PHP is not limited to processing text data, it can also create dynamic images in different formats

*Images can also be processed through the GD library, which are processed in the memory first, and then output to the browser in the form of a file stream or saved in the disk of the server after processing.

1. Create an image
imagecreatetruecolor( $width, $height ); //Create a true color image

2. Draw the image
imagecolorallocate( resource image, R,G,B) //Allocate color
imgefill(resource img, x ,y ,$color); //area fill, x and y are coordinates, the upper left corner of the image is (0, 0)

3. Output image
imagejpeg( ); //Output to the browser
header (); //Tell the browser to return a picture, note : before the header function, no content can be output

4. Release resources
imagedestroy(); //Destroy an image

<?php
header('content-type:image/jpeg');
$img = imagecreateturecolor( 200,200); //创建一个长和高都为200的真彩色图像
$color1 = imagecolorallocate( $img ,50,50,50);   // 分配颜色 
imagefill( $img ,0,0, $color1);   //从(0,0)坐标开始填充
imagejpeg($img);
imagedestroy($img);

Image processing-making verification codes

<?php
header('cont-type:image/jpeg');
$width = 120;
$height = 40;
$element= arry('a','b','c','d','f','g','h','i','j','k','l');
//设置一个数组,包括从A到Z的字符

$string=' ';   //初始化string
for($i=0;$i<4;$i++)
{
	$string=$element[rand(0,count($element-1)];
}
//用for循环,执行随机随机字母,共生成4个字符

$img = imagecreatetruecolor($width,$height);

$color_bg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
//rand() ,设置一个随机数,我们这里设置的是RGB的取值范围,让图片呈现一个动态颜色变化

$color_border=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));

imagefill($img,0,0,$color_bg );
imagerectangle($img,0,0,$width-1,$height-1,$color_border);
//imagerectangle是设置矩形边框,前面两位数据是矩形左上角的坐标,后两位数据是右下角的坐标

for($i=0;$i,100;$i++){
		imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
}
//画一个点,前两个数据是点的位置,后面的数据是分配给点的颜色。用了for循环,执行了随机生成了100个点

for(i=0;i<3;i++){
	imageline($image,rand(0,$width/2),rand(0,$height/2,rand($width/2,$width),rand($height/2,$height),imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
}
//imageline,画一条线,前面四是个数据同上,是坐标信息。最后一个是颜色,for循环执行了3次

$color_string=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));

//imagestring($img,5,0,0,'abcdef',$color_string);
//imagestring( 作用对象,字的大小,x,y[字符串的左上角坐标],字符串,颜色) , 是用来创建字符

imagettftext($img,14,0,rand(5,15),rand(20,35),$color_string, 'font/SketchyComic.ttf',$string);
//imagettftext ,有很多字体
//格式为  ( 作用对象,字体的大小,字体倾斜的角度,x,y[字符串的左 下角坐标],字符串,颜色,字体路径,内容)

imagejpeg($img);
imagedestroy($img);

Guess you like

Origin blog.csdn.net/qq_41076531/article/details/100073874