程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php可生成縮略圖的文件上傳類實例

php可生成縮略圖的文件上傳類實例

編輯:PHP綜合

本文實例講述了php可生成縮略圖的文件上傳類及其用法。分享給大家供大家參考。具體實現方法如下:

類文件調用方法如下:

復制代碼 代碼如下:<?php
if ($_GET['action'] == 'save') {        
       
    $up = new upload();        
    $up->set_dir(dirname(__FILE__).'/upload/','{y}/{m}');        
    $up->set_thumb(100,80);        
    $up->set_watermark(dirname(__FILE__).'/jblog/images/watermark.png',6,90);        
    $fs = $up->execute();        
       
    var_dump($fs);        
}     
?>
<html>        
    <head><title>test</title></head>        
    <body style="margin:0;padding:0">        
    <form name="upload" method="post" action="?action=save" enctype="multipart/form-data" style="margin:0">        
        <input type="file" name="attach[]" />        
        <input type="file" name="attach[]" />        
        <input type="submit" name="submit" value="上 傳" />        
    </form>        
    </body>        
</html>
upload上傳類文件如下:

復制代碼 代碼如下:class upload {
    var $dir;            //附件存放物理目錄        
    var $time;           //自定義文件上傳時間        
    var $allow_types;    //允許上傳附件類型        
    var $field;          //上傳控件名稱        
    var $maxsize;        //最大允許文件大小,單位為KB        
       
    var $thumb_width;    //縮略圖寬度        
    var $thumb_height;   //縮略圖高度        
       
    var $watermark_file; //水印圖片地址        
    var $watermark_pos;  //水印位置        
    var $watermark_trans;//水印透明度         
        
    //構造函數        
    //$types : 允許上傳的文件類型 , $maxsize : 允許大小 ,  $field : 上傳控件名稱 , $time : 自定義上傳時間        
    function upload($types = 'jpg|png', $maxsize = 1024, $field = 'attach', $time = '') {        
        $this->allow_types = explode('|',$types);        
        $this->maxsize = $maxsize * 1024;        
        $this->field = $field;        
        $this->time = $time ? $time : time();        
    }        
       
    //設置並創建文件具體存放的目錄        
    //$basedir  : 基目錄,必須為物理路徑        
    //$filedir  : 自定義子目錄,可用參數{y}、{m}、{d}        
    function set_dir($basedir,$filedir = '') {        
        $dir = $basedir;        
        !is_dir($dir) && @mkdir($dir,0777);        
        if (!emptyempty($filedir)) {        
            $filedir = str_replace(array('{y}','{m}','{d}'),array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir));        
            $dirs = explode('/',$filedir);        
            foreach ($dirs as $d) {        
                !emptyempty($d) && $dir .= $d.'/';        
                !is_dir($dir) && @mkdir($dir,0777);        
            }        
        }        
        $this->dir = $dir;        
    }        
       
    //圖片縮略圖設置,如果不生成縮略圖則不用設置        
    //$width : 縮略圖寬度 , $height : 縮略圖高度        
    function set_thumb ($width = 0, $height = 0) {        
        $this->thumb_width  = $width;        
        $this->thumb_height = $height;        
    }        
       
    //圖片水印設置,如果不生成添加水印則不用設置        
    //$file : 水印圖片 , $pos : 水印位置 , $trans : 水印透明度        
    function set_watermark ($file, $pos = 6, $trans = 80) {        
        $this->watermark_file  = $file;        
        $this->watermark_pos   = $pos;        
        $this->watermark_trans = $trans;        
    }        
       
    /*----------------------------------------------------------------      
    執行文件上傳,處理完返回一個包含上傳成功或失敗的文件信息數組,      
    其中:name 為文件名,上傳成功時是上傳到服務器上的文件名,上傳失敗則是本地的文件名      
          dir  為服務器上存放該附件的物理路徑,上傳失敗不存在該值      
          size 為附件大小,上傳失敗不存在該值      
          flag 為狀態標識,1表示成功,-1表示文件類型不允許,-2表示文件大小超出      
    -----------------------------------------------------------------*/       
    function execute() {        
        $files = array(); //成功上傳的文件信息        
        $field = $this->field;        
        $keys = array_keys($_FILES[$field]['name']);        
        foreach ($keys as $key) {        
            if (!$_FILES[$field]['name'][$key]) continue;        
                    
            $fileext = $this->fileext($_FILES[$field]['name'][$key]); //獲取文件擴展名        
            $filename = date('Ymdhis',$this->time).mt_rand(10,99).'.'.$fileext; //生成文件名        
            $filedir = $this->dir;  //附件實際存放目錄        
            $filesize = $_FILES[$field]['size'][$key]; //文件大小
                    
            //文件類型不允許        
            if (!in_array($fileext,$this->allow_types)) {        
                $files[$key]['name'] = $_FILES[$field]['name'][$key];        
                $files[$key]['flag'] = -1;        
                continue;        
            }        
       
            //文件大小超出        
            if ($filesize > $this->maxsize) {        
                $files[$key]['name'] = $_FILES[$field]['name'][$key];        
                $files[$key]['name'] = $filesize;        
                $files[$key]['flag'] = -2;        
                continue;        
            }        
       
            $files[$key]['name'] = $filename;        
            $files[$key]['dir'] = $filedir;        
            $files[$key]['size'] = $filesize;        
       
            //保存上傳文件並刪除臨時文件        
            if (is_uploaded_file($_FILES[$field]['tmp_name'][$key])) {        
                move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename);        
                @unlink($_FILES[$field]['tmp_name'][$key]);        
                $files[$key]['flag'] = 1;        
       
                //對圖片進行加水印和生成縮略圖        
                if (in_array($fileext,array('jpg','png'))) {        
                    if ($this->thumb_width) {        
                        if ($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)) {        
                            $files[$key]['thumb'] = 'thumb_'.$filename;  //縮略圖文件名        
                        }        
                    }        
                    $this->create_watermark($filedir.$filename);        
                }        
            }        
        }        
       
        return $files;        
    }        
       
    //創建縮略圖,以相同的擴展名生成縮略圖        
    //$src_file : 來源圖像路徑 , $thumb_file : 縮略圖路徑        
    function create_thumb ($src_file,$thumb_file) {        
        $t_width  = $this->thumb_width;        
        $t_height = $this->thumb_height;        
       
        if (!file_exists($src_file)) return false;        
       
        $src_info = getImageSize($src_file);        
       
        //如果來源圖像小於或等於縮略圖則拷貝源圖像作為縮略圖        
        if ($src_info[0] <= $t_width && $src_info[1] <= $t_height) {        
            if (!copy($src_file,$thumb_file)) {        
                return false;        
            }        
            return true;        
        }        
       
        //按比例計算縮略圖大小        
        if ($src_info[0] - $t_width > $src_info[1] - $t_height) {        
            $t_height = ($t_width / $src_info[0]) * $src_info[1];        
        } else {        
            $t_width = ($t_height / $src_info[1]) * $src_info[0];        
        }        
       
        //取得文件擴展名        
        $fileext = $this->fileext($src_file);        
       
        switch ($fileext) {        
            case 'jpg' :        
                $src_img = ImageCreateFromJPEG($src_file); break;        
            case 'png' :        
                $src_img = ImageCreateFromPNG($src_file); break;        
            case 'gif' :        
                $src_img = ImageCreateFromGIF($src_file); break;        
        }        
       
        //創建一個真彩色的縮略圖像        
        $thumb_img = @ImageCreateTrueColor($t_width,$t_height);        
       
        //ImageCopyResampled函數拷貝的圖像平滑度較好,優先考慮        
        if (function_exists('imagecopyresampled')) {        
            @ImageCopyResampled($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]);        
        } else {        
            @ImageCopyResized($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]);        
        }        
       
        //生成縮略圖        
        switch ($fileext) {        
            case 'jpg' :        
                ImageJPEG($thumb_img,$thumb_file); break;        
            case 'gif' :        
                ImageGIF($thumb_img,$thumb_file); break;        
            case 'png' :        
                ImagePNG($thumb_img,$thumb_file); break;        
        }        
       
        //銷毀臨時圖像        
        @ImageDestroy($src_img);        
        @ImageDestroy($thumb_img);        
       
        return true;        
       
    }        
       
    //為圖片添加水印        
    //$file : 要添加水印的文件        
    function create_watermark ($file) {        
       
        //文件不存在則返回        
        if (!file_exists($this->watermark_file) || !file_exists($file)) return;        
        if (!function_exists('getImageSize')) return;        
                
        //檢查GD支持的文件類型        
        $gd_allow_types = array();        
        if (function_exists('ImageCreateFromGIF')) $gd_allow_types['image/gif'] = 'ImageCreateFromGIF';        
        if (function_exists('ImageCreateFromPNG')) $gd_allow_types['image/png'] = 'ImageCreateFromPNG';        
        if (function_exists('ImageCreateFromJPEG')) $gd_allow_types['image/jpeg'] = 'ImageCreateFromJPEG';        
       
        //獲取文件信息        
        $fileinfo = getImageSize($file);        
        $wminfo   = getImageSize($this->watermark_file);        
       
        if ($fileinfo[0] < $wminfo[0] || $fileinfo[1] < $wminfo[1]) return;        
       
        if (array_key_exists($fileinfo['mime'],$gd_allow_types)) {        
            if (array_key_exists($wminfo['mime'],$gd_allow_types)) {        
                        
                //從文件創建圖像        
                $temp = $gd_allow_types[$fileinfo['mime']]($file);        
                $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file);        
       
                //水印位置        
                switch ($this->watermark_pos) {                     
                    case 1 :  //頂部居左        
                        $dst_x = 0; $dst_y = 0; break;                      
                    case 2 :  //頂部居中        
                        $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = 0; break;                        
                    case 3 :  //頂部居右        
                        $dst_x = $fileinfo[0]; $dst_y = 0; break;                       
                    case 4 :  //底部居左        
                        $dst_x = 0; $dst_y = $fileinfo[1]; break;                       
                    case 5 :  //底部居中        
                        $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break;             
                    case 6 :  //底部居右        
                        $dst_x = $fileinfo[0]-$wminfo[0]; $dst_y = $fileinfo[1]-$wminfo[1]; break;        
                    default : //隨機        
                        $dst_x = mt_rand(0,$fileinfo[0]-$wminfo[0]); $dst_y = mt_rand(0,$fileinfo[1]-$wminfo[1]);        
                }        
       
                if (function_exists('ImageAlphaBlending')) ImageAlphaBlending($temp_wm,True); //設定圖像的混色模式        
                if (function_exists('ImageSaveAlpha')) ImageSaveAlpha($temp_wm,True); //保存完整的 alpha 通道信息        
       
                //為圖像添加水印        
                if (function_exists('imageCopyMerge')) {        
                    ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this->watermark_trans);        
                } else {        
                    ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]);        
                }        
       
                //保存圖片        
                switch ($fileinfo['mime']) {        
                    case 'image/jpeg' :        
                        @imageJPEG($temp,$file);        
                        break;        
                    case 'image/png' :        
                        @imagePNG($temp,$file);        
                        break;        
                    case 'image/gif' :         
                        @imageGIF($temp,$file);        
                        break;        
                }        
                //銷毀零時圖像        
                @imageDestroy($temp);        
                @imageDestroy($temp_wm);        
            }        
        }        
    }        
       
    //獲取文件擴展名        
    function fileext($filename) {        
        return strtolower(substr(strrchr($filename,'.'),1,10));        
    }        
}

希望本文所述對大家的PHP程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved