亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用php怎么實現一個可生成縮略圖的文件上傳功能

發布時間:2020-12-10 14:45:34 來源:億速云 閱讀:165 作者:Leah 欄目:開發技術

本篇文章給大家分享的是有關利用php怎么實現一個可生成縮略圖的文件上傳功能,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

類文件調用方法如下:

代碼如下:
<&#63;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);        
}     
&#63;>
<html>        
    <head><title>test</title></head>        
    <body >        
    <form name="upload" method="post" action="&#63;action=save" enctype="multipart/form-data" >        
        <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 &#63; $time : time();        
    }        
       
    //設置并創建文件具體存放的目錄        
    //$basedir  : 基目錄,必須為物理路徑        
    //$filedir  : 自定義子目錄,可用參數{y}、{m}、aegqsqibtmh        
    function set_dir($basedir,$filedir = '') {        
        $dir = $basedir;        
        !is_dir($dir) && @mkdir($dir,0777);        
        if (!emptyempty($filedir)) {        
            $filedir = str_replace(array('{y}','{m}','aegqsqibtmh'),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怎么實現一個可生成縮略圖的文件上傳功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

甘谷县| 马鞍山市| 科尔| 仙游县| 锦屏县| 亳州市| 鄂州市| 商丘市| 塔城市| 鹤山市| 开原市| 深泽县| 永善县| 武宁县| 商河县| 金乡县| 兴业县| 五峰| 阿拉尔市| 桐梓县| 嘉定区| 当阳市| 阿拉善盟| 界首市| 高青县| 扎兰屯市| 昌宁县| 蕉岭县| 江城| 琼结县| 东方市| 海丰县| 定南县| 容城县| 永安市| 临安市| 青神县| 多伦县| 法库县| 寿宁县| 塔城市|