PHP-画像処理

PHPはテキストデータの処理に限定されず、さまざまな形式で動的な画像を作成することもできます

*画像はGDライブラリを介して処理することもできます。GDライブラリは最初にメモリで処理され、ファイルストリームの形式でブラウザに出力されるか、処理後にサーバーのディスクに保存されます。

1.画像を作成します
imagecreatetruecolor($ width、$ height); //トゥルーカラー画像を作成します

2.画像​​を描画します
imagecolorallocate(resource image、R、G、B) //色を割り当てます
imgefill(resource img、x、y、$ color); //領域の塗りつぶし、xとyは座標で、左上隅は画像は(0、0)

前記出力画像
imagejpeg();ブラウザに出力//
ヘッダ(); //画像を返すようにブラウザに教える、:ヘッダ関数の前に、何のコンテンツを出力することはできません

4.リソースを
解放しますimagedestroy(); //イメージを破棄します

<?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);

画像処理-検証コードの作成

<?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);

おすすめ

転載: blog.csdn.net/qq_41076531/article/details/100073874