/** * 上传文件 * @return string */ public function uploadFile() { if($this->checkError() && $this->checkSize() && $this->checkExt() && $this->checkMime() && $this->checkTrueImg() && $this->checkHTTPPost()) { $this->checkUploadPath(); $this->uniName = $this->fileInfo['name'];//图片名字 $this->destination = $this->uploadPath . '/' . $this->uniName; if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)) { $im = $this->destination; $tofile = $this->uploadPath . '/'.'tmp'.'/'. $this->uniName ; $maxwidth = 600; //设置图片的最大宽度 $maxheight = 800; //设置图片的最大高度 $res = $this->resize($im,$tofile,$maxwidth,$maxheight,''); if($res) return $res; else $this->error = '014'; $this->showError(); } else { $this->error = '013'; $this->showError(); } } else { $this->showError(); } } /** * 设置宽高 按比例缩放 */ function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100) { list($width, $height, $type, $attr) = getimagesize($srcImage); if($width < $maxWidth || $height < $maxHeight) return ; switch ($type) { case 1: $img = imagecreatefromgif($srcImage); break; case 2: $img = imagecreatefromjpeg($srcImage); break; case 3: $img = imagecreatefrompng($srcImage); break; } $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例 if($scale < 1) { $newWidth = floor($scale*$width); $newHeight = floor($scale*$height); $newImg = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); $newName = ""; $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile); switch($type) { case 1: if(imagegif($newImg, "$toFile$newName.gif" )) return "$toFile$newName.gif"; break; case 2: if(imagejpeg($newImg, "$toFile$newName.jpg")) return "$toFile$newName.jpg"; break; //case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality)) case 3: if(imagepng($newImg, "$toFile$newName.png")); return "$toFile$newName.png"; break; default: if(imagejpeg($newImg, "$toFile$newName.jpg")) return "$toFile$newName.jpg"; break; } imagedestroy($newImg); } imagedestroy($img); return false; }