Image.php 2.9 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. //*******************************简单使用***************************************************************
  10. // 先把本地的example.jpg上传到指定$bucket, 命名为$object
  11. $ossClient->uploadFile($bucketName, $object, "example.jpg");
  12. // 图片缩放
  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. // 图片裁剪
  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. // 图片旋转
  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. // 图片锐化
  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. // 图片水印
  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. // 图片格式转换
  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. // 获取图片信息
  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. * 生成一个带签名的可用于浏览器直接打开的url, URL的有效期是3600秒
  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. //最后删除上传的$object
  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. }