php运用一(图像生成保存、图像验证码、水印、九九乘法表)

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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值