最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php文件上传程序(支持缩略图)
时间:2010-09-11 编辑:简简单单 来源:一聚教程网
代码如下 | 复制代码 |
/*********************** 程序:上传文件 功能:上传文件、缩略图、加水印 ****************************/ include("common/upfiles.class.php"); $path="../upload/coolsite"; //文件上传路径 $mix="smallimg"; //缩略图路径(在upload下建立) $mark="markimg"; //加水引的图片存放路径(在upload下建立) $text = array("www.111com.net"); //水印内容 $oupload= new upfiles($path,$mix,$mark); //实例化类文件 if(isset($_post['up'])){ //echo $newsmallimg; //输出缩略图路径
/*========================= if($this->uptype){ |
代码如下 | 复制代码 |
if(@empty($this->upname)) die("没有上传文件信息,请确认 返回"); //$name = explode(".",$this->upname);//将上传前的文件以"."分开取得文件类型 //$filcount = count($name);//获得截取的数量 //$filtype = $name[$filcount-1];//取得文件的类型 $filtype = $fileextname; if(strpos($this->upfiletype,$filtype) === false){ die("上传文件类型仅支持 ".$this->upfiletype." 不支持 ".$filtype ." 返回"); } $filsize = $this->upsize; if(round($filsize/1024) > ($this->upfilemax*1024)) { die("上传文件超过 ".$this->upfilemax."kb"); } $filename = "es_".date("ymdhis").".".$filtype;//写入数据库教程的文件名 $fileurl = $this->annexfolder."/".$filename;//上传后的文件名称 $upfileok = move_uploaded_file($this->uptmp_name,$fileurl); if($this->uperror == 0 || $upfileok) { echo "文件上传成功 继续上传"; } else { die("上传文件失败,请确认你的上传文件不超过 $upfilemax kb 或上传时间超时"); } return $filename; //return $fileurl; } public function getinfo($photo) { $photo = $this->annexfolder."/".$photo; $imageinfo = getimagesize($photo); $imginfo["width"] = $imageinfo[0]; $imginfo["height"] = $imageinfo[1]; $imginfo["type"] = $imageinfo[2]; $imginfo["name"] = basename($photo); return $imginfo; } /*========================= //缩略图 ===========================*/ public function smallimg($photo,$width=250,$height=192) { $imginfo = $this->getinfo($photo); $photo = $this->annexfolder."/".$photo;//获得图片源 $newname = substr($imginfo["name"],0,strrpos($imginfo["name"], "."))."_thumb.jpg";//新图片名称 if($imginfo["type"] == 1) { $img = imagecreatefromgif($photo); } elseif($imginfo["type"] == 2) { $img = imagecreatefromjpeg($photo); } elseif($imginfo["type"] == 3) { $img = imagecreatefrompng($photo); } else { $img = ""; } if(empty($img)) return false; $width = ($width > $imginfo["width"]) ? $imginfo["width"] : $width; $height = ($height > $imginfo["height"]) ? $imginfo["height"] : $height; $srcw = $imginfo["width"]; $srch = $imginfo["height"]; if ($srcw * $width > $srch * $height) { $height = round($srch * $width / $srcw); } else { $width = round($srcw * $height / $srch); } if (function_exists("imagecreatetruecolor")) { $newimg = imagecreatetruecolor($width, $height); imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]); } else { $newimg = imagecreate($width, $height); imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]); } if ($this->tofile) { if (file_exists($this->smallfolder."/".$newname)){ @unlink($this->smallfolder."/".$newname); } imagejpeg($newimg,$this->smallfolder."/".$newname); return $this->smallfolder."/".$newname; } else { imagejpeg($newimg); } imagedestroy($newimg); imagedestroy($img); return $newname; } /*========================= //加水印 ===========================*/ public function watermark($photo,$text) { $imginfo = $this->getinfo($photo); $photo = $this->annexfolder."/".$photo; $newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . "_mark.jpg"; //$newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . ".jpg"; switch ($imginfo["type"]) { case 1: $img = imagecreatefromgif($photo); break; case 2: $img = imagecreatefromjpeg($photo); break; case 3: $img = imagecreatefrompng($photo); break; default: return false; } if (empty($img)) return false; $width = ($this->maxwidth > $imginfo["width"]) ? $imginfo["width"] : $this->maxwidth; $height = ($this->maxheight > $imginfo["height"]) ? $imginfo["height"] : $this->maxheight; $srcw = $imginfo["width"]; $srch = $imginfo["height"]; if ($srcw * $width > $srch * $height) { $height = round($srch * $width / $srcw); } else { $width = round($srcw * $height / $srch); } if (function_exists("imagecreatetruecolor")) { $newimg = imagecreatetruecolor($width, $height); imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]); } else { $newimg = imagecreate($width, $height); imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]); } $white = imagecolorallocate($newimg, 255, 255, 255); $black = imagecolorallocate($newimg, 0, 0, 0); $alpha = imagecolorallocatealpha($newimg, 230, 230, 230, 80); //imagefilledrectangle($newimg, 0, $height-26, $width, $height, $alpha); //imagefilledrectangle($newimg, 13, $height-20, 15, $height-7, $black); imagettftext($newimg, 14, 0, 20, $height-14, $white, $this->fonttype, $text[0]); imagettftext($newimg, 14, 0, 20, $height-6, $white, $this->fonttype, $text[1]); if($this->tofile) { if (file_exists($this->markfolder."/".$newname)){ @unlink($this->markfolder."/".$newname); } imagejpeg($newimg,$this->markfolder."/".$newname); if(file_exists($this->annexfolder."/".$newname)){ unlink($this->annexfolder."/".$newname); } return $this->markfolder."/".$newname; } else { imagejpeg($newimg); } imagedestroy($newimg); imagedestroy($img); return $newname; } }?> |
-
上一个: php 邮件发送程序代码(支持附件)
-
下一个: php mysql 数据库备份程序
相关文章
- PHP导出数据超时的优化建议解读 10-31
- PHP之mysql位运算解析 10-31
- Laravel实现登录跳转功能解析 10-31
- php双向队列解读 10-31
- Laravel异常上下文解决教程 10-24
- php数组查询元素位置方法介绍 10-24