ImageUrlBuilderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Qiniu\Tests;
  3. /**
  4. * imageprocess test
  5. *
  6. * @package Qiniu
  7. * @subpackage test
  8. * @author Sherlock Ren <sherlock_ren@icloud.com>
  9. */
  10. class ImageUrlBuilderTest extends \PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * 缩略图测试
  14. *
  15. * @test
  16. * @return void
  17. * @author Sherlock Ren <sherlock_ren@icloud.com>
  18. */
  19. public function testThumbutl()
  20. {
  21. $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder();
  22. $url = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg';
  23. $url2 = $url . '?watermark/1/gravity/SouthEast/dx/0/dy/0/image/'
  24. . 'aHR0cDovL2Fkcy1jZG4uY2h1Y2h1amllLmNvbS9Ga1R6bnpIY2RLdmRBUFc5cHZZZ3pTc21UY0tB';
  25. // 异常测试
  26. $this->assertEquals($url, $imageUrlBuilder->thumbnail($url, 1, 0, 0));
  27. $this->assertEquals($url, \Qiniu\thumbnail($url, 1, 0, 0));
  28. // 简单缩略测试
  29. $this->assertEquals(
  30. $url . '?imageView2/1/w/200/h/200/ignore-error/1/',
  31. $imageUrlBuilder->thumbnail($url, 1, 200, 200)
  32. );
  33. $this->assertEquals(
  34. $url . '?imageView2/1/w/200/h/200/ignore-error/1/',
  35. \Qiniu\thumbnail($url, 1, 200, 200)
  36. );
  37. // 输出格式测试
  38. $this->assertEquals(
  39. $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/',
  40. $imageUrlBuilder->thumbnail($url, 1, 200, 200, 'png')
  41. );
  42. $this->assertEquals(
  43. $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/',
  44. \Qiniu\thumbnail($url, 1, 200, 200, 'png')
  45. );
  46. // 渐进显示测试
  47. $this->assertEquals(
  48. $url . '?imageView2/1/w/200/h/200/format/png/interlace/1/ignore-error/1/',
  49. $imageUrlBuilder->thumbnail($url, 1, 200, 200, 'png', 1)
  50. );
  51. $this->assertEquals(
  52. $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/',
  53. \Qiniu\thumbnail($url, 1, 200, 200, 'png', 2)
  54. );
  55. // 图片质量测试
  56. $this->assertEquals(
  57. $url . '?imageView2/1/w/200/h/200/format/png/interlace/1/q/80/ignore-error/1/',
  58. $imageUrlBuilder->thumbnail($url, 1, 200, 200, 'png', 1, 80)
  59. );
  60. $this->assertEquals(
  61. $url . '?imageView2/1/w/200/h/200/format/png/interlace/1/ignore-error/1/',
  62. \Qiniu\thumbnail($url, 1, 200, 200, 'png', 1, 101)
  63. );
  64. // 多参数测试
  65. $this->assertEquals(
  66. $url2 . '|imageView2/1/w/200/h/200/ignore-error/1/',
  67. $imageUrlBuilder->thumbnail($url2, 1, 200, 200)
  68. );
  69. $this->assertEquals(
  70. $url2 . '|imageView2/1/w/200/h/200/ignore-error/1/',
  71. \Qiniu\thumbnail($url2, 1, 200, 200)
  72. );
  73. }
  74. /**
  75. * 图片水印测试
  76. *
  77. * @test
  78. * @param void
  79. * @return void
  80. * @author Sherlock Ren <sherlock_ren@icloud.com>
  81. */
  82. public function waterImgTest()
  83. {
  84. $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder();
  85. $url = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg';
  86. $url2 = $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/';
  87. $image = 'http://developer.qiniu.com/resource/logo-2.jpg';
  88. // 水印简单测试
  89. $this->assertEquals(
  90. $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  91. . '/dissolve/100/gravity/SouthEast/',
  92. $imageUrlBuilder->waterImg($url, $image)
  93. );
  94. $this->assertEquals(
  95. $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  96. . '/gravity/SouthEast/',
  97. $imageUrlBuilder->waterImg($url, $image, 101)
  98. );
  99. $this->assertEquals(
  100. $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw==/',
  101. $imageUrlBuilder->waterImg($url, $image, 101, 'sdfsd')
  102. );
  103. $this->assertEquals(
  104. $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  105. . '/dissolve/100/gravity/SouthEast/',
  106. \Qiniu\waterImg($url, $image)
  107. );
  108. // 横轴边距测试
  109. $this->assertEquals(
  110. $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  111. . '/dissolve/100/gravity/SouthEast/dx/10/',
  112. $imageUrlBuilder->waterImg($url, $image, 100, 'SouthEast', 10)
  113. );
  114. $this->assertEquals(
  115. $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  116. . '/dissolve/100/gravity/SouthEast/',
  117. \Qiniu\waterImg($url, $image, 100, 'SouthEast', 'sad')
  118. );
  119. // 纵轴边距测试
  120. $this->assertEquals(
  121. $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  122. . '/dissolve/100/gravity/SouthEast/dx/10/dy/10/',
  123. $imageUrlBuilder->waterImg($url, $image, 100, 'SouthEast', 10, 10)
  124. );
  125. $this->assertEquals(
  126. $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  127. . '/dissolve/100/gravity/SouthEast/',
  128. \Qiniu\waterImg($url, $image, 100, 'SouthEast', 'sad', 'asdf')
  129. );
  130. // 自适应原图的短边比例测试
  131. $this->assertEquals(
  132. $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  133. . '/dissolve/100/gravity/SouthEast/dx/10/dy/10/ws/0.5/',
  134. $imageUrlBuilder->waterImg($url, $image, 100, 'SouthEast', 10, 10, 0.5)
  135. );
  136. $this->assertEquals(
  137. $url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  138. . '/dissolve/100/gravity/SouthEast/',
  139. \Qiniu\waterImg($url, $image, 100, 'SouthEast', 'sad', 'asdf', 2)
  140. );
  141. // 多参数测试
  142. $this->assertEquals(
  143. $url2 . '|watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  144. . '/dissolve/100/gravity/SouthEast/',
  145. $imageUrlBuilder->waterImg($url2, $image)
  146. );
  147. $this->assertEquals(
  148. $url2 . '|watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
  149. . '/dissolve/100/gravity/SouthEast/',
  150. \Qiniu\waterImg($url2, $image)
  151. );
  152. }
  153. /**
  154. * 文字水印测试
  155. *
  156. * @test
  157. * @param void
  158. * @return void
  159. * @author Sherlock Ren <sherlock_ren@icloud.com>
  160. */
  161. public function waterTextTest()
  162. {
  163. $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder();
  164. $url = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg';
  165. $url2 = $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/';
  166. $text = '测试一下';
  167. $font = '微软雅黑';
  168. $fontColor = '#FF0000';
  169. // 水印简单测试
  170. $this->assertEquals($url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
  171. . 'fontsize/500/dissolve/100/gravity/SouthEast/', $imageUrlBuilder->waterText($url, $text, $font, 500));
  172. $this->assertEquals(
  173. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
  174. . 'dissolve/100/gravity/SouthEast/',
  175. \Qiniu\waterText($url, $text, $font, 'sdf')
  176. );
  177. // 字体颜色测试
  178. $this->assertEquals(
  179. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/'
  180. . 'I0ZGMDAwMA==/dissolve/100/gravity/SouthEast/',
  181. $imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor)
  182. );
  183. $this->assertEquals(
  184. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA=='
  185. . '/dissolve/100/gravity/SouthEast/',
  186. \Qiniu\waterText($url, $text, $font, 'sdf', $fontColor)
  187. );
  188. // 透明度测试
  189. $this->assertEquals(
  190. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
  191. . '/dissolve/80/gravity/SouthEast/',
  192. $imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80)
  193. );
  194. $this->assertEquals(
  195. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA=='
  196. . '/gravity/SouthEast/',
  197. \Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101)
  198. );
  199. // 水印位置测试
  200. $this->assertEquals(
  201. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
  202. . '/dissolve/80/gravity/East/',
  203. $imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80, 'East')
  204. );
  205. $this->assertEquals(
  206. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA==/',
  207. \Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101, 'sdfsdf')
  208. );
  209. // 横轴距离测试
  210. $this->assertEquals(
  211. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
  212. . '/dissolve/80/gravity/East/dx/10/',
  213. $imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80, 'East', 10)
  214. );
  215. $this->assertEquals(
  216. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA==/',
  217. \Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101, 'sdfsdf', 'sdfs')
  218. );
  219. // 纵轴距离测试
  220. $this->assertEquals(
  221. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
  222. . '/dissolve/80/gravity/East/dx/10/dy/10/',
  223. $imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80, 'East', 10, 10)
  224. );
  225. $this->assertEquals(
  226. $url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA==/',
  227. \Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101, 'sdfsdf', 'sdfs', 'ssdf')
  228. );
  229. // 多参数测试
  230. $this->assertEquals(
  231. $url2 . '|watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
  232. . 'fontsize/500/dissolve/100/gravity/SouthEast/',
  233. $imageUrlBuilder->waterText($url2, $text, $font, 500)
  234. );
  235. $this->assertEquals(
  236. $url2 . '|watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
  237. . 'fontsize/500/dissolve/100/gravity/SouthEast/',
  238. \Qiniu\waterText($url2, $text, $font, 500)
  239. );
  240. }
  241. }