图片上传 宽高截取

来源:互联网 发布:手机装机必备软件知乎 编辑:程序博客网 时间:2024/06/10 15:02

<?php 
class upphoto{ 
 public $ph_name;   //上传图片文件名 
 public $ph_path="img/upload/";    //上传文件存放路径 
 public $al_ph_type=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png');    //允许上传图片类型 
 public $al_ph_size=2097152; //允许上传文件大小 
  function __construct(){ 
   //文件夹路径
    $this->ph_path.=date('Ym').'/';
  } 

  //获取原文件名 
  function get_ph_name($phname){ 
   //文件名称时间随机
   $datetime=time().rand(10,99);
    $this->ph_name=$this->ph_path.$datetime.strrchr($phname,"."); //strrchr获取文件的点最后一次出现的位置 
  return $this->ph_name; 
 } 
 //判断上传文件存放目录 
  function check_path(){
    if(!file_exists($this->ph_path)){ 
      mkdir($this->ph_path,0777,true); 
   } 
  } 
  //判断上传文件是否超过允许大小 
  function check_size($size){ 
    if($size> $this->al_ph_size){ 
     return $this->showerror("上传图片超过2M"); 
    } 
  } 
  //判断文件类型 
  function check_type($type){ 
   if(!in_array($type,$this->al_ph_type)){ 
        return $this->showerror("上传图片类型错误"); 
   } 
  }
   
  //上传图片 
  function up_photo($file){ 
   $this->check_path(); 
    $this->get_ph_name($file['name']);
    if(!move_uploaded_file($file['tmp_name'],$this->ph_name)){ 
     $this->showerror("上传文件出错"); 
    } 
   return array('error'=>'','file'=>'/'.$this->ph_name);
  } 
 
  //错误提示 
  function showerror($errorstr){ 
    return array('error'=>$errorstr,'file'=>''); 
  } 

  //上传 处理图片
  function save($file,$width=0,$height=0)
  { 
   if (empty($file) || $file['error']!=0)
   {
   return  $this->showerror("上传错误"); 
   }
   $type=$this->check_type($file['type']);
   if (!empty($type['error']))
    {
     return $type;
    }
    $size=$this->check_size($file['size']);
   if (!empty($size['error']))
    {
     return $size;
    }
   $res= $this->up_photo($file);
   if (!empty($res['file']))
   {
    if ($width>0)
    {
     $this->resize_image(substr($res['file'], 1),substr($res['file'], 1),$width,$height);
    }
   }
   return $res;
  } 
 
/**
 * 改变图片的宽高
 *
 * @author flynetcn (2009-12-16)
 *
 * @param string $img_src 原图片的存放地址或url
 * @param string $new_img_path  新图片的存放地址
 * @param int $new_width  新图片的宽度
 * @param int $new_height 新图片的高度 默认等比例压缩
 * @return bool  成功true, 失败false
 */
function resize_image($img_src, $new_img_path, $new_width, $new_height=0)
{
    $img_info = @getimagesize($img_src);
 if (!$img_info)
    {
     return false;
    }
    if ($img_info[0]<=$new_width)
    {
     return false;
    }
    //高度自动变换
    if (empty($new_height))
    {
     $r=round(($new_width/$img_info[0]),2);
     $new_height=round($img_info[1]*$r);
    }
    if ($new_width < 1 || $new_height < 1 || empty($new_img_path)) {
        return false;
    }
   
    if (strpos($img_info['mime'], 'jpeg') !== false) {
        $pic_obj = imagecreatefromjpeg($img_src);
    } else if (strpos($img_info['mime'], 'gif') !== false) {
        $pic_obj = imagecreatefromgif($img_src);
    } else if (strpos($img_info['mime'], 'png') !== false) {
        $pic_obj = imagecreatefrompng($img_src);
    } else {
        return false;
    }
    $pic_width = imagesx($pic_obj);
    $pic_height = imagesy($pic_obj);
    if (function_exists("imagecopyresampled")) {
        $new_img = imagecreatetruecolor($new_width,$new_height);
        imagecopyresampled($new_img, $pic_obj, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
    } else {
        $new_img = imagecreate($new_width, $new_height);
        imagecopyresized($new_img, $pic_obj, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
    }
    if (preg_match('~.([^.]+)$~', $new_img_path, $match)) {
        $new_type = strtolower($match[1]);
        switch ($new_type) {
            case 'jpg':
                imagejpeg($new_img, $new_img_path);
                break;
            case 'gif':
                imagegif($new_img, $new_img_path);
                break;
            case 'png':
                imagepng($new_img, $new_img_path);
                break;
            default:
                imagejpeg($new_img, $new_img_path);
        }
    } else {
        imagejpeg($new_img, $new_img_path);
    }
    imagedestroy($pic_obj);
    imagedestroy($new_img);
    return true;
}

?>

调用

include_once(ROOT_PATH.'lib/upphoto/upphoto.php');
  $up=new  upphoto();
   $res=$up->save($_FILES['file'],600); 
   echo json_encode($res);
  die;