求教:php中如何进行图片的上传与显示?求高手帮忙指点下(要完整点的,我用的数据库是Mysql,如能解决,不胜感激!)

解决方案 »

  1.   

    blob类型
    <?php
      class upload_file {
        //保存的文件名
       public $file_name;
     //系统中上传文件的临时存放路径
     public $file_tmp_name;
     //文件大小
     public $file_size;
     //完整的文件类型
     public $full_file_type;
     //文件类型
     public $file_type;
     //文件是否覆盖
     public $override = 1;
     //文件的保存路径
     public $file_save_path = '';
     //上传文件大小的最大值 单位是字节 2M
     public public $file_max_size = 210000000;
     //public public $file_max_size = 102400; //构造函数
     function __construct($file_name = '', $file_tmp_name = '', $full_file_type = '', $file_size = '', $file_save_path = '') {
            $this->file_name = $file_name;
            $this->file_tmp_name = $file_tmp_name;
         $this->full_file_type = $full_file_type;
         $this->file_size = $file_size;
         $this->file_save_path = $file_save_path;
     }
        //取得文件的后缀名,即文件类型
     function get_file_type() {
      $type_array = explode('.', $this->file_name);
            return $type_array[count($type_array)-1];
     }
     //判断文件的大小
     function check_size() {
      if($this->file_size > $this->file_max_size) {
       return false;
      }
      return true;
     }
     //取得文件的大小
     function get_size() {
      return intval($this->file_size/1024);
     }
     //上传图片 格式 jpg,png,gif,pjpeg
     function check_upload_pic() {
      $type = $this->get_file_type();
      $type_array = array('jpg', 'png', 'gif', 'bmp');
      foreach($type_array as $value) {
       if($value = $type) {
        return true;
       }
       return false;
      }
     }
     //上传文件 格式 zip rar
     function check_upload_file() {
      $type = $this->get_file_type();
      $type_array = array('jpg','gif','bmp','png');
      foreach($type_array as $value) {
       if($value == $type) {
        return true;
       }
       return false;
      } }
     //判断文件是否存在
     function check_exist() {
      $file = $this->file_save_path.$this->file_name;
      return file_exists($file);
     }
     //上传文件
     function move_upfile() {
      if(!$this->check_upload_pic()) {
       echo "ok1";
       return false;
      }
      else {
       if(!$this->check_size()) {
        echo "ok2";
        return false;
       }
       else {
    //    if($this->check_exist()) {
    //     echo "该文件已存在";
    //     return false;
    //    }
    //    else {
         $path = $this->file_save_path.$this->file_name;
         if(move_uploaded_file($this->file_tmp_name, $path)) {
          return true;
         }
         else {
          return false;
         }//    }
       }
      }
     }
     //将上传的图片打水印
     /**
      * $water_pic_name 将要被打水印的目标图片
      * $water_word     水印文字
      * $path           将来生成水印图片的存放路径
      */
     function create_water_pic($water_word) {
      $type = $this->get_file_type();
      $filename = $this->file_save_path.$this->file_name;
      switch($type) {
       case 'jpg':
       header("content-type:image/jpeg");          //定义输出图像的类型
       $im = imagecreatefromjpeg($filename);       //载入图片
       break;
       case 'png':
       header("content-type:image/png");
       $im = imagecreatefrompng($filename);
       break;
       case 'gif':
       header("content-type:image/gif");
       $im = imagecreatefromgif($filename);
       break;
       case 'bmp':
       header("content-type:image/xbm");          //上传bmp格式存在问题
       $im = imagecreatefromxbm($filename);       //无法打水印
       break;
       default: {
        echo "文件格式不符";
       }
      }
      $textcolor = imagecolorallocate($im, 56, 73,136);  //设定字体的颜色
      $font = "simhei.ttf";      //定义字体
      $word = $water_word;      //水印字符
      $x = imagesx($im);         //获取图片的宽度
      $y = imagesy($im);         //获取文件的高度
      $position_x = $x-80;
      $position_y = $y-10;
      $str = iconv('gbk', 'utf-8', $word);           //将中文文字显示出来的编码过程
      imagettftext($im, 20, 0, $position_x, $position_y, $textcolor, $font, $str);
      //imagejpeg($im);                                //显示图片
      $new = $this->file_save_path.'water'.$this->file_name;     //生成新的文件名
      switch($type) {
       case 'jpg':
          imagejpeg($im, $new);     //生成jpg图像
       break;
       case 'png':
          imagepng($im, $new);      //生成png图像
       break;
       case 'gif':
          imagegif($im, $new);      //生成gif图像
       break;
       case 'bmp':
          imagexbm($im, $new);      //生成bmp图像     该格式的文件处理有问题
       break;
       default: {
        echo "文件格式不符";
       }
      }
      imagedestroy($im);  //结束图形,释放内存空间*/
     }
     //生成缩略图
     /**
      * $pic 图片名 包括其扩展名,但不包括路径
      * $width  将来生成缩略图的宽度
      * $height 将来生成缩略图的高度
      * $path 生成缩略图的存放路径
      */
     function create_thumbnail($width, $height) {
      $type = $this->get_file_type();
      $filename = $this->file_save_path.$this->file_name;
      $img = getimagesize($filename);
      //print_r($img);
      //die();
      switch($img[2]) {
       case 1:
       header("content-type:image/gif");          //定义输出图像的类型
       $im = imagecreatefromgif($filename);            //载入图片
       break;
       case 2:
       header("content-type:image/jpeg");
       $im = imagecreatefromjpeg($filename);
       break;
       case 3:
       header("content-type:image/png");
       $im = imagecreatefrompng($filename);
       break;
       case 6:
       header("content-type:image/xbm");          //bmp格式存在问题
       $im = imagecreatefromxbm($filename);            //无法打水印
       break;
       default: {
        echo "文件格式不符";
       }
      }
      $thumb = imagecreatetruecolor($width, $height);    //创建一个新的空白的面板
      $color = imagecolorallocate($im, 200, 255, 100);   //调色板
      /*bool imagecopyresized ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
            imagecopyresized() 将一幅图像中的一块正方形区域拷贝到另一个图像中。dst_image 和 src_image 分别是目标图像和源图像的标识符。
      */
      imagecopyresized($thumb, $im, 0, 0, 0, 0, $width, $height, $img[0], $img[1]);
      //imagejpeg($thumb);
      $thumb_path = $this->file_save_path."thumbnail/".$this->file_name;
      switch($img[2]) {
       case 1:
       imagejpeg($thumb, $thumb_path);
       break;
       case 2:
       imagegif($thumb, $thumb_path);
       break;
       case 3:
       imagepng($thumb, $thumb_path);
       break;
       case 6:
       imagexbm($thumb, $thumb_path);
       break;
       default: {
        echo "文件格式不符";
       }
      }
     }
      }
    ?>
    前几天做的一个类,可以正常的使用,但还存在问题,仅供参考!望对您有所帮助