OssClientImageTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace OSS\Tests;
  3. require_once __DIR__ . '/Common.php';
  4. require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
  5. use OSS\OssClient;
  6. class OssClientImageTest extends TestOssClientBase
  7. {
  8. private $bucketName;
  9. private $client;
  10. private $local_file;
  11. private $object;
  12. private $download_file;
  13. protected function setUp(): void
  14. {
  15. parent::setUp();
  16. $this->client = $this->ossClient;
  17. $this->bucketName = $this->bucket;
  18. $this->local_file = "example.jpg";
  19. $this->object = "oss-example.jpg";
  20. $this->download_file = "image.jpg";
  21. Common::waitMetaSync();
  22. $this->client->uploadFile($this->bucketName, $this->object, $this->local_file);
  23. }
  24. protected function tearDown(): void
  25. {
  26. parent::tearDown();
  27. unlink($this->download_file);
  28. }
  29. public function testImageResize()
  30. {
  31. $options = array(
  32. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  33. OssClient::OSS_PROCESS => "image/resize,m_fixed,h_100,w_100",);
  34. $this->check($options, 100, 100, 3267, 'jpg');
  35. }
  36. public function testImageCrop()
  37. {
  38. $options = array(
  39. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  40. OssClient::OSS_PROCESS => "image/crop,w_100,h_100,x_100,y_100,r_1",);
  41. $this->check($options, 100, 100, 1969, 'jpg');
  42. }
  43. public function testImageRotate()
  44. {
  45. $options = array(
  46. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  47. OssClient::OSS_PROCESS => "image/rotate,90",);
  48. $this->check($options, 267, 400, 20998, 'jpg');
  49. }
  50. public function testImageSharpen()
  51. {
  52. $options = array(
  53. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  54. OssClient::OSS_PROCESS => "image/sharpen,100",);
  55. $this->check($options, 400, 267, 23015, 'jpg');
  56. }
  57. public function testImageWatermark()
  58. {
  59. $options = array(
  60. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  61. OssClient::OSS_PROCESS => "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ",);
  62. $this->check($options, 400, 267, 26369, 'jpg');
  63. }
  64. public function testImageFormat()
  65. {
  66. $options = array(
  67. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  68. OssClient::OSS_PROCESS => "image/format,png",);
  69. $this->check($options, 400, 267, 160733, 'png');
  70. }
  71. public function testImageTofile()
  72. {
  73. $options = array(
  74. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  75. OssClient::OSS_PROCESS => "image/resize,m_fixed,w_100,h_100",);
  76. $this->check($options, 100, 100, 3267, 'jpg');
  77. }
  78. public function testProcesObject()
  79. {
  80. $object = 'process-object.jpg';
  81. $process = 'image/resize,m_fixed,w_100,h_100' .
  82. '|sys/saveas' .
  83. ',o_' . $this->base64url_encode($object) .
  84. ',b_' . $this->base64url_encode($this->bucketName);
  85. $result = $this->client->processObject($this->bucketName, $this->object, $process);
  86. $this->assertTrue(stripos($result, '"object": "process-object.jpg",') > 0);
  87. $this->assertTrue(stripos($result, '"status": "OK"') > 0);
  88. $options = array(
  89. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  90. );
  91. $this->client->getObject($this->bucketName, $object, $options);
  92. $array = getimagesize($this->download_file);
  93. $this->assertEquals(100, $array[0]);
  94. $this->assertEquals(100, $array[1]);
  95. $this->assertEquals(2, $array[2]);
  96. //without bucket
  97. $object = 'process-object-1.jpg';
  98. $process = 'image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ' .
  99. '|sys/saveas' .
  100. ',o_' . $this->base64url_encode($object);
  101. $result = $this->client->processObject($this->bucketName, $this->object, $process);
  102. $this->assertTrue(stripos($result, '"object": "process-object-1.jpg",') > 0);
  103. $this->assertTrue(stripos($result, '"status": "OK"') > 0);
  104. $options = array(
  105. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  106. );
  107. $this->client->getObject($this->bucketName, $object, $options);
  108. $array = getimagesize($this->download_file);
  109. $this->assertEquals(400, $array[0]);
  110. $this->assertEquals(267, $array[1]);
  111. $this->assertEquals(2, $array[2]);
  112. }
  113. private function check($options, $width, $height, $size, $type)
  114. {
  115. $this->client->getObject($this->bucketName, $this->object, $options);
  116. $array = getimagesize($this->download_file);
  117. $this->assertEquals($width, $array[0]);
  118. $this->assertEquals($height, $array[1]);
  119. $this->assertEquals($type === 'jpg' ? 2 : 3, $array[2]);//2 <=> jpg
  120. }
  121. private function base64url_encode($data)
  122. {
  123. return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  124. }
  125. }