php 图片上传代码,具有生成缩略图与增加水印功能 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能,可以说是一款完美的图片上传类,实例代码如下:
-
- class upfile {
- public $filepath = "www.phpfensi.com/";
-
- public $filesize = 1000000;
-
-
-
- public $reimagesize = array (
- true,
- 400,
- 300,
- ""
- );
-
- public $india = true;
-
- public $indiaimage = "";
-
- public $indiaimagex = 100;
-
- public $indiaimagey = 10;
-
- public $indiatext = "www.phpfensi.com";
-
- public $fontsize = 6;
-
- public $indiatextx = 10;
-
- public $indiatexty = 10;
-
- public $r = 250;
-
- public $g = 250;
-
- public $b = 250;
-
- public $indiapath = "";
-
-
- function uploadfile($upfile) {
- if ($upfile == "") {
- die("uploadfile:参数不足");
- }
- if (!file_exists($this->filepath)) {
- mkdir($this->filepath);
- }
- $upfiletype = $upfile['type'];
- $upfilesize = $upfile['size'];
- $upfiletmpname = $upfile['tmp_name'];
- $upfilename = $upfile['name'];
- $upfileerror = $upfile['error'];
- if ($upfilesize > $this->filesize) {
- return false;
- }
- switch ($upfiletype) {
- case 'image/jpeg' :
- $type = 'jpg';
- break;
- case 'image/pjpeg' :
- $type = 'jpg';
- break;
- case 'image/png' :
- $type = 'png';
- break;
- case 'image/gif' :
- $type = 'gif';
- break;
- }
- if (!isset ($type)) {
- return false;
- }
- if (!is_uploaded_file($upfiletmpname) or !is_file($upfiletmpname)) {
- return false;
- ;
- }
- if ($this->upfileerror != 0) {
- return false;
- }
- if ($this->upfileerror == 0) {
- if (!file_exists($upfiletmpname)) {
- return false;
- } else {
- $filename = date("ymdhis", time() + 3600 * 8);
- $filename = $this->filepath . $filename . "." . $type;
- if (!move_uploaded_file($upfiletmpname, $filename)) {
- return false;
- } else {
- if ($this->india == true) {
- $this->goindia($filename, $type,true);
- } else {
- if ($this->reimagesize[0] == true) {
- $this->goreimagesize($filename, $type);
- } else {
- return true;
- unlink($upfiletmpname);
- }
- }
- }
-
- }
- }
-
- }
-
- function goindia($filename, $filetype,$reimage=false) {
- if (!file_exists($filename)) {
- $this->reerror(7);
- } else {
- if ($filetype == "jpg") {
- $im = imagecreatefromjpeg($filename);
- } else
- if ($filetype == "gif") {
- $im = imagecreatefromgif($filename);
- } else
- if ($filetype == "png") {
- $im = imagecreatefrompng($filename);
- }
- if ($this->indiatext != "") {
- $textcolor = imagecolorallocate($im, $this->r, $this->g, $this->b);
- imagestring($im, $this->fontsize, $this->indiatextx, $this->indiatexty, $this->indiatext, $textcolor);
- }
- if ($this->indiaimage != "") {
- $indiaimagetype = getimagesize($this->indiaimage);
- $logow = $indiaimagetype[0];
- $logoh = $indiaimagetype[1];
- switch ($indiaimagetype[2]) {
- case 1 :
- $indiaimagetype = "gif";
- $logo = imagecreatefromgif($this->indiaimage);
- break;
- case 2 :
- $indiaimagetype = "jpg";
- $logo = imagecreatefromjpeg($this->indiaimage);
- break;
- case 3 :
- $indiaimagetype = "png";
- $logo = imagecreatefrompng($this->indiaimage);
- break;
- }
- imagealphablending($im, true);
- imagecopy($im, $logo, $this->indiaimagex, $this->indiaimagey, 0, 0, $logow, $logoh);
- imagedestroy($im);
- imagedestroy($logo);
- }
- }
- if ($this->indiapath == "") {
- if ($filetype == "jpg") {
- imagejpeg($im, $filename);
- } else
- if ($filetype == "gif") {
- imagegif($im, $filename);
- } else
- if ($filetype == "png") {
- imagepng($im, $filename);
- }
- if($reimage == true){
- $this->goreimagesize($filename,$filetype);
- }else{
- return true;
- }
- } else {
- if (!file_exists($this->indiapath)) {
- mkdir($this->indiapath);
- return false;
- } else {
- $indianame = basename($filename);
- $indianame = $this->indiapath . $indianame;
- if ($filetype == "jpg") {
- imagejpeg($im, $indianame);
- } else
- if ($filetype == "gif") {
- imagegif($im, $indianame);
- } else
- if ($filetype == "png") {
- imagepng($im, $indianame);
- }
- if($reimage == true){
- $this->goreimagesize($indianame,$filetype);
- echo $indianame;
- }else{
- return true;
- }
- }
- }
- }
- function goreimagesize($filename, $filetype) {
- if (!file_exists($filename)) {
- return false;
- } else {
- if ($filetype == 'jpg') {
- $reimage = imagecreatefromjpeg($filename);
- }
- elseif ($filetype == 'png') {
- $reimage = imagecreatefrompng($filename);
- } else
- if ($filetype == 'gif') {
- $reimage = imagecreatefromgif($filename);
- }
- if (isset ($reimage)) {
- $srcimagetype = getimagesize($filename);
- $srcimagetypew = $srcimagetype[0];
- $srcimagetypeh = $srcimagetype[1];
- $reim = imagecreatetruecolor($this->reimagesize[1], $this->reimagesize[2]);
- imagecopyresized($reim, $reimage, 0, 0, 0, 0, $this->reimagesize[1], $this->reimagesize[2], $srcimagetypew, $srcimagetypeh);
- $reimagepath = $this->reimagesize[3];
- if ($reimagepath != "") {
- if (!file_exists($reimagepath)) {
- mkdir($reimagepath);
- } else {
- $reimagename = basename($filename);
- $reimagename = $reimagepath . "r_" . $reimagename;
- if ($filetype == "gif")
- imagegif($reim, $reimagename);
- else
- if ($filetype == "jpg")
- imagejpeg($reim, $reimagename);
- else
- if ($filetype == "png")
- imagepng($reim, $reimagename);
- return true;
- }
- } else {
- $filename = basename($filename);
- if($this->indiapath == ""){
- $filename = $this->filepath."r_" . $filename;
- }else{
- $filename = $this->indiapath."r_" . $filename;
- }
- if ($filetype == "gif")
- imagegif($reim, $filename);
- else
- if ($filetype == "jpg")
- imagejpeg($reim, $filename);
- else
- if ($filetype == "png")
- imagepng($reim, $filename);
- return true;
- }
-
- }
- }
- }
-
- }
- if ($_post["submit"]) {
- $file = $_files['uploadfile'];
- $upfile = new upfile();
- echo $upfile->uploadfile($file);
- }
- ?>
- method="post" enctype="multipart/form-data">
- "file" name="uploadfile"/>
- "submit" value="上传" name="submit"/>
-
|
|
|
|