/** *保存图片页 */ public function ActionSavePicture() { //获取参数 $city_id = isset($_GET['city_id']) ? $_GET['city_id'] : ""; $create_thumbnails_pic = isset($_GET['create_thumbnails_pic']) ? $_GET['create_thumbnails_pic'] : ""; $picture_category_id = isset($_GET['picture_category_id']) ? $_GET['picture_category_id'] : ""; $picture_name = isset($_GET['picture_name']) ? $_GET['picture_name'] : ""; $picture_title = isset($_GET['picture_title']) ? $_GET['picture_title'] : ""; $picture_description = isset($_GET['picture_description']) ? $_GET['picture_description'] : ""; //解析图片名称和扩展名 $name_noext = substr($picture_name, 0, strrpos($picture_name, '.')); //名称(无扩展名) $name_ext = strrchr($picture_name, "."); //扩展名 //将图片中文名称转换为拼音 $name_noext_arr = IconvEncode::gbk2Pinyin($name_noext); $name_noext_pinyin = implode("", $name_noext_arr); //中、小图名称 $picture_middle_name = $name_noext_pinyin . "_middle" . $name_ext; //中规格图名称 $picture_samll_name = $name_noext_pinyin . "_samll" . $name_ext; //小规格图名称 //获取图片类型 $picture_category = PictureCategory::model()->findByPk($picture_category_id); if (isset($picture_category) && !empty($picture_category)) { if (isset($picture_category['PictureCategoryPinyin']) && !empty($picture_category['PictureCategoryPinyin'])) { //创建图片路径 $pic_dir_path = IMAGES_PATH . $picture_category['PictureCategoryPinyin'] . '/' . date("Y-m") . '/'; DirectoryFile::dirCreate($pic_dir_path); //大图上传 $pic_big_src = $pic_dir_path . $name_noext_pinyin . $name_ext; //上传原始图片并生产规定格式的中图和小图 file_put_contents($pic_big_src, file_get_contents('php://input')); //存放大图 //图片信息存入数据库 if (empty($picture_title)) { $picture_title = $name_noext; } $picture_model = new Picture(); $picture_model->CityId = $city_id; $picture_model->CategoryId = $picture_category_id; $picture_model->Name = $picture_name; $picture_model->Title = $picture_title; $picture_model->Description = $picture_description; $picture_model->save(); //图片中、小图片路径保存到数据库 $pic_id = $picture_model->attributes['ID']; if (isset($pic_id) && !empty($pic_id)) { $picture_source['big'] = new PictureSource(); $picture_source['big']->PicId = $pic_id; $picture_source['big']->PicSrc = $pic_big_src; $picture_source['big']->SizeType = 0; //原图 // 判断是否生成中、小图 if ($create_thumbnails_pic) { $pic_middle_src = $pic_dir_path . $picture_middle_name; $pic_small_src = $pic_dir_path . $picture_samll_name; ImageChange::imgResize($pic_big_src, $pic_middle_src, 380, 210); //生成中图 ImageChange::imgResize($pic_big_src, $pic_small_src, 180, 120); //生成小图 $picture_source['middle'] = new PictureSource(); $picture_source['middle']->PicId = $pic_id; $picture_source['middle']->PicSrc = $pic_middle_src; $picture_source['middle']->SizeType = 1; //中图 $picture_source['small'] = new PictureSource(); $picture_source['small']->PicId = $pic_id; $picture_source['small']->PicSrc = $pic_small_src; $picture_source['small']->SizeType = 2; //小图 } //事务[S] $transaction = Yii::app()->db->beginTransaction(); //开启事务 try { foreach ($picture_source as $pic_src) { if (!$pic_src->save()) { throw new Exception(); } } $transaction->commit(); //提交事务 } catch (Exception $e) { $transaction->rollBack(); //回滚事务 } //事务[S] } echo $pic_dir_path; } } }
/** * 添加酒店图片 */ public function ActionAddHotelPic() { $isPost = Yii::app()->request->isPostRequest; if (empty($isPost)) { // 获取酒店ID $hotel_id = intval(trim(Yii::app()->request->getParam('hotel_id'))); // 根据酒店ID 获取酒店数据 if (!empty($hotel_id)) { $hotel_model = Hotel::model()->findByPk($hotel_id); $this->render('add_hotel_picture', array('hotel_model' => $hotel_model)); } else { Yii::app()->user->setFlash('参数错误,请刷新后重试'); } } else { // 生成图的名称(大,中,小图)[S] $picture_name = isset($_GET['picture_name']) ? $_GET['picture_name'] : ""; $picture_title = isset($_GET['picture_title']) ? $_GET['picture_title'] : ""; $picture_desc = isset($_GET['picture_description']) ? $_GET['picture_description'] : ""; // 解析图片名称和扩展名 $name_noext = substr($picture_name, 0, strrpos($picture_name, '.')); //名称(无扩展名) $name_ext = strrchr($picture_name, "."); //扩展名 $name_rand = md5(time() . mt_rand(1, 1000000)); // 将图片中文名称转换为拼音 $name_noext_arr = IconvEncode::gbk2Pinyin($name_noext); $name_noext_pinyin = implode("", $name_noext_arr) . $name_rand; // 中、小图名称 $picture_middle_name = $name_noext_pinyin . "_middle" . $name_ext; //中规格图名称 $picture_samll_name = $name_noext_pinyin . "_samll" . $name_ext; //小规格图名称 // 生成图的名称(大,中,小图)[E] $hotel_id = Yii::app()->request->getParam('hotel_id'); // 酒店ID $hotel_count = Hotel::model()->count('ID=:id', array(':id' => $hotel_id)); if ($hotel_count > 0) { // 判断该酒店是否存在 // 创建图片路径 $pic_dir_path = IMAGES_PATH . 'hotel' . '/' . date("Y-m") . '/'; DirectoryFile::dirCreate($pic_dir_path); // 大、中、小图上传 $pic_big_src = $pic_dir_path . $name_noext_pinyin . $name_ext; $pic_middle_src = $pic_dir_path . $picture_middle_name; $pic_small_src = $pic_dir_path . $picture_samll_name; // 上传原始图片并生产规定格式的中图和小图 file_put_contents($pic_big_src, file_get_contents('php://input')); // 存放大图 ImageChange::imgResize($pic_big_src, $pic_middle_src, 380, 210); // 生成中图 ImageChange::imgResize($pic_big_src, $pic_small_src, 180, 120); // 生成小图 // 图片信息存入数据库 if (empty($picture_title)) { $picture_title = $name_noext; } // 图片路径保存到数据库 $HotelPicture['big'] = new HotelPicture(); $HotelPicture['big']->PicSrc = $pic_big_src; $HotelPicture['big']->PicTitle = $picture_title; $HotelPicture['big']->PicDes = $picture_desc; $HotelPicture['big']->Hotel_Id = $hotel_id; $HotelPicture['big']->SizeType = 0; $HotelPicture['middle'] = new HotelPicture(); $HotelPicture['middle']->PicSrc = $pic_middle_src; $HotelPicture['middle']->PicTitle = $picture_title; $HotelPicture['middle']->PicDes = $picture_desc; $HotelPicture['middle']->Hotel_Id = $hotel_id; $HotelPicture['middle']->SizeType = 1; $HotelPicture['small'] = new HotelPicture(); $HotelPicture['small']->PicSrc = $pic_small_src; $HotelPicture['small']->PicTitle = $picture_title; $HotelPicture['small']->PicDes = $picture_desc; $HotelPicture['small']->Hotel_Id = $hotel_id; $HotelPicture['small']->SizeType = 2; //事务[S] $transaction = Yii::app()->db->beginTransaction(); //开启事务 try { foreach ($HotelPicture as $pic_src) { if (!$pic_src->save()) { throw new Exception(); } } $transaction->commit(); //提交事务 } catch (Exception $e) { $transaction->rollBack(); //回滚事务 } //事务[E] echo $pic_dir_path; } } }