PHP圖片類(驗證碼,水印,縮略)

jopen 12年前發布 | 18K 次閱讀 PHP PHP開發

class image{
        static $errno=null;
        static $error=array(
            '1'=>'原圖或者水印不存在',
            '2'=>'水印圖片寬高不能大于原圖',
            '3'=>'原圖無法創建畫布',
            '4'=>'水印圖無法創建畫布',
            '5'=>'加水印失敗',
            '6'=>'原圖加水印的效果圖無法創建畫布',
            '7'=>'保存水印效果圖失敗',
            '8'=>'要縮略的圖片不存在',
            '9'=>'縮略的圖片寬高不能小于效果圖的寬高',
            '10'=>'縮略圖的畫布無法創建',
            '11'=>'縮略失敗',
            '12'=>'生成縮略效果圖片文件失敗'
        );
        / 
        水印 
        所用主要函數imagestring
        int $strnum 驗證碼中字符串的個數
        int $width 驗證碼區域的寬度
        int $height 驗證碼區域的高度
        /
        static function verify($strnum,$width,$height){
            $im=imagecreatetruecolor($width,$height);
            $bgcolor=imagecolorallocate($im,200,200,200);
            imagefill($im,0,0,$bgcolor);
            $str='abcdefghijkmnpqrstuvwxyz23456789';
            $str=substr(str_shuffle($str),0,$strnum);//獲取隨機字符串
            for($i=0,$j=$width/$strnum;$i<$strnum;$i++){
                $strcolor=imagecolorallocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
                imagestring($im,5,$j$i+5,mt_rand(1,$height/4),$str[$i],$strcolor);
            }
            header('Content-type:image/png');
            imagepng($im);
            imdestroy($im);
        }
        /
            水印
            函數:imagecopymerge
            string dst 要加水印的圖片(目標圖)
            string src 用來當做水印的圖片(水印圖)
            string savename 加水印后的效果圖
            int $postion 水印位置 0左上角 1右上角 2右下角 3左下角
            int $pct 透明度

    */

    static function water($dst,$src,$savename='',$postion=2,$pct=50){
        if(!file_exists($dst)||!file_exists($src)){
            self::$errno=1;
            return false;
        }
        $dstinfo=self::getImgInfo($dst);
        $srcinfo=self::getImgInfo($src);
        if(($dstinfo['width']<$srcinfo['width'])||($dstinfo['height']<$srcinfo['height'])){
            self::$errno=2;
            return false;
        }
        $dstcreatefunc='imagecreatefrom'.(($dstinfo['type']=='jpg')?'jpeg':$dstinfo['type']);
        if(!function_exists($dstcreatefunc)){
            self::$errno=3;
            return false;
        }
        $srccreatefunc='imagecreatefrom'.(($srcinfo['type']=='jpg')?'jpeg':$srcinfo['type']);
        if(!function_exists($srccreatefunc)){
            self::$errno=4;
            return false;
        }
        $dstimg=$dstcreatefunc($dst);//創建目標圖畫布資源
        $srcimg=$srccreatefunc($src);//創建水印圖畫布資源


        switch($postion){//判斷位置,確定放到目標圖的位置坐標
            case 0:
                $dst_x=0;
                $dst_y=0;
                break;
            case 1:
                $dst_x=$dstinfo['width']-$srcinfo['width'];
                $dst_y=0;
                break;
            case 3:
                $dst_x=0;
                $dst_y=$dstinfo['height']-$srcinfo['height'];
                break;
            default:
                $dst_x=$dstinfo['width']-$srcinfo['width'];
                $dst_y=$dstinfo['height']-$srcinfo['height'];
        }

        if(!imagecopymerge($dstimg,$srcimg,$dst_x,$dst_y,0,0,$srcinfo['width'],$srcinfo['height'],$pct)){
            self::$errno=5;
            return false;
        }
        if(!$savename){
           $savename=$dst;
           unlink($dst);
        }
        $dstimgfun='image'.(($dstinfo['type']=='jpg')?'jpeg':$dstinfo['type']);
        if(!function_exists($dstimgfun)){
            self::$errno=6;
            return false;
        }
        if(!$dstimgfun($dstimg,$savename)){
            self::$errno=7;
            return false;
        }
        return true;
    }


    //獲取圖片的長,寬,類別
    static function getImgInfo($image){
        $imginfo=array();
        $img=getimagesize($image);
        $type=explode('/',$img['mime']);
        $type=end($type);
        $imginfo['width']=$img[0];
        $imginfo['height']=$img[1];
        $imginfo['type']=$type;
        return $imginfo;
    }

    /*
    string src 要縮略的圖(原圖)
    int width 縮略后的小圖寬度
    int height 縮略后的小圖高度
    string savename 縮略效果圖的保存路徑
    !!兩邊留白
    所用函數imagecopyresampled
    */

    static function thumb($src,$savename='',$width=200,$height=200){
        if(!file_exists($src)){
          self::$errno=8;
          return false;
        }
        $srcinfo=self::getImgInfo($src);
        if(($srcinfo['width']<$width)&&($srcinfo['height']<$height)){//判斷原圖和縮略圖的寬高大小
          self::$errno=9;
          return false;
        }

        $dst=imagecreatetruecolor($width,$height);
        $white=imagecolorallocate($dst,255,255,255);
        imagefill($dst,0,0,$white);//創建縮略圖畫布,以白色填充

        $srccreatefunc='imagecreatefrom'.(($srcinfo['type']=='jpg')?'jpeg':$srcinfo['type']);
        if(!function_exists($srccreatefunc)){
            self::$errno=3;
            return false;
        }
        $srcimg=$srccreatefunc($src);//創建原圖畫布

        $p=min($width/$srcinfo['width'],$height/$srcinfo['height']);//確定縮放比例
        $dst_w=$p*$srcinfo['width'];//確定原圖縮略后的寬度    
        $dst_h=$p*$srcinfo['height'];//確定原圖縮略后的高度
        $dst_x=($width-$dst_w)/2;//確定原圖縮略后的小圖放在縮略圖位置的x坐標
        $dst_y=($height-$dst_h)/2;//確定原圖縮略后的小圖放在縮略圖位置y坐標
        if(!imagecopyresampled($dst,$srcimg,$dst_x,$dst_y,0,0,$dst_w,$dst_h,$srcinfo['width'],$srcinfo['height'])){
            self::$errno=11;
            return false;
        }

        $imgfunc='image'.(($srcinfo['type']=='jpg')?'jpeg':$srcinfo['type']);
        if(!function_exists($imgfunc)){
            self::$errno=10;
            return false;
        }

        if(!$savename){
            $savename=$src;
            unlink($src);
        }
        if(!$imgfunc($dst,$savename)){
            self::$errno=12;
            return false;
        }
        return true;
    }

}</pre><br />
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!