php 基本的图形处理函数

   觉得用php写程序来处理图片和做一个验证码比较有意思,所以就写了一个简单公共函数库,跟大家分享

<?php

    /*公共函数*/

    /*载入图片*/
    function load_picture($path)
    {
        $info = getimagesize($path);
         switch($info[2])
         {
             case 1:
                 $im = imagecreatefromgif($path);
                 return $im;
             case 2:
                 $im = imagecreatefromjpeg($path);
                 return $im;
             case 3:
                 $im = imagecreatefrompng($path);
                 return $im;
         }
    }

    /*改变图片的大小*/
    function change_picture_size($path,$size)
    {
        $im = load_picture($path);
        $info = getimagesize($path);
        $width = $info[0]*$size;
        $height = $info[1]*$size;
        $new = imagecreatetruecolor($width,$height);
        $res = imagecopyresized($new,$im,0,0,0,0,$width,$height,$info[0],$info[1]);
        if($res)
        {
          return $new;
        }
        else
          echo  "<br>Sorry ,failed!<br>";
    }

    /*增加字体水印*/
    function add_fonts($im,$fontsize,$liner,$x,$y,$content)
    {
        $te = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
        imagettftext($im,$fontsize,$liner,$x,$y,$te,"FZFangSong.ttf",$content);
    }

    /*增加图片水印*/
    function add_water_code($dim,$sim_path,$x,$y,$cut_x,$cut_y,$width,$height)
    {
        $sim = load_picture($sim_path);
        imagecopy($dim,$sim,$x,$y,$cut_x,$cut_y,$width,$height);
    }

    /*生成验证码*/
    function check_code()
    {
        //生成随机数
        for($i=0;$i<4;$i++)
        {
           $rand .=dechex(rand(0,15));
        }
        //创建图片
        $im = imagecreatetruecolor(100,30);
        //创建调色板
        $bg = imagecolorallocate($im,0,0,0);
        $te = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
        //写随机数
        //imagestring($im,4,rand(20,60),rand(8,16),$rand,$te);
        //写汉字
        //$content = iconv("gbk","UTF-8","生日快乐");(windows下需要使用)
        imagettftext($im,15,0,rand(20,50),rand(16,20),$te,"FZFangSong.ttf","生日快乐");
        //画点和画画线
        for($j=0;$j<5;$j++)
        {
           imageline($im,100,30,rand(0,100),rand(0,30),$te);
        }
        for($j=0;$j<100;$j++)
        {
           imagesetpixel($im,rand(0,100),rand(0,30),$te);
        }
        return $im;
    }

 

//测试

   $path = "picture/1.jpg";
   $pic = change_picture_size($path,"0.5");
   add_fonts($pic,48,0,50,50,"中国你好");
   add_water_code($pic,"picture/2.jpg",50,200,10,20,100,100);

   //$imge = check_code();
   header("Content-type:image/jpeg");
   imagejpeg($pic);

?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值