AipImageCensor.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /*
  3. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy of
  7. * the License at
  8. *
  9. * Http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. require_once 'lib/AipBase.php';
  18. /**
  19. * 黄反识别
  20. */
  21. class AipImageCensor extends AipBase{
  22. /**
  23. * antiporn api url
  24. * @var string
  25. */
  26. private $antiPornUrl = 'https://aip.baidubce.com/rest/2.0/antiporn/v1/detect';
  27. /**
  28. * antiporn gif api url
  29. * @var string
  30. */
  31. private $antiPornGifUrl = 'https://aip.baidubce.com/rest/2.0/antiporn/v1/detect_gif';
  32. /**
  33. * antiterror api url
  34. * @var string
  35. */
  36. private $antiTerrorUrl = 'https://aip.baidubce.com/rest/2.0/antiterror/v1/detect';
  37. /**
  38. * @var string
  39. */
  40. private $faceAuditUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/face_audit';
  41. /**
  42. * @var string
  43. */
  44. private $imageCensorCombUrl = 'https://aip.baidubce.com/api/v1/solution/direct/img_censor';
  45. /**
  46. * @var string
  47. */
  48. private $imageCensorUserDefinedUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined';
  49. /**
  50. * @var string
  51. */
  52. private $antiSpamUrl = 'https://aip.baidubce.com/rest/2.0/antispam/v2/spam';
  53. /**
  54. * @var string
  55. */
  56. private $textCensorUserDefinedUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined';
  57. /**
  58. * @param string $image 图像读取
  59. * @return array
  60. */
  61. public function antiPorn($image){
  62. $data = array();
  63. $data['image'] = base64_encode($image);
  64. return $this->request($this->antiPornUrl, $data);
  65. }
  66. /**
  67. * @param string $image 图像读取
  68. * @return array
  69. */
  70. public function multi_antiporn($images){
  71. $data = array();
  72. foreach($images as $image){
  73. $data[] = array(
  74. 'image' => base64_encode($image),
  75. );
  76. }
  77. return $this->multi_request($this->antiPornUrl, $data);
  78. }
  79. /**
  80. * @param string $image 图像读取
  81. * @return array
  82. */
  83. public function antiPornGif($image){
  84. $data = array();
  85. $data['image'] = base64_encode($image);
  86. return $this->request($this->antiPornGifUrl, $data);
  87. }
  88. /**
  89. * @param string $image 图像读取
  90. * @return array
  91. */
  92. public function antiTerror($image){
  93. $data = array();
  94. $data['image'] = base64_encode($image);
  95. return $this->request($this->antiTerrorUrl, $data);
  96. }
  97. /**
  98. * @param string $images 图像读取
  99. * @return array
  100. */
  101. public function faceAudit($images, $configId=''){
  102. // 非数组则处理为数组
  103. if(!is_array($images)){
  104. $images = array(
  105. $images,
  106. );
  107. }
  108. $data = array(
  109. 'configId' => $configId,
  110. );
  111. $isUrl = substr(trim($images[0]), 0, 4) === 'http';
  112. if(!$isUrl){
  113. $arr = array();
  114. foreach($images as $image){
  115. $arr[] = base64_encode($image);
  116. }
  117. $data['images'] = implode(',', $arr);
  118. }else{
  119. $urls = array();
  120. foreach($images as $url){
  121. $urls[] = urlencode($url);
  122. }
  123. $data['imgUrls'] = implode(',', $urls);
  124. }
  125. return $this->request($this->faceAuditUrl, $data);
  126. }
  127. /**
  128. * @param string $image 图像读取
  129. * @return array
  130. */
  131. public function imageCensorComb($image, $scenes='antiporn', $options=array()){
  132. $scenes = !is_array($scenes) ? explode(',', $scenes) : $scenes;
  133. $data = array(
  134. 'scenes' => $scenes,
  135. );
  136. $isUrl = substr(trim($image), 0, 4) === 'http';
  137. if(!$isUrl){
  138. $data['image'] = base64_encode($image);
  139. }else{
  140. $data['imgUrl'] = $image;
  141. }
  142. $data = array_merge($data, $options);
  143. return $this->request($this->imageCensorCombUrl, json_encode($data), array(
  144. 'Content-Type' => 'application/json',
  145. ));
  146. }
  147. /**
  148. * @param string $image 图像
  149. * @return array
  150. */
  151. public function imageCensorUserDefined($image){
  152. $data = array();
  153. $isUrl = substr(trim($image), 0, 4) === 'http';
  154. if(!$isUrl){
  155. $data['image'] = base64_encode($image);
  156. }else{
  157. $data['imgUrl'] = $image;
  158. }
  159. return $this->request($this->imageCensorUserDefinedUrl, $data);
  160. }
  161. /**
  162. * @param string $text
  163. * @return array
  164. */
  165. public function textCensorUserDefined($text){
  166. $data = array();
  167. $data['text'] = $text;
  168. return $this->request($this->textCensorUserDefinedUrl, $data);
  169. }
  170. /**
  171. * @param string $content
  172. * @return array
  173. */
  174. public function antiSpam($content, $options=array()){
  175. $data = array();
  176. $data['content'] = $content;
  177. $data = array_merge($data, $options);
  178. return $this->request($this->antiSpamUrl, $data);
  179. }
  180. }