private function saveRichMedia($type, $src_url, $thumb_url = "") { if (!$src_url) { return true; } $hash_url = md5($src_url); $has_in = RichMedia::findOne(['hash_url' => $hash_url]); if ($has_in) { return true; } $date_now = date("Y-m-d H:i:s"); $data = file_get_contents($src_url); if (!$data) { return false; } $file_type = "jpeg"; switch ($type) { case "voice": $file_type = "mp3"; break; case "video": $file_type = "mp4"; break; } $filename = md5($data) . ".{$file_type}"; $filepath = "/tmp/{$filename}"; file_put_contents($filepath, $data); $ret_upload = UploadService::uploadByFile($filename, $filepath, $hash_url); if (!$ret_upload) { return false; } $exif_info = @exif_read_data($ret_upload['path'], 0, true); $model_rich_media = new RichMedia(); $model_rich_media->type = $type; $model_rich_media->src_url = $ret_upload['uri']; $model_rich_media->hash_url = $hash_url; $model_rich_media->thumb_url = $thumb_url; $model_rich_media->status = 0; $model_rich_media->exif = json_encode($exif_info); $model_rich_media->updated_time = $date_now; $model_rich_media->created_time = $date_now; $model_rich_media->save(0); return true; }
public function actionScan() { $type = trim($this->post("type", "EAN_13")); $code = trim($this->post("isbn")); $date_now = date("Y-m-d H:i:s"); //$this->recode("test"); if (!$type || !$code) { return $this->renderJSON([], "请输入参数type 和 isbn", -1); } $book_info = Book::find()->where(['isbn' => $code])->one(); if ($book_info) { return $this->renderJSON(["name" => $book_info['subtitle'], "origin_image_url" => $book_info['origin_image_url'], "isbn" => $book_info['isbn']]); } $ret = $this->doubanApi($code, $type); if (!$ret) { return $this->render([], "无法从豆瓣获取数据信息", -1); } $ret_upload = UploadService::uploadByUrl($ret['origin_image_url']); $model_book = new Book(); $model_book->isbn = $ret['isbn']; $model_book->bartype = $ret['bartype']; $model_book->name = $ret['name']; $model_book->subtitle = $ret['subtitle']; $model_book->creator = json_encode($ret['creator']); $model_book->binding = $ret['binding']; $model_book->pages = $ret['pages']; $model_book->publish_date = date("Y-m-d", strtotime($ret['publish_date'])); $model_book->publishing_house = $ret['publishing_house']; $model_book->tags = $ret['tags']; $model_book->summary = $ret['summary']; $model_book->image_url = $ret_upload ? $ret_upload['uri'] : ''; $model_book->origin_image_url = $ret['origin_image_url']; $model_book->status = 0; $model_book->updated_time = $date_now; $model_book->created_time = $date_now; $model_book->save(0); return $this->renderJSON(["name" => $ret['subtitle'], "origin_image_url" => $ret['origin_image_url'], "isbn" => $code]); }
private function downImg($src_url) { $ret = UploadService::uploadByUrl($src_url); return $ret ? $ret['url'] : false; }
private function uploadMedia() { $allow_file_type = ["image/jpg", "image/gif", "image/bmp", "image/jpeg", "image/png", "video/mp4"]; //设置允许上传文件的类型 if (!isset($_FILES['richmedia'])) { return $this->renderJSON([], '请选择文件!', -1); } $file_target = $_FILES['richmedia']; if ($file_target['error'] > 0) { switch ($file_target['error']) { case 1: return $this->renderJSON([], '文件大小超过服务器限制', -1); break; case 2: return $this->renderJSON([], '文件太大!', -1); break; case 3: return $this->renderJSON([], '文件只加载了一部分!', -1); break; case 4: return $this->renderJSON([], '文件加载失败!', -1); break; } } if (!is_uploaded_file($file_target['tmp_name'])) { return $this->renderJSON([], "非法上传文件!", -1); } $type = $file_target["type"]; if (!in_array($type, $allow_file_type)) { return $this->renderJSON([], "只能上传图片!", -1); } $hash_url = md5(file_get_contents($file_target['tmp_name'])); $has_in = RichMedia::findOne(['hash_url' => $hash_url]); if ($has_in) { return $this->renderJSON([], "上传成功!"); } $ret_upload = UploadService::uploadByFile($file_target['name'], $file_target['tmp_name'], $hash_url); if (!$ret_upload) { return $this->renderJSON([], UploadService::getLastErrorMsg(), -1); } $exif_info = @exif_read_data($ret_upload['path'], 0, true); $exif_ifd0_info = isset($exif_info['IFD0']) ? $exif_info['IFD0'] : []; if ($exif_ifd0_info && isset($exif_ifd0_info['Orientation']) && $exif_ifd0_info['Orientation'] == 6) { $this->rotate($ret_upload['path']); } $date_now = date("Y-m-d H:i:s"); $gps = trim($this->post("gps", "")); $tiff = trim($this->post("tiff", "")); $db_type = "image"; if ($type == "video/mp4") { $db_type = "video"; } $model_rich_media = new RichMedia(); $model_rich_media->type = $db_type; $model_rich_media->src_url = $ret_upload['uri']; $model_rich_media->hash_url = $hash_url; $model_rich_media->thumb_url = ""; $model_rich_media->gps = $gps; $model_rich_media->tiff = $tiff; $model_rich_media->status = 0; $model_rich_media->exif = json_encode($exif_info); $model_rich_media->updated_time = $date_now; $model_rich_media->created_time = $date_now; $model_rich_media->save(0); return $this->renderJSON([], "上传成功!"); }
private function uploadUeditorImage() { $up_target = $_FILES["upfile"]; if ($up_target["error"] > 0) { return $this->retUeditor("上传失败!error:" . $up_target["error"]); } if (!is_uploaded_file($up_target['tmp_name'])) { return $this->retUeditor("非法上传文件!!"); } $type = $up_target["type"]; $filename = $up_target["name"]; if (!in_array($type, $this->allow_file_type)) { return $this->retUeditor("只能上传图片!!"); } $ret = UploadService::uploadByFile($filename, $up_target['tmp_name']); if (!$ret) { return $this->retUeditor(UploadService::getLastErrorMsg()); } if (isset($ret['code']) && $ret['code'] == 205) { return $this->retUeditor("此图片已经上传过了"); } $display_url = GlobalUrlService::buildPic1Static($ret['uri'], ['w' => 600]); //return $this->renderJSON([ 'url' => $display_url, 'filename' => $filename ]); return $this->retUeditor("SUCCESS", $display_url, $ret['hash_key'], $filename, $type, $up_target['size']); }