AipImageCensor.php 5.1 KB

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