Image.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. $bucketName = Common::getBucketName();
  5. $object = "example.jpg";
  6. $ossClient = Common::getOssClient();
  7. $download_file = "download.jpg";
  8. if (is_null($ossClient)) exit(1);
  9. //******************************* Simple Usage ***************************************************************
  10. // Upload example.jpg to the specified bucket and rename it to $object.
  11. $ossClient->uploadFile($bucketName, $object, "example.jpg");
  12. // Image resize
  13. $options = array(
  14. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  15. OssClient::OSS_PROCESS => "image/resize,m_fixed,h_100,w_100", );
  16. $ossClient->getObject($bucketName, $object, $options);
  17. printImage("imageResize",$download_file);
  18. // Image crop
  19. $options = array(
  20. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  21. OssClient::OSS_PROCESS => "image/crop,w_100,h_100,x_100,y_100,r_1", );
  22. $ossClient->getObject($bucketName, $object, $options);
  23. printImage("iamgeCrop", $download_file);
  24. // Image rotate
  25. $options = array(
  26. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  27. OssClient::OSS_PROCESS => "image/rotate,90", );
  28. $ossClient->getObject($bucketName, $object, $options);
  29. printImage("imageRotate", $download_file);
  30. // Image sharpen
  31. $options = array(
  32. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  33. OssClient::OSS_PROCESS => "image/sharpen,100", );
  34. $ossClient->getObject($bucketName, $object, $options);
  35. printImage("imageSharpen", $download_file);
  36. // Add watermark into a image
  37. $options = array(
  38. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  39. OssClient::OSS_PROCESS => "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ", );
  40. $ossClient->getObject($bucketName, $object, $options);
  41. printImage("imageWatermark", $download_file);
  42. // Image format convertion
  43. $options = array(
  44. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  45. OssClient::OSS_PROCESS => "image/format,png", );
  46. $ossClient->getObject($bucketName, $object, $options);
  47. printImage("imageFormat", $download_file);
  48. // Get image information
  49. $options = array(
  50. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  51. OssClient::OSS_PROCESS => "image/info", );
  52. $ossClient->getObject($bucketName, $object, $options);
  53. printImage("imageInfo", $download_file);
  54. /**
  55. * Generate a signed url which could be used in browser to access the object. The expiration time is 1 hour.
  56. */
  57. $timeout = 3600;
  58. $options = array(
  59. OssClient::OSS_PROCESS => "image/resize,m_lfit,h_100,w_100",
  60. );
  61. $signedUrl = $ossClient->signUrl($bucketName, $object, $timeout, "GET", $options);
  62. Common::println("rtmp url: \n" . $signedUrl);
  63. // Finally delete the $object uploaded.
  64. $ossClient->deleteObject($bucketName, $object);
  65. function printImage($func, $imageFile)
  66. {
  67. $array = getimagesize($imageFile);
  68. Common::println("$func, image width: " . $array[0]);
  69. Common::println("$func, image height: " . $array[1]);
  70. Common::println("$func, image type: " . ($array[2] === 2 ? 'jpg' : 'png'));
  71. Common::println("$func, image size: " . ceil(filesize($imageFile)));
  72. }