PHP处理图片的类

<?php
    class Upload_image{
       
        var $tmp_file;        // 上传图片的临时路径
        var $old_file_name; //图片原来的名字
        var $new_file_name    = 'image'; //处理后图片保存的名字
        var $old_file_type;    //图片原来的类型
        var $file_size;        //图片原来的大小
        var $alow_file_type    = array('jpeg','gif','png','jpg')    ;//允许上传的图片类型
        var $max_size        = "500*1024"; //允许上传图片的大小
        var $folder_name    = array(
                                '1'
                            );
        ##构造函数,在类初始化的时候检查,需检查的参数,文件的类型,文件的大小

        function __construct($tmp_file,$old_file_name,$old_file_size){
            $this->tmp_file            = $tmp_file;
            $this->file_size        = $file_size;
            $this->old_file_name    = $old_file_name;   
            $this->check_file_type();
            $this->check_file_size();
        }

        ##比较文件类型
        function check_file_type(){
            $ffixes_name    = strtolower($this->get_suffixes_name());
            $alow_file_type    = $this->alow_file_type;
            if(!in_array($ffixes_name,$alow_file_type)){
                $this->msg("文件不符合jpeg/gif/png/jpg 格式!");
            }
        }
       
        ##比较文件大小
        function check_file_size(){
             if( $this->file_size > $this->maxSize ){
                $this->msg("目标文件不能大于". $this->maxSize/1024 ." KB");
            }
        }

        ##获取文件后缀名
        function get_suffixes_name(){
            $suffixes_name    = explode('.',$this->old_file_name);
            $ffixes_name    = $suffixes_name[count($suffixes_name)-1];
            return $ffixes_name;
        }

        ##创建文件夹
        function create_folder(){
            foreach($this->folder_name as $val){
                    $folder_name .= $val.'/';
                    if(!is_dir($folder_name)){
                        $oldumask=umask(0);
                        mkdir($folder_name,0777);
                        umask($oldumask);
                    }
                }
            return $folder_name;
        }
       
        # 检测上传结果是否成功
        function chk_savefile(){
           $file_name    = $this->new_file_name;
           $filename    = $this->create_folder().$file_name.'.'.$this->get_suffixes_name();
           $copymsg = copy($this->tmp_file,"./".$filename);
           if( $copymsg ){
                return $filename;
           }
           else{
                $this->msg("文件上传失败! /n/n请重新上传! ");
           }
        }
       
        # 保存文件
        function savefile(){
           $this->create_folder();
           return $this->chk_savefile();
        }

        # 提示错误信息并终止操作
        function msg($Error){
           echo "<script language=/"javascript/">/n";
           echo " alert('".$Error."');/n";
           echo " window.history.back();/n";
           echo "</script>/n";
           die();
        }

        /*
       函数:生成缩略图
        MakeBuild("images/a.jpg","news/b.jpg","100");
       参数:
       echo $BuildFile;   原图 带路径
       echo $newFile;    生成的缩略图 带路径
       echo $File_width;   缩略图宽度值
       echo $File_height;   缩略图高度值 (默认为宽度的比例值)
       echo $rate;     缩略图象品质;
    */
        function MakeBuild($File_width=100,$File_height=100,$rate=1000) {
           $BuildFile    = $this->tmp_file;
           $file_name    = $this->new_file_name;
           $this->create_folder();
           $newFile    = $this->create_folder().$file_name.'.'.$this->get_suffixes_name();
           if(!is_file($BuildFile)){
                $this->msg("文件 ".$BuildFile." 不是一个有效的图形文件!/n/n系统无法生成该文件的缩略图!");
                return false;
           }
           $data = GetImageSize($BuildFile);
           switch($data[2]){
            case 1:
                $im = @ImageCreateFromGIF($BuildFile);
                break;
            case 2:
                $im = @ImageCreateFromJPEG($BuildFile);
                break;
            case 3:
                $im = @ImageCreateFromPNG($BuildFile);
                break;
           }
           if(!$im){
                return false;
           } else {
                $srcW = ImageSX($im);  # 取得原图宽度;
                $srcH = ImageSY($im); # 取得原图高度;
                $dstX = 0;
                $dstY = 0;
                if($File_height==0){
                    $File_height = $File_width/$srcW*$srcH;
                }
               if($srcW>$File_width || $srcH>$File_height){;
                    $fFile_height=$File_height;
                    $fFile_width=$File_width;
                    if ($srcW*$File_height>$srcH*$File_width){
                        $fFile_height = round($srcH*$File_width/$srcW);
                        $dstY = floor(($File_height-$fFile_height)/2);
                        $fFile_width = $File_width;
                    } else {
                        $fFile_width = round($srcW*$File_height/$srcH);
                        $dstX = floor(($File_width-$fFile_width)/2);
                        $fFile_height = $File_height;
                    }
               }else{
                    $fFile_height    = $srcH;
                    $fFile_width    = $srcW;
                    $dstX = floor(($File_width-$fFile_width)/2);
                    $dstY = floor(($File_height-$fFile_height)/2);

               }
                $ni = ImageCreateTrueColor($File_width,$File_height);
                $kek=imagecolorallocate($ni,255,255,255);
                imagefill($ni,0,0,$kek);
                $dstX = ($dstX<0)?0:$dstX;
                $dstY = ($dstX<0)?0:$dstY;
                $dstX = ($dstX>($File_width/2))?floor($File_width/2):$dstX;
                $dstY = ($dstY>($File_height/2))?floor($File_height/s):$dstY;
                imagecopyresampled($ni,$im,$dstX,$dstY,0,0,$fFile_width,$fFile_height,$srcW,$srcH);
                ImageJpeg($ni,$newFile,$rate); # 生成缩略图;
                imagedestroy($ni);
                imagedestroy($im);     # imagedestroy(resource) 释放image关联的内存
           }

           return $newFile;
        }
    }
   
    /*include('upload_image.php');
    //print_R($_FILES);
    $tmp_name    = $_FILES['img']['tmp_name'];
    $name        = $_FILES['img']['name'];
    $size        = $_FILES['img']['size'];
    $upload_image    = new Upload_image($tmp_name,$name,$size);
    $upload_image->new_file_name= rand(0,time());
    $upload_image->folder_name    = array(date('Y'),date('m'),date('d'));
    //$upload_image->savefile();//只上传不处理
    echo $upload_image->MakeBuild(100,525); 上传生成指定的大小 */
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值