您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php 图片上传并生成缩略图代码

2025/10/22 22:09:01发布23次查看
本代码的功能是把你要上传的图片保存到服务器,然后再把当前图重按比例生成一个小的缩略图哦。
if($_files['image']['size']){
     if($_files['image']['type'] == image/pjpeg){
      $im = @imagecreatefromjpeg($_files['image']['tmp_name']);
      $n_bmp.='.jpg';
     }elseif($_files['image']['type'] == image/x-png){
      $im = @imagecreatefrompng($_files['image']['tmp_name']);
      $n_bmp.='.png';
     }elseif($_files['image']['type'] == image/gif){
      $im = @imagecreatefromgif($_files['image']['tmp_name']);
      $n_bmp.='.gif';
     }  
    resizeimage($im,8888,8888,$n_bmp);    
       imagedestroy ($im);
    $n_tag =1;
}
function uploadfile($str){
$save_path = './product/';//文件保存目录路径
$ext_arr   = array('rar','zip','jpg','gif','png','bmp');//定义允许上传的文件扩展名
$max_size  = 1000000;//最大文件大小
$file_rand ='';
$file_ext  ='';
@mkdir($save_path, 0777); //更改目录权限
if ($_files[$str]['name']) {//有上传文件时
$file_name = $_files[$str]['name'];//原文件名
$tmp_name = $_files[$str]['tmp_name'];//服务器上临时文件名
$file_size = $_files[$str]['size'];//文件大小
if (@is_dir($save_path) === false) {//检查目录
     alert(上传目录不存在。,'');
    }
if (@is_writable($save_path) === false) {//检查目录写权限
     alert(上传目录没有写权限。,'');
    }
if (@is_uploaded_file($tmp_name) === false) {//检查是否已上传
     alert(临时文件可能不是上传文件。,'');
    }
if ($file_size > $max_size) {//检查文件大小
     alert(上传文件大小超过限制。,'');
    }
$temp_arr = explode(., $file_name);//获得文件扩展名
    $file_ext = array_pop($temp_arr);
    $file_ext = trim($file_ext);
    $file_ext = strtolower($file_ext);
    $file_rand= md5(date(y-m-d));
    if (in_array($file_ext, $ext_arr) === false) {//检查扩展名
     alert(上传文件扩展名是不允许的扩展名。,'');
    }
if (move_uploaded_file($tmp_name, $save_path.$file_rand.'.'.$file_ext) === false) {//移动文件
     alert(上传文件失败。,'');
    }    
   } 
  return $save_path.$file_rand.'.'.$file_ext;
 }
function resizeimage($im,$maxwidth,$maxheight,$name){
   $width = imagesx($im);
   $height = imagesy($im);
  if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
   if($maxwidth && $width > $maxwidth){
   $widthratio = $maxwidth/$width;
   $resizewidth=true;
   }
   if($maxheight && $height > $maxheight){
   $heightratio = $maxheight/$height;
   $resizeheight=true;
   }
  if($resizewidth && $resizeheight){
  if($widthratio    $ratio = $widthratio;
   }else{
   $ratio = $heightratio;
   }
  }elseif($resizewidth){
   $ratio = $widthratio;
  }elseif($resizeheight){
   $ratio = $heightratio;
  }
  $newwidth = $width * $ratio;
  $newheight = $height * $ratio;
  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,'./product_e/'.$name);
  imagedestroy ($newim);
  }else{
  imagejpeg ($im,'./product_e/'.$name);
  }
  }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product