OssClientImageTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace OSS\Tests;
  3. require_once __DIR__ . '/Common.php';
  4. use OSS\OssClient;
  5. class OssClinetImageTest extends \PHPUnit_Framework_TestCase
  6. {
  7. private $bucketName;
  8. private $client;
  9. private $local_file;
  10. private $object;
  11. private $download_file;
  12. public function setUp()
  13. {
  14. $this->client = Common::getOssClient();
  15. $this->bucketName = 'php-sdk-test-bucket-image-' . strval(rand(0, 10000));
  16. $this->client->createBucket($this->bucketName);
  17. Common::waitMetaSync();
  18. $this->local_file = "example.jpg";
  19. $this->object = "oss-example.jpg";
  20. $this->download_file = "image.jpg";
  21. $this->client->uploadFile($this->bucketName, $this->object, $this->local_file);
  22. }
  23. public function tearDown()
  24. {
  25. $this->client->deleteObject($this->bucketName, $this->object);
  26. $this->client->deleteBucket($this->bucketName);
  27. }
  28. public function testImageResize()
  29. {
  30. $options = array(
  31. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  32. OssClient::OSS_PROCESS => "image/resize,m_fixed,h_100,w_100", );
  33. $this->check($options, 100, 100, 3267, 'jpg');
  34. }
  35. public function testImageCrop()
  36. {
  37. $options = array(
  38. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  39. OssClient::OSS_PROCESS => "image/crop,w_100,h_100,x_100,y_100,r_1", );
  40. $this->check($options, 100, 100, 1969, 'jpg');
  41. }
  42. public function testImageRotate()
  43. {
  44. $options = array(
  45. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  46. OssClient::OSS_PROCESS => "image/rotate,90", );
  47. $this->check($options, 267, 400, 20998, 'jpg');
  48. }
  49. public function testImageSharpen()
  50. {
  51. $options = array(
  52. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  53. OssClient::OSS_PROCESS => "image/sharpen,100", );
  54. $this->check($options, 400, 267, 23015, 'jpg');
  55. }
  56. public function testImageWatermark()
  57. {
  58. $options = array(
  59. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  60. OssClient::OSS_PROCESS => "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ", );
  61. $this->check($options, 400, 267, 26369, 'jpg');
  62. }
  63. public function testImageFormat()
  64. {
  65. $options = array(
  66. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  67. OssClient::OSS_PROCESS => "image/format,png", );
  68. $this->check($options, 400, 267, 160733, 'png');
  69. }
  70. public function testImageTofile()
  71. {
  72. $options = array(
  73. OssClient::OSS_FILE_DOWNLOAD => $this->download_file,
  74. OssClient::OSS_PROCESS => "image/resize,m_fixed,w_100,h_100", );
  75. $this->check($options, 100, 100, 3267, 'jpg');
  76. }
  77. private function check($options, $width, $height, $size, $type)
  78. {
  79. $this->client->getObject($this->bucketName, $this->object, $options);
  80. $array = getimagesize($this->download_file);
  81. $this->assertEquals($width, $array[0]);
  82. $this->assertEquals($height, $array[1]);
  83. $this->assertEquals($type === 'jpg' ? 2 : 3, $array[2]);//2 <=> jpg
  84. }
  85. }