php上传图片及缩略图生成

在网上找了2小时代码,一个也不能用。。。哭了

###生成文件夹代码,比如sdfsdf/sdfsd/sdf/sdfs/dfsd/fsdfsdf/sdfsd/

 function createFolder($dirpic)
 {
    if (!file_exists($dirpic))
    {
    createFolder(dirname($dirpic));
    mkdir($dirpic, 0777);
     }
  }

 

//获取文件后缀名函数
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}

 

    
//获取文件后缀名函数
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}

 

//生成缩略图函数

//$o_photo原来的图片的路径包括文件名例如images/00/000/987/034/2009-08-09pic.jpg

//$d_photo新生成的图片的路径images/00/000/987/034/2009-08-09-smallpic.jpg

//$width---缩略图的宽  $height---高了

function cutphoto($o_photo,$d_photo,$width,$height){

$temp_img = imagecreatefromjpeg($o_photo);
$o_width  = imagesx($temp_img);                                //取得原图宽
$o_height = imagesy($temp_img);                                //取得原图高

//判断处理方法
if($width>$o_width || $height>$o_height){        //原图宽或高比规定的尺寸小,进行压缩

$newwidth=$o_width;
$newheight=$o_height;

if($o_width>$width){
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
}

if($newheight>$height){
$newwidth=$newwidth*$height/$newheight;
$newheight=$height;
}

//缩略图片
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo);               
imagedestroy($new_img);


}else{                                                                                //原图宽与高都比规定尺寸大,进行压缩后裁剪

if($o_height*$width/$o_width>$height){        //先确定width与规定相同,如果height比规定大,则ok
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
$x=0;
$y=($newheight-$height)/2;
}else{                                                                        //否则确定height与规定相同,width自适应
$newwidth=$o_width*$height/$o_height;
$newheight=$height;
$x=($newwidth-$width)/2;
$y=0;
}

//缩略图片
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo);               
imagedestroy($new_img);

$temp_img = imagecreatefromjpeg($d_photo);
$o_width  = imagesx($temp_img);                                //取得缩略图宽
$o_height = imagesy($temp_img);                                //取得缩略图高

//裁剪图片
$new_imgx = imagecreatetruecolor($width,$height);
imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height);
imagejpeg($new_imgx , $d_photo);
imagedestroy($new_imgx);
}
}

 

 

 

 

<?
if($_GET['act']=="ok")
{
$vid=$_POST['vid'];
 $uptypes=array('image/jpg',   //上传文件类型列表
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'application/x-shockwave-flash',
'image/x-png');
$max_file_size=5000000;    //上传文件大小限制, 单位BYTE
$destination_folder="upload/"; //上传文件路径
$watermark=0;    //是否附加水印(1为加水印,其他为不加水印);
$watertype=1;    //水印类型(1为文字,2为图片)
$waterposition=1;    //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
$waterstring="www.netviiwiki.com"; //水印字符串
$waterimg="xplore.gif";   //水印图片
$imgpreview=1;    //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2;   //缩略图比例

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
{
echo "<font color='red'>文件不存在!</font>";
exit;
}

$file = $_FILES["upfile"];
if($max_file_size < $file["size"])
//检查文件大小
{
echo "<font color='red'>文件太大!</font>";
exit;
   }

if(!in_array($file["type"], $uptypes))
//检查文件类型
{
echo "<font color='red'>只能上传图像文件或Flash!</font>";
exit;
}

if(!file_exists($destination_folder))
mkdir($destination_folder);

$filename=$file["tmp_name"];
$image_size = getimagesize($filename);
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo[extension];
$destination = $destination_folder.time().".".$ftype;
if (file_exists($destination) && $overwrite != true)
{
      echo "<font color='red'>同名文件已经存在了!</a>";
      exit;
   }

if(!move_uploaded_file ($filename, $destination))
{
    echo "<font color='red'>移动文件出错!</a>";
    exit;
}

$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
//echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>";
//echo " 宽度:".$image_size[0];
//echo " 长度:".$image_size[1];
$pathImages=$destination_folder.$fname;

$bpathImages=$destination_folder."1".$fname;
copy($pathImages,$bpathImages);

     $uid;
  if($_SESSION['uid']=="")
  {
    $uid= -1;
  }
  else
  {
  $uid=$_SESSION['uid']; 
  }
   $timeadd = date("Y-m-d H:i:s");
     if($_SESSION['admin_name']!="")
  {
   $oldpath=GetPosterByID($vid);
   $bigoldpath=GetBigPosterByID($vid);
   $dirpathpic="../".GetPictureDirPath($vid);
   if(file_exists("../".$bigoldpath)== true)
   {
    unlink("../".$oldpath);
    unlink("../".$bigoldpath);
   }
   else
   {    
          //不存在创建文件夹及文件
       createFolder($dirpathpic);
   }
   rename($pathImages,"../".$bigoldpath);
   rename($bpathImages,"../".$oldpath);
  
  //cutphoto(dir.date("his",time()).".jpg",dir."thumb/".date("his",time())."_thumb.jpg",256,192);//缩略图生成
   cutphoto("../".$oldpath,"../".$oldpath,130,96);
   cutphoto("../".$bigoldpath,"../".$bigoldpath,260,320);
  
  }
  else
  {
     $rs3 = $db1->getfirst("select id from nv_cover order by id desc limit 1");
     $nnid = $rs3['id'] + 1;
     $db1->insert("insert into nv_cover (id,video_id,picurl,timeadd,uid) values ($nnid,$vid,'$pathImages','$timeadd',$uid)");
  }
 
 

//再此加水印
}

?>

 

 

 

html代码:

 

 

<table width="440" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="23"><strong>请选择要上传的图片</strong></td>
  </tr><form enctype="multipart/form-data" method="post" name="upform" action="video_poster.php?act=ok">
  <input type="hidden" name="vid" value="<? echo $vid; ?>" />
  <tr>
    <td><input name="upfile" type="file"   style="width:200;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="40">
<br>
允许上传的文件类型为:jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf </td>
  </tr>
  <tr>
    <td height="44"><input type="submit" name="button" id="button" value="提交" />
     </td>
  </tr>
 
  </form>
</table>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值