SimilarimageModel.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. use ocr\AipImageSearch;
  6. use org\Qiniu;
  7. class SimilarimageModel extends Model{
  8. protected $name = 'ocrimage';
  9. // 开启自动写入时间戳字段
  10. protected $autoWriteTimestamp = true;
  11. //显示
  12. public function index($map, $Nowpage, $limits,$od)
  13. {
  14. return $this->alias ('o')
  15. //field('r.id,r.title,r.cate_id,r.photo,r.remark,r.keyword,r.content,r.views,r.type,r.is_tui,r.from,r.writer,r.ip,r.create_time,r.update_time,r.status,rc.name,r.music')
  16. //->join('article_cate rc', 'r.cate_id = rc.id')
  17. ->where($map)
  18. ->page($Nowpage, $limits)
  19. ->order($od)
  20. ->select();
  21. }
  22. //图片添加
  23. public function add($data)
  24. {
  25. $data['type'] = $data['tid'];
  26. $add=$this->allowField(true)->save($data);
  27. $oid=$this->oid;
  28. if($add){
  29. $APP_ID = config('site.appid');
  30. $API_KEY = config('site.appkey');
  31. $SRCRET_KEY = config('site.srcretkey');
  32. $client = new AipImageSearch($APP_ID, $API_KEY, $SRCRET_KEY);
  33. //$image=$data["product_image"];
  34. $image = file_get_contents(ROOT_PATH . 'public' . DS . $data["product_image"]);
  35. //$image = ROOT_PATH . 'public' . DS . $data["product_image"];
  36. //echo $dir;die;
  37. // 如果有可选参数
  38. $options = array();
  39. $arr=array("name"=>$data["pruduct_name"],"id"=>$oid);
  40. $options["brief"] =json_encode($arr);
  41. $options["tags"] = $data['tid'].','.$data['user_id'];
  42. // 带参数调用商品图检索—入库, 图片参数为本地图片
  43. $res=$client->similarAdd($image, $options);
  44. if(isset($res["error_code"])){
  45. $del = Db::name('ocrimage')->where('oid',$oid)->delete();
  46. return json(["code"=>100,"msg"=>"上传失败"]);
  47. }
  48. if($res){
  49. return json(["code"=>200,"msg"=>"上传成功"]);
  50. }else{
  51. $del = Db::name('ocrimage')->where('oid',$oid)->delete();
  52. return json(["code"=>100,"msg"=>"上传失败"]);
  53. }
  54. }
  55. else{
  56. return json(["code"=>100,"msg"=>"添加失败"]);
  57. }
  58. }
  59. //图库信息修改
  60. public function edit($data){
  61. $a = 500000;
  62. $b = 700000000;
  63. $c =$a/$b;
  64. echo $c;die;
  65. halt($data);
  66. $edit=$this->allowField(true)->save($data,["oid"=>$data['oid']]);
  67. if($edit){
  68. }else{
  69. return json(["code"=>100,"msg"=>"修改失败"]);
  70. }
  71. }
  72. //图库删除
  73. public function del($oid)
  74. {
  75. $data=$this->where("oid",$oid)->find();
  76. $del=$this->where("oid",$oid)->delete();
  77. $APP_ID = config('site.appid');
  78. $API_KEY = config('site.appkey');
  79. $SRCRET_KEY = config('site.srcretkey');
  80. $client = new AipImageSearch($APP_ID, $API_KEY, $SRCRET_KEY);
  81. $image = file_get_contents(ROOT_PATH . 'public' . DS . $data["product_image"]);
  82. // 调用删除相似图,传入参数为图片
  83. $res=$client->similarDeleteByImage($image);
  84. if($del&&$res){
  85. return json(["code"=>200,"msg"=>"删除成功"]);
  86. }else{
  87. return json(["code"=>100,"msg"=>"删除失败"]);
  88. }
  89. }
  90. }