Exemplo n.º 1
0
 private function handle($uploadedfile)
 {
     include_once ROOT_DIR . 'lib/class/gdimage.php';
     //源文件
     if (filesize($uploadedfile) / 1024 / 1024 >= IMG_SIZE) {
         $this->errorOutput(IMG_SIZE_ERROR);
     }
     $image = getimagesize($uploadedfile);
     $width = $image[0];
     $height = $image[1];
     if (strpos(strtolower($image['mime']), 'jpeg')) {
         $type = '.jpg';
     } else {
         if (strpos(strtolower($image['mime']), 'png')) {
             $type = '.png';
         } else {
             if (strpos(strtolower($image['mime']), 'gif')) {
                 $type = '.gif';
             }
         }
     }
     //文件名
     $file_name = hg_generate_user_salt(16) . ".jpg";
     $size = array("larger" => array('t' => "l_", 'size' => IMG_SIZE_LARGER), "middle" => array('t' => "m_", 'size' => IMG_SIZE_MIDDLE), "small" => array('t' => "s_", 'size' => IMG_SIZE_SMALL));
     //目录
     $file_dir = UPLOAD_DIR . IMG_DIR . ceil($userinfo['id'] / NUM_IMG) . "/";
     //文件路径
     $file_path = $file_dir . $file_name;
     if (!hg_mkdir($file_dir)) {
         $this->errorOutput(UPLOAD_ERR_NO_FILE);
     }
     if (!copy($uploadedfile, $file_path)) {
         $this->errorOutput(UPLOAD_ERR_NO_FILE);
     }
     $img = new GDImage($file_path, $file_path, '');
     $info = array();
     foreach ($size as $key => $value) {
         $new_name = $value['t'] . $file_name;
         $save_file_path = $file_dir . $new_name;
         $img->init_setting($file_path, $save_file_path, '');
         if ($key == "larger") {
             $img->maxWidth = $width > $value['size'] ? $value['size'] : $width;
             $img->maxHeight = $height * ($img->maxWidth / $width);
             $img->makeThumb(1, false, true);
         } else {
             if ($width > $height) {
                 $img->maxWidth = $width > $value['size'] ? $value['size'] : $width;
                 $img->maxHeight = $height * ($img->maxWidth / $width);
             } else {
                 $img->maxHeight = $height > $value['size'] ? $value['size'] : $height;
                 $img->maxWidth = $width * ($img->maxHeight / $height);
             }
             $img->makeThumb(3);
         }
         $info[$key] = UPLOAD_URL . IMG_DIR . ceil($userinfo['id'] / NUM_IMG) . "/" . $new_name;
         if (defined('WATER_MARK_DONE') && WATER_MARK_DONE == true) {
             $img->create_watermark($save_file_path, $type, 4, WATER_MARK_IMG);
         }
     }
     $ip = hg_getip();
     $create_at = time();
     $sql = "INSERT " . DB_PREFIX . "media(status_id,dir,url,ip,create_at) VALUES(0,'" . IMG_DIR . ceil($userinfo['id'] / NUM_IMG) . "/" . "','" . $file_name . "','" . $ip . "'," . $create_at . ")";
     $this->db->query($sql);
     $id = $this->db->insert_id();
     $info['id'] = $id;
     $info['url'] = $file_name;
     $info['ip'] = $ip;
     $info['create_at'] = $create_at;
     $info['type'] = 0;
     return $info;
 }