PHP图片上传大集合

PHP 1368 0 2013-04-30

PHP图片上传大集合
<?php
//PHP图片上传大集合开始
//验证图片类型是否正确
function CheckImageType($imt){
$imtArr=array("image/jpeg","image/pjpeg","image/x-png","image/gif");
if(!empty($imt) && in_array($imt,$imtArr)){
   return true;
}else{
   return false;
}
}
//验证图片大小
function CheckImageSize($ims,$sizeValue){
//文件的大小,以字节计算
if($ims>$sizeValue){
   return false;
}else{
   return true;
}
}
//生成固定大小(缩略)图
function ResetImage($im,$upImgType,$maxwidth,$maxheight,$uploaddir,$name){
if($upImgType == "image/pjpeg"){
$im = imagecreatefromjpeg($im);
}elseif($upImgType == "image/jpeg"){
$im = imagecreatefrompng($im);
}elseif($upImgType == "image/x-png"){
$im = imagecreatefrompng($im);
}elseif($upImgType == "image/gif"){
$im = imagecreatefromgif($im);
}else{
echo('Upload fail! The format is not supported.');
return false;
}
$width = imagesx($im);
$height = imagesy($im);
if(file_exists("$uploaddir.$name.jpg")){ unlink("$uploaddir.$name.jpg"); } //判断是否存在同名并删除
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
$newwidth = $maxwidth;
$newheight = $maxheight;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$uploaddir . $name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$uploaddir . $name . ".jpg");
}
ImageDestroy ($im);
}
//原样大小上传图片
function UploadImage($tmpName,$FileName,$newName,$uploaddir){
move_uploaded_file($tmpName, $uploaddir . $newName . substr($FileName,strrpos($FileName,".")));
}
//PHP图片上传大集合结束


if($_GET['act']=='up'){
   $upImgSize=$_FILES['image']['size'];      //图片大小
   $upImgType=$_FILES['image']['type'];      //图片类型
   $ImgName=$_FILES["image"]["name"];
   $upImgName=$_FILES['image']['tmp_name']; //存储在服务器的临时副本名称
$FILENAME=date('Ymdhis').rand(1000,9999); //图片名称
$uploaddir='../uploads/'; //图片路径
$RESIZEWIDTH=50;   // 生成图片的宽度
$RESIZEHEIGHT=60; // 生成图片的高度
if(CheckImageType($upImgType) && CheckImageSize($upImgSize,100000)){
ResetImage($upImgName,$upImgType,$RESIZEWIDTH,$RESIZEHEIGHT,$uploaddir,'b'.$FILENAME);
UploadImage($upImgName,$ImgName,'s'.$FILENAME,$uploaddir);
}else{
echo('false');
}
}
?>
<form enctype="multipart/form-data" method="post" action="?act=up">
<input type="file" name="image" size="50" value="浏览"><p>
<input type="submit" value="上传图片">
</form>

上一篇:PHP计算时间剩余时间

下一篇:PHP数组的应用

讨论数量:0

请先登录再发表讨论。 2024-05-04

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链