1.图像生成与保存
在PHP动态网页中,使用函数创造图片,分配颜色,使用颜色填充生成的图片,并且将图片保存。
<?php
// header('Content-type:image/jpeg');
//var_dump(imagecreatetruecolor(200,200));
$img=imagecreatetruecolor(1000,800);
$color1 =imagecolorallocate($img,50,50,50);
$color2 =imagecolorallocate($img,229,36,36);
$color3 =imagecolorallocate($img,46,219,50);
imagefill($img,0,0,$color2);
if(imagejpeg($img,'D:/phpstudy_pro/WWW\php学习/实战学习/11.jpeg')){
echo '保存成功';
}
imagedestroy($img);
?>
2. 图像验证码
在上一个图片生成的基础上,通过随机函数,让验证码在图片上动态生成。
<?php
header('Content-type:image/jpeg');
$width=112;
$height=40;
$element=array('a','b','c','d','e','h','r','w','2','5','6');
$string='';
for($i=0;$i<5;$i++){
$string.=$element[rand(0,count($element)-1)];
}
$img=imagecreatetruecolor($width,$height);
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorString=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
imagefill($img,0,0,$colorBg);
imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);
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($i=0;$i<5;$i++){ imageline($img,rand(0,$width/2),rand(0,$height),rand($width/2,$width),rand(0,$height),imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255)));
}
imagestring($img,10,rand(35,40),rand(10,20),$string,$colorString);//D:/phpstudy_pro/WWW/php学习/实战学习/Adorable.ttf
// $font='./Adorable.ttf';
// var_dump(imagettftext( $img, 16, 0, 0, 25, $colorString,'D:\phpstudy_pro\WWW/php学习\实战学习\Adorable.ttf', $string))
;//无法使用
imagejpeg($img);
imagedestroy($img);
?>
3.水印
和图像验证码原理相似,但是比他简单,不需要随机生成,只要将指定的水印放在图片上就行。
<?php
header('Content-type:image/jpeg;charset=utf-8');
$img=imagecreatefromjpeg('https://scpic.chinaz.net/files/pic/pic9/202009/apic27858.jpg');
// $img=imagecreatefromjpeg("/images/blackground.jpeg");
$color=imagecolorallocate($img,255,255,255);
imagestring($img,10,rand(35,40),rand(10,20),'yingyushu',$color);
// imagettftext($img,20,0,100,100,$color,'Adorable.TTF','网校');
imagejpeg($img);
imagedestroy($img);
?>
4.九九乘法表
简单的使用表格输出九九乘法表格。
<table border=1>
<?php for($i=1;$i<10;$i++){?>
<tr>
<?php for($j=1;$j<$i;$j++){?>
<td>
<?php echo $j . ' * '. $i.' = '.$i*$j;?>
</td>
<?php }?>
</tr>
<?php }?>
</table>
<table border=1>
<?php for($i=1;$i<10;$i++):?>
<tr>
<?php for($j=1;$j<$i;$j++):?>
<td>
<?php echo $j . ' * '. $i.' = '.$i*$j;?>
</td>
<?php endfor;?>
</tr>
<?php endfor;?>
</table>