thinkphp圖片上傳+validate表單驗證+圖片木馬檢測+縮略圖生成

    目錄

1.案例

  1.1圖片上傳

    1.2進行圖片木馬檢測

    1.3縮略圖生成

    1.4控制器中調用縮略圖生成方法

 

1.案例

  前言:在thinkphp框架的Thinkphp/Library/Think文件中有Upload.class.php(文件上傳類),我們上傳文件或者圖片都要用到這個類

 

  1.1圖片上傳

  HTML代碼

<form id="addForm" action="" method="post">
    <input name="file" multiple="multiple" type="file" id="file">
    <input type="submit" value="提交">
<form>

   

  JS代碼,進行validate表單驗證

 

<script>

    //進行提交表單驗證
    $("#addForm").validate({

        //驗證規則
        rules: {
            file: {
                required: true
            }
        },

        //錯誤信息放置位置
        errorPlacement: function (error, element) {
            error.appendTo(element.parent().parent());
        },

        //錯誤提示信息
        messages: {
            file: {
                required: "請上傳圖片"
            }
        }
    });

</script>

 

  

   PHP控制器處理,把表單提交的數據,根據自己的需求進行處理。

 

  1.2進行圖片木馬檢測

 

  圖片上傳完後,可以得到圖片的存放路徑,我們可以對該圖片進行木馬檢測。

  具體前參考我寫的木馬檢測方法http://www.cnblogs.com/jingmin/p/6308870.html

  如果我們檢測到木馬後,用 unlink($url) 把這張圖片從服務器刪除。

  我們也可以在圖片上傳前對圖片進行木馬檢測。

 

  1.3縮略圖生成

  function方法(我們要在function中寫入縮略圖的生成方法)

<?php
/**
 * 計算生成等比縮略圖的寬和高
 * @param $width
 * @param $height
 * @param $picpath
 * @return mixed
 */

//參數(寬,高,圖片路徑)
function show_pic_scal($width, $height, $src) { //獲取上傳圖片的寬高 $imginfo = getImageInfo($src); $imgw = $imginfo [0]; $imgh = $imginfo [1]; //將寬高比以千位分組格式保存在變量中 $ra = number_format(($imgw / $imgh), 1); //寬高比 $ra2 = number_format(($imgh / $imgw), 1); //高寬比 //如果圖片的寬大於縮略圖的寬 或者圖片的高大於縮略圖的高 if ($imgw > $width or $imgh > $height) { //如果縮略圖的寬大於縮略圖的高 if ($imgw > $imgh) { $newWidth = $width; $newHeight = round($newWidth / $ra); } elseif ($imgw < $imgh) { $newHeight = $height; $newWidth = round($newHeight / $ra2); } else { $newWidth = $width; $newHeight = round($newWidth / $ra); } } else { $newHeight = $imgh; $newWidth = $imgw; } $newsize [0] = $newWidth; $newsize [1] = $newHeight; //返回圖片等比例壓縮後的能生成的實際寬和高 return $newsize; } /** * 獲取圖片信息 * @param $src * @return array */ function getImageInfo($src) { //讀取文件大小信息 return getimagesize($src); } /** * 創建圖片,返回資源類型 * @param string $src 圖片路徑 * @return resource $im 返回資源類型 * **/ function create($src) { //讀取圖片信息 $info = getImageInfo($src); //$info[2]圖片類型,根據圖片類型選擇 switch ($info[2]) { case 1: //從gif文件新建一圖像 $im = imagecreatefromgif($src); break; case 2: //從jpeg文件新建一圖像 $im = imagecreatefromjpeg($src); break; case 3: //從png文件新建一圖像 $im = imagecreatefrompng($src); break; } return $im; } /** * 縮略圖主函數 * @param string $src 圖片路徑 * @param int $w 縮略圖寬度 * @param int $h 縮略圖高度 * @return mixed 返回縮略圖路徑 * **/ function resize($src, $w, $h) { //讀取圖片所有信息信息 $temp = pathinfo($src); $name = $temp["basename"];//文件名 $thumbname = date('YmdHis');//要生成的文件名 $dir = $temp["dirname"];//文件所在的文件夾 $extension = $temp["extension"];//文件擴展名 $savepath = "{$dir}/{$thumbname}" . ".$extension";//縮略圖保存路徑,生成新的的文件名(可以根據自己的實際需求改動) //獲取圖片的基本信息 $info = getImageInfo($src); $width = $info[0];//獲取圖片寬度 $height = $info[1];//獲取圖片高度 $per1 = round($width / $height, 2);//計算原圖長寬比 $per2 = round($w / $h, 2);//計算縮略圖長寬比 //計算縮放比例 if ($per1 > $per2 || $per1 == $per2) { //原圖長寬比大於或者等於縮略圖長寬比,則按照寬度優先 $per = $w / $width; } if ($per1 < $per2) { //原圖長寬比小於縮略圖長寬比,則按照高度優先 $per = $h / $height; } $temp_w = intval($width * $per);//計算原圖縮放後的寬度 $temp_h = intval($height * $per);//計算原圖縮放後的高度 $temp_img = imagecreatetruecolor($temp_w, $temp_h);//創建畫布 $im = create($src);//生成縮略圖 //重採樣拷貝部分圖像並調整大小(目標圖象連接資源,源圖象連接資源,目標 X座標點,目標 Y座標點,源的 X 座標點,源的 Y 座標點,目標寬度,目標高度,源圖象的寬度,源圖象的高度) imagecopyresampled($temp_img, $im, 0, 0, 0, 0, $temp_w, $temp_h, $width, $height); if ($per1 > $per2) { //輸出圖象到瀏覽器或文件,參數(由圖象創建函數返回的圖象資源,文件保存路徑,圖像質量,範圍從 0(最差質量,文件更小)到 100(最佳質量,文件最大)) imagejpeg($temp_img, $savepath, 100); //釋放與$im關聯的內存 imagedestroy($im); return addBg($savepath, $w, $h, "w"); //寬度優先,在縮放之後高度不足的情況下補上背景 } if ($per1 == $per2) { imagejpeg($temp_img, $savepath, 100); imagedestroy($im); return $savepath; //等比縮放 } if ($per1 < $per2) { imagejpeg($temp_img, $savepath, 100); imagedestroy($im); return addBg($savepath, $w, $h, "h"); //高度優先,在縮放之後寬度不足的情況下補上背景 } } /** * 添加背景 * @param string $src 圖片路徑 * @param int $w 背景圖像寬度 * @param int $h 背景圖像高度 * @param String $first 決定圖像最終位置的,w 寬度優先 h 高度優先 wh:等比 * @return 返回加上背景的圖片 * **/ function addBg($src, $w, $h, $fisrt = "w") { //新建一個真彩色圖像,參數(圖像寬度,圖像高度) $bg = imagecreatetruecolor($w, $h); //爲一幅圖像分配顏色,參數(圖片對象,分配顏色),如果填充失敗,返回-1; $white = imagecolorallocate($bg, 255, 255, 255); //填充背景(圖像對象,X座標,Y座標,分配的顏色) imagefill($bg, 0, 0, $white); //獲取目標圖片信息 $info = getImageInfo($src); $width = $info[0];//目標圖片寬度 $height = $info[1];//目標圖片高度 //創建圖片 $img = create($src); if ($fisrt == "wh") { //等比縮放 return $src; } else { if ($fisrt == "w") { $x = 0; $y = ($h - $height) / 2;//垂直居中 } if ($fisrt == "h") { $x = ($w - $width) / 2;//水平居中 $y = 0; } imagecopymerge($bg, $img, $x, $y, 0, 0, $width, $height, 100); imagejpeg($bg, $src, 100); imagedestroy($bg); imagedestroy($img); return $src; } }

 

   把縮略圖方法寫入function中,我幾乎把所有的代碼進行了註釋,如果你再看不懂,我也沒辦法(秒懂!!)

   如果在方法中你還有很多不懂的,大概就是對GD庫不是很瞭解了,可以點擊  http://php.net/manual/zh/ref.image.php去學習PHP GD庫

 

  1.4在控制器中調用縮略圖生成方法

            //實例化
            $banner = M('banner');

            //接收圖片路徑信息(就是上面表單提交圖片的路徑)
            $data['pic_url'] = $_POST['image'][0];
         
            //上傳圖片路徑
            $img_url = "." . $data['pic_url'];

            //建立新的縮略圖的寬和高,參數(寬,高,圖片對象)
            $show_pic_scal = show_pic_scal(50, 36, $img_url);

            //生成縮略圖,並返回圖片路徑信息
            $data['compress_name'] = resize($img_url, $show_pic_scal[0], $show_pic_scal[1]);

            //打印出縮略圖的路徑信息
            dump($data['compress_name']);exit;  

 

 

   簡而言之,這個方法就是找到上傳的圖片,然後建立縮略圖,把原圖和縮略圖路徑信息都存入數據庫中。

 

  有什麼疑問可以留言,或者評論,我會及時回覆大家,感覺好的點個贊,謝謝大家!

 

發佈了42 篇原創文章 · 獲贊 9 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章