php SWFUpload 怎麼建立縮略圖而且保存到指定文件夾裏面
upload.php php
- <?php
- /*
- * swfupload圖片上傳
- */
- if (isset($_POST["PHPSESSID"])) {
- session_id($_POST["PHPSESSID"]);
- }
- session_start();
- ini_set("html_errors", "0");
- if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) ||$_FILES["Filedata"]["error"] != 0) {
- echo "錯誤:無效的上傳!";
- exit(0);
- }
-
- // Get the image and create a thumbnail
- $file_types=explode(".",$_FILES["Filedata"]["name"]);
- $file_type=$file_types[count($file_types)-1];
- if(strtolower($file_type)=='gif' )
- {
- $img = imagecreatefromgif($_FILES["Filedata"]["tmp_name"]);
- }
- else if(strtolower($file_type)=='png')
- {
- $img = imagecreatefrompng($_FILES["Filedata"]["tmp_name"]);
- }
- else if(strtolower($file_type)=='bmp')
- {
- $img = imagecreatefromwbmp($_FILES["Filedata"]["tmp_name"]);
- }
- else
- {
- $img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
- }
-
- if (!$img) {
- echo "錯誤:沒法建立圖像 ". $_FILES["Filedata"]["tmp_name"];
- exit(0);
- }
-
- $width = imageSX($img);
- $height = imageSY($img);
-
- if (!$width || !$height) {
- echo "錯誤:無效的高或高";
- exit(0);
- }
-
- // Build the thumbnail
- $target_width = 100;
- $target_height = 100;
- $target_ratio = $target_width / $target_height;
-
- $img_ratio = $width / $height;
-
- if ($target_ratio > $img_ratio) {
- $new_height = $target_height;
- $new_width = $img_ratio * $target_height;
- } else {
- $new_height = $target_width / $img_ratio;
- $new_width = $target_width;
- }
-
- if ($new_height > $target_height) {
- $new_height = $target_height;
- }
- if ($new_width > $target_width) {
- $new_height = $target_width;
- }
-
- $new_img = ImageCreateTrueColor(100, 100);
- if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, 0)) { // Fill the image black
- echo "錯誤:不能填充新圖片";
- exit(0);
- }
-
- if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0,0, $new_width, $new_height, $width, $height)) {
- echo "錯誤:不能調整大小的圖像";
- exit(0);
- }
-
- if (!isset($_SESSION["file_info"])) {
- $_SESSION["file_info"] = array();
- }
- ob_start();
- imagejpeg($new_img);
- $imagevariable = ob_get_contents();
- ob_end_clean();
-
- $file_id = md5($_FILES["Filedata"]["tmp_name"] + rand()*100000);
-
- $_SESSION["file_info"][$file_id] = $imagevariable;
- echo "FILEID:" . $file_id; // Return the file id to the script
- include("upimg.class.php");
- if(!empty($_FILES["Filedata"]) and count(explode(",",$_SESSION["upload_tem"]))<5)
- {
- $folder="upload/images/tem/".date("Y-m-d");
- $up = new upimg("$folder","$folder"); //能夠寫成:$up = new upimg();
- $up->autoThumb = TRUE; //可省略
- $up->srcDel=TRUE;
- $up->thumbWidth = 550; //可省略
- $up->thumbHeight = 400; //可省略
- $up->maxsize=2014; //上傳文件大小單位是kb
- $result= $up->upload('Filedata'); // HTML中<input />的name屬性值
- $_SESSION["upload_tem"]=$_SESSION["upload_tem"].",".$up->thumbPath;
- $_SESSION["upload_tem"]=trim($_SESSION["upload_tem"],",");
-
- }
-
- ?>
生成縮略圖類upimg.class.php:
- <?php
- class upimg{
- public $uploadFolder = 'upload'; // 圖片存放目錄
- public $thumbFolder = 'upload/thumb'; // 縮略圖存放目錄
- public $thumbWidth = ''; // 縮略圖寬度
- public $thumbHeight = ''; // 縮略圖高度
- public $autoThumb = ''; // 是否自動生成縮略圖
- public $error = ''; // 錯誤信息
- public $imgPath = ''; // 上傳成功後的圖片位置
- public $thumbPath = ''; // 上傳成功後的縮略圖位置
- public $maxsize='';
-
- // 說明:初始化,建立存放目錄
- function __construct($uploadFolder = 'upload', $thumbFolder = 'upload/thumb'){
- $this->uploadFolder = $uploadFolder;
- $this->thumbFolder = $thumbFolder;
- $this->_mkdir();
- }
-
- // 說明:上傳圖片,參數是<input />的name屬性值;成功返回圖片的相對URL,失敗返回FALSE和錯誤信息(在$this->error裏)
- // bool/sting upload(string $html_tags_input_attrib_name);
- function upload($inputName){ // 上傳操做,參數是input標籤的name屬性。
- if ($this->error){ // 若是有錯,直接返回(例如_mkdir)
- return FALSE;
- }
- if(!$_FILES[$inputName]["name"]){
- $this->error = '沒有上傳圖片';
- return FALSE;
- }
- //檢測文件大小
- if($_FILES[$inputName]["size"] > ($this->maxsize*1024)){
- $this->error = '上傳文件'.$inputName.'太大,最大支持'.ceil($this->maxsize/1024).'kb的文件';
- return FALSE;
- }
- if($_FILES[$inputName]["name"]){
- $isUpFile = $_FILES[$inputName]['tmp_name'];
- if (is_uploaded_file($isUpFile)){
- $imgInfo = $this->_getinfo($isUpFile);
- if (FALSE == $imgInfo){
- return FALSE;
- }
- $extName = $imgInfo['type'];
- $microSenond = floor(microtime()*10000);// 取一個毫秒級數字,4位。
- $newFileName = $uploadFolder . '/' . date('YmdHis') . $microSenond . '.' . $extName ; // 所上傳圖片的新名字。
- $location = $this->uploadFolder . $newFileName;
- $result = move_uploaded_file($isUpFile, $location);
- if ($result)
- {
- if (TRUE == $this->autoThumb)
- { // 是否生成縮略圖
- $thumb = $this->thumb($location, $this->thumbWidth, $this->thumbHeight);
- if (FALSE == $thumb)
- {
- return FALSE;
- }
- }
- //是否刪除原圖
- if(TRUE==$this->srcDel)
- {
- @unlink ($location);
- }
- $this->imgPath = $location;
- return $location;
- }else{
- $this->error = '移動臨時文件時出錯';
- return FALSE;
- }
- }else{
- $uploadError = $_FILES[$inputName]['error'];
- if (1 == $uploadError){ // 文件大小超過了php.ini中的upload_max_filesize
- $this->error = '文件太大,服務器拒絕接收大於' . ini_get('upload_max_filesize') . '的文件';
- return FALSE;
- }elseif (3 == $uploadError){ // 上傳了部分文件
- $this->error = '上傳中斷,請重試';
- return FALSE;
- }elseif (4 == $uploadError){
- $this->error = '沒有文件被上傳';
- return FALSE;
- }elseif (6 == $uploadError){
- $this->error = '找不到臨時文件夾,請聯繫您的服務器管理員';
- return FALSE;
- }elseif (7 == $uploadError){
- $this->error = '文件寫入失敗,請聯繫您的服務器管理員';
- return FALSE;
- }else{
- if (0 != $uploadError){
- $this->error = '未知上傳錯誤,請聯繫您的服務器管理員';
- return FALSE;
- }
- } // end if $uploadError
- } // end if is_uploaded_file else
- } // end if $_FILES[$inputName]["name"]
- }
-
- // 說明:獲取圖片信息,參數是上傳後的臨時文件,成功返回數組,失敗返回FALSE和錯誤信息
- // array/bool _getinfo(string $upload_tmp_file)
- private function _getinfo($img){
- if (!file_exists($img)){
- $this->error = '找不到圖片,沒法獲取其信息';
- return FALSE;
- }
- $tempFile = @fopen($img, "rb");
- $bin = @fread($tempFile, 2); //只讀2字節
- @fclose($tempFile);
- $strInfo = @unpack("C2chars", $bin);
- $typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
- $fileType = '';
- switch ($typeCode){ // 6677:bmp 255216:jpg 7173:gif 13780:png 7790:exe 8297:rar 8075:zip tar:109121 7z:55122 gz 31139
- case '255216':
- $fileType = 'jpg';
- break;
- case '6677':
- $fileType = 'bmp';
- break;
- case '7173':
- $fileType = 'gif';
- break;
- case '13780':
- $fileType = 'png';
- break;
- default:
- $fileType = 'unknown';
- }
- if ($fileType == 'jpg' || $fileType == 'gif' || $fileType == 'png' || $fileType == 'bmp'){
- $imageInfo = getimagesize($img);
- $imgInfo['size'] = $imageInfo['bits'];
- $imgInfo["type"] = $fileType;
- $imgInfo["width"] = $imageInfo[0];
- $imgInfo["height"] = $imageInfo[1];
- return $imgInfo;
- }else{ // 非圖片類文件信息
- $this->error = '圖片類型錯誤';
- return FALSE;
- }
- } // end _getinfo
-
- // 說明:生成縮略圖,等比例縮放或拉伸
- // bool/string thumb(string $uploaded_file, int $thumbWidth, int $thumbHeight, string $thumbTail);
- function thumb($img, $thumbWidth = 300, $thumbHeight = 200,$thumbTail = '_thumb')
- {
- $filename = $img; // 保留一個名字供新的縮略圖名字使用
- $imgInfo = $this->_getinfo($img,$i);
- if(FALSE == $imgInfo)
- {
- return FALSE;
- }
- $imgType = $imgInfo['type'];
- switch ($imgType)
- { // 建立一個圖,並給出擴展名
- case "jpg" :
- $img = imagecreatefromjpeg($img);
- $extName = 'jpg';
- break;
- case 'gif' :
- $img = imagecreatefromgif($img);
- $extName = 'gif';
- break;
- case 'bmp' :
- $img = imagecreatefromgif($img);
- $extName = 'bmp';
- break;
- case 'png' :
- $img = imagecreatefrompng($img);
- $extName = 'png';
- break;
- default : // 若是類型錯誤,生成一張空白圖
- $img = imagecreate($thumbWidth,$thumbHeight);
- imagecolorallocate($img,0x00,0x00,0x00);
- $extName = 'jpg';
- }
- // 縮放後的圖片尺寸(小則拉伸,大就縮放)
- $imgWidth = $imgInfo['width'];
- $imgHeight = $imgInfo['height'];
- if($imgHeight > $imgWidth)
- { // 豎圖
- $newHeight = $thumbHeight;
- $newWidth = ceil($imgWidth / ($imgHeight / $thumbHeight ));
- }
- else if($imgHeight < $imgWidth)
- { // 橫圖
- $newHeight = ceil($imgHeight / ($imgWidth / $thumbWidth ));
- $newWidth = $thumbWidth;
- }
- else if($imgHeight == $imgWidth)
- { // 等比例圖
- $newHeight = $thumbWidth;
- $newWidth = $thumbWidth;
- }
- $bgimg = imagecreatetruecolor($newWidth,$newHeight);
- $bg = imagecolorallocate($bgimg,0x00,0x00,0x00);
- imagefill($bgimg,0,0,$bg);
- $sampled = imagecopyresampled($bgimg,$img,0,0,0,0,$newWidth,$newHeight,$imgWidth,$imgHeight);
- if(!$sampled )
- {
- $this->error = '縮略圖生成失敗';
- $this->path=$this->uploadFolder . '/' . $filename;
- return FALSE;
- }
- $filename = basename($filename);
- $newFileName = substr($filename, 0, strrpos($filename, ".")) . $thumbTail . '.' . $extName ; // 新名字
- $thumbPath = $this->thumbFolder . '/' . $newFileName;
- switch ($extName){
- case 'jpg':
- $result = imagejpeg($bgimg, $thumbPath);
- break;
- case 'gif':
- $result = imagegif($bgimg, $thumbPath);
- break;
- case 'png':
- $result = imagepng($bgimg, $thumbPath);
- break;
- default: // 上邊判斷類型出錯時會建立一張空白圖,並給出擴展名爲jpg
- $result = imagejpeg($bgimg, $thumbPath);
- }
- if ($result)
- {
- $this->thumbPath = $thumbPath;
- $this->path=$this->uploadFolder . '/' . $filename;
- return $thumbPath;
- }
- else
- {
- $this->error = '縮略圖建立失敗';
- $this->path=$this->uploadFolder . '/' . $filename;
- return FALSE;
- }
- } // end thumb
-
- // 說明:建立圖片的存放目錄
- private function _mkdir()
- { // 建立圖片上傳目錄和縮略圖目錄
- if(!is_dir($this->uploadFolder))
- {
- $dir = explode('/', $this->uploadFolder);
- foreach($dir as $v)
- {
- if($v)
- {
- $d .= $v . '/';
- if(!is_dir($d))
- {
- $state = mkdir($d);
- if(!$state)
- {
- $this->error = '在建立目錄' . $d . '時出錯!';
- }
-
- }
- }
- }
- }
- if(!is_dir($this->thumbFolder) && TRUE == $this->autoThumb)
- {
- $dir = explode('/', $this->thumbFolder);
- foreach($dir as $v)
- {
- if($v)
- {
- $d .= $v . '/';
- if(!is_dir($d))
- {
- $state = mkdir($d);
- if(!$state)
- {
- $this->error = '在建立目錄' . $d . '時出錯!';
- }
-
- }
- }
- }
- }
- }
- }
- ?>
歡迎關注本站公眾號,獲取更多信息