/** * 创建图片 * @param $uploadedfile 需生成的图片路径 * @param $name 生成后的返回的图片名称 * @param $path 需要存放的图片上级目录 * @param $size 缩略图的尺寸(array) * @param $max_pixel 尺寸的最大值(M) * return 生成后的图片名 */ function hg_mk_images($uploadedfile, $name, $path, $size, $max_pixel = 2) { if (!$uploadedfile || !$name || !$path || !is_array($size)) { return OBJECT_NULL; } include_once ROOT_PATH . 'lib/class/gdimage.php'; //源文件 if (filesize($uploadedfile) / 1024 / 1024 >= $max_pixel) { return PIXEL_ERROR; } $image = getimagesize($uploadedfile); $width = $image[0]; $height = $image[1]; $file_name = $name; //目录 $file_dir = $path; //文件路径 $file_path = $file_dir . $file_name; if (!hg_mkdir($file_dir)) { return UPLOAD_ERR_NO_FILE; } if (!copy($uploadedfile, $file_path)) { return UPLOAD_ERR_NO_FILE; } $img = new GDImage($file_path, $file_path, ''); foreach ($size as $key => $value) { $new_name = $value['label'] . $file_name; $save_file_path = $file_dir . $new_name; $img->init_setting($file_path, $save_file_path, ''); $img->maxHeight = $value['height']; $img->maxWidth = $value['width']; /* if($width > $height) { $img->maxWidth = $width > $value['width']?$value['width'] : $width; $img->maxHeight = $height * ($img->maxWidth/$width); } else { $img->maxHeight = $height > $value['height']?$value['height'] : $height; $img->maxWidth = $width * ($img->maxHeight/$height); } */ $img->makeThumb(3); } return $file_name; }
/** * 头像生成接口 * @return array 头像地址 */ public function uploadImage() { if (!$this->input['user_id']) { $userinfo = $this->mUser->verify_credentials(); } else { $userinfo['id'] = intval($this->input['user_id']); } if (!$userinfo['id']) { $this->errorOutput(USENAME_NOLOGIN); } $files = $_FILES['files']; include_once ROOT_DIR . 'lib/class/gdimage.php'; //源文件 $uploadedfile = $files['tmp_name']; //源文件类型 $tmp = explode('.', $uploadedfile); $file_type = $tmp[1]; //文件名 $file_name = $userinfo['id'] . ".jpg"; //目录 $file_dir = AVATAR_DIR . ceil($userinfo['id'] / NUM_IMG) . "/"; //文件路径 $file_path = $file_dir . $file_name; $size = array("larger" => array(LARGER_IMG_WIDTH, LARGER_IMG_HEIGHT), "middle" => array(MIDDLE_IMG_WIDTH, MIDDLE_IMG_HEIGHT), "small" => array(SMALL_IMG_WIDTH, SMALL_IMG_HEIGHT)); if (!hg_mkdir($file_dir)) { $this->errorOutput(UPLOAD_ERR_NO_FILE); } if (!move_uploaded_file($uploadedfile, $file_path)) { $this->errorOutput(UPLOAD_ERR_NO_FILE); } //如果传递了裁剪信息 if ($this->input['cut_info']) { $cut_info = urldecode($this->input['cut_info']); $info = explode(',', $cut_info); //裁剪的起点坐标 $src_x = $info[0]; $src_y = $info[1]; //裁剪图片的大小 $src_w = $info[2]; $src_h = $info[3]; $src_img = imagecreatefromjpeg($file_path); $dst_img = imageCreateTrueColor($src_w, $src_h); imageCopy($dst_img, $src_img, 0, 0, $src_x, $src_y, $src_w, $src_h); imageJPEG($dst_img, $file_path, 100); } $img = new GDImage($file_path, $file_path, ''); $info = array(); foreach ($size as $key => $value) { $save_file_path = $file_dir . $key . '_' . $file_name; $img->init_setting($file_path, $save_file_path, ''); $img->maxWidth = $value[0]; $img->maxHeight = $value[1]; $img->makeThumb(3, false); $info[$key] = AVATAR_URL . ceil($userinfo['id'] / NUM_IMG) . "/" . $key . '_' . $file_name . "?" . hg_rand_num(7); } $info['ori'] = AVATAR_URL . ceil($userinfo['id'] / NUM_IMG) . "/" . $file_name . "?" . hg_rand_num(7); $sql = "UPDATE " . DB_PREFIX . "member \r\n\t\tSET avatar = '" . $userinfo['id'] . ".jpg' \r\n\t\tWHERE id=" . $userinfo['id']; $this->db->query($sql); $info['id'] = $userinfo['id']; $this->setXmlNode('img', 'imagefile'); $this->addItem($info); return $this->output(); }
/** *修改视频图片 */ public function update_schematic() { $userinfo = $this->mUser->verify_credentials(); if (!$userinfo['id']) { $this->errorOutput(USENAME_NOLOGIN); } $video_id = intval(trim($this->input['video_id'])); $file_name = urldecode($this->input['schematic']); if (!$video_id) { $this->errorOutput(OBJECT_NULL); } $files = $_FILES['files']; include_once ROOT_DIR . 'lib/class/gdimage.php'; //源文件 $uploadedfile = $files['tmp_name']; $image = getimagesize($uploadedfile); $width = $image[0]; $height = $image[1]; $size = array("big" => array('t' => "b_", 'width' => VIDEO_IMG_WIDTH * VIDEO_IMG_MULTIPLE, 'height' => VIDEO_IMG_HEIGHT * VIDEO_IMG_MULTIPLE), "normal" => array('t' => "n_", 'width' => VIDEO_IMG_WIDTH, 'height' => VIDEO_IMG_HEIGHT)); //文件名 if (!$file_name) { $file_name = hg_generate_user_salt(16) . ".jpg"; } else { str_replace(DOMAIN, "", $file_name, $cnt); if ($cnt) { $file_name = hg_generate_user_salt(16) . ".jpg"; } else { $arr = explode("/", $file_name); $file_name = substr($arr[count($arr) - 1], 2); if (!trim($file_name)) { $file_name = hg_generate_user_salt(16) . ".jpg"; } } } //目录 $file_dir = UPLOAD_DIR . VIDEO_DIR . ceil($video_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, ''); $img->maxWidth = $value['width']; $img->maxHeight = $value['height']; $img->makeThumb(3, false); $info[$key] = UPLOAD_URL . VIDEO_DIR . ceil($video_id / NUM_IMG) . "/" . $new_name . "?" . hg_generate_user_salt(5); } $sql = "UPDATE " . DB_PREFIX . "video SET bschematic = '" . $file_name . "' , schematic = '" . $file_name . "' WHERE id = " . $video_id; $info['ori'] = $file_name; $info['id'] = $video_id; $this->db->query($sql); $this->setXmlNode('media', 'info'); $this->addItem($info); return $this->output(); }
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; }
function hg_turn_img($file_path, $save_file_path, $direction) { include_once ROOT_PATH . 'lib/class/gdimage.php'; $angle = ''; switch ($direction) { case 1: //左旋转 $angle = '90'; break; case 2: //右旋转 $angle = '-90'; break; case 3: //180度 掉头 $angle = '180'; break; default: return false; break; } $img = new GDImage(); $img->init_setting($file_path, $save_file_path, '', $angle); $img->turnImg(); return true; }
public function handle($video_id, $uploadedfile, $title = "") { //源文件 $image = getimagesize($uploadedfile); $width = $image[0]; $height = $image[1]; //文件名 $file_name = md5($video_id) . ".jpg"; $size = $this->settings['video_img_size']; //目录 $file_dir = UPLOAD_DIR . VIDEO_DIR . ceil($video_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)) { return "ID为" . $video_id . "视频《" . $title . "》缩略图<span style='color:red;'>生成失败!</span><br />"; } $img = new GDImage($file_path, $file_path, ''); $info = array(); foreach ($size as $key => $value) { $new_name = $value['label'] . $file_name; $save_file_path = $file_dir . $new_name; $img->init_setting($file_path, $save_file_path, ''); $img->maxWidth = $value['width']; $img->maxHeight = $value['height']; $img->makeThumb(3, false); $info[$key] = UPLOAD_URL . VIDEO_DIR . ceil($video_id / NUM_IMG) . "/" . $new_name; } $sql = "update " . DB_PREFIX . "video set images='" . $file_name . "' where id=" . $video_id; $this->db->query($sql); return "ID为" . $video_id . "视频《" . $title . "》缩略图生成成功!<br />"; }
function logo() { $mInfo = $this->mUser->verify_credentials(); if (!$mInfo) { $this->errorOutput(USENAME_NOLOGIN); } $logo = urldecode($this->input['logo']) ? urldecode($this->input['logo']) : ''; $files = $_FILES['files']; $uploadedfile = $files['tmp_name']; $image = getimagesize($uploadedfile); $width = $image[0]; $height = $image[1]; if ($width < LOGO_SIZE_SMALL && $height < LOGO_SIZE_SMALL) { $this->setXmlNode('station', 'info'); $this->addItem(""); $this->output(); } if ($logo) { $size = array("logo_o" => array('t' => "", 'size' => LOGO_SIZE_SMALL), "small" => array('t' => "s_", 'size' => LOGO_SIZE_SMALL)); $infos = array(); foreach ($size as $key => $value) { $new_name = $value['t'] . $logo; $infos[$key] = UPLOAD_DIR . LOGO_DIR . ceil($mInfo['id'] / NUM_IMG) . "/" . $new_name; if (is_file($infos[$key])) { unlink($infos[$key]); } } } $sql = 'SELECT * FROM ' . DB_PREFIX . 'network_station WHERE user_id = ' . $mInfo['id']; $q = $this->db->query_first($sql); if ($q) { $sta_id = $q['id']; } else { $sta_id = 0; } include_once ROOT_DIR . 'lib/class/gdimage.php'; //源文件 //文件名 $file_name = hg_generate_user_salt(5) . ".jpg"; $size = array("small" => array('t' => "s_", 'size' => LOGO_SIZE_SMALL)); //目录 $file_dir = UPLOAD_DIR . LOGO_DIR . ceil($mInfo['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, ''); $img->maxWidth = $value['size']; $img->maxHeight = $value['size']; $img->makeThumb(3, false); $info[$key] = UPLOAD_URL . LOGO_DIR . ceil($mInfo['id'] / NUM_IMG) . "/" . $new_name . "?" . hg_rand_num(7); } if ($sta_id) { $sql = "UPDATE " . DB_PREFIX . "network_station SET \r\n\t\t\tlogo='" . $file_name . "' \r\n\t\t\tWHERE id = " . $sta_id . " AND user_id = " . $mInfo['id']; $this->db->query($sql); $info['id'] = 0; } else { $sql = "INSERT INTO " . DB_PREFIX . "network_station(user_id,logo,create_time) VALUES(" . $mInfo['id'] . ",'" . $file_name . "'," . time() . ")"; $this->db->query($sql); $info['id'] = $this->db->insert_id(); include_once ROOT_PATH . 'lib/user/user.class.php'; $this->mUser = new user(); $this->mUser->update_type($mInfo['id']); } $info['logo'] = $file_name; $this->setXmlNode('station', 'info'); $this->addItem($info); $this->output(); }