index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { request, urlSafeBase64Encode } from '../utils';
  2. function getImageUrl(key, domain) {
  3. key = encodeURIComponent(key);
  4. if (domain.slice(domain.length - 1) !== '/') {
  5. domain += '/';
  6. }
  7. return domain + key;
  8. }
  9. export function imageView2(op, key, domain) {
  10. if (!/^\d$/.test(String(op.mode))) {
  11. throw 'mode should be number in imageView2';
  12. }
  13. var mode = op.mode, w = op.w, h = op.h, q = op.q, format = op.format;
  14. if (!w && !h) {
  15. throw 'param w and h is empty in imageView2';
  16. }
  17. var imageUrl = 'imageView2/' + encodeURIComponent(mode);
  18. imageUrl += w ? '/w/' + encodeURIComponent(w) : '';
  19. imageUrl += h ? '/h/' + encodeURIComponent(h) : '';
  20. imageUrl += q ? '/q/' + encodeURIComponent(q) : '';
  21. imageUrl += format ? '/format/' + encodeURIComponent(format) : '';
  22. if (key && domain) {
  23. imageUrl = getImageUrl(key, domain) + '?' + imageUrl;
  24. }
  25. return imageUrl;
  26. }
  27. // invoke the imageMogr2 api of Qiniu
  28. export function imageMogr2(op, key, domain) {
  29. var autoOrient = op['auto-orient'];
  30. var thumbnail = op.thumbnail, strip = op.strip, gravity = op.gravity, crop = op.crop, quality = op.quality, rotate = op.rotate, format = op.format, blur = op.blur;
  31. var imageUrl = 'imageMogr2';
  32. imageUrl += autoOrient ? '/auto-orient' : '';
  33. imageUrl += thumbnail ? '/thumbnail/' + encodeURIComponent(thumbnail) : '';
  34. imageUrl += strip ? '/strip' : '';
  35. imageUrl += gravity ? '/gravity/' + encodeURIComponent(gravity) : '';
  36. imageUrl += quality ? '/quality/' + encodeURIComponent(quality) : '';
  37. imageUrl += crop ? '/crop/' + encodeURIComponent(crop) : '';
  38. imageUrl += rotate ? '/rotate/' + encodeURIComponent(rotate) : '';
  39. imageUrl += format ? '/format/' + encodeURIComponent(format) : '';
  40. imageUrl += blur ? '/blur/' + encodeURIComponent(blur) : '';
  41. if (key && domain) {
  42. imageUrl = getImageUrl(key, domain) + '?' + imageUrl;
  43. }
  44. return imageUrl;
  45. }
  46. // invoke the watermark api of Qiniu
  47. export function watermark(op, key, domain) {
  48. var mode = op.mode;
  49. if (!mode) {
  50. throw "mode can't be empty in watermark";
  51. }
  52. var imageUrl = 'watermark/' + mode;
  53. if (mode !== 1 && mode !== 2) {
  54. throw 'mode is wrong';
  55. }
  56. if (mode === 1) {
  57. var image = op.image;
  58. if (!image) {
  59. throw "image can't be empty in watermark";
  60. }
  61. imageUrl += image ? '/image/' + urlSafeBase64Encode(image) : '';
  62. }
  63. if (mode === 2) {
  64. var text = op.text, font = op.font, fontsize = op.fontsize, fill = op.fill;
  65. if (!text) {
  66. throw "text can't be empty in watermark";
  67. }
  68. imageUrl += text ? '/text/' + urlSafeBase64Encode(text) : '';
  69. imageUrl += font ? '/font/' + urlSafeBase64Encode(font) : '';
  70. imageUrl += fontsize ? '/fontsize/' + fontsize : '';
  71. imageUrl += fill ? '/fill/' + urlSafeBase64Encode(fill) : '';
  72. }
  73. var dissolve = op.dissolve, gravity = op.gravity, dx = op.dx, dy = op.dy;
  74. imageUrl += dissolve ? '/dissolve/' + encodeURIComponent(dissolve) : '';
  75. imageUrl += gravity ? '/gravity/' + encodeURIComponent(gravity) : '';
  76. imageUrl += dx ? '/dx/' + encodeURIComponent(dx) : '';
  77. imageUrl += dy ? '/dy/' + encodeURIComponent(dy) : '';
  78. if (key && domain) {
  79. imageUrl = getImageUrl(key, domain) + '?' + imageUrl;
  80. }
  81. return imageUrl;
  82. }
  83. // invoke the imageInfo api of Qiniu
  84. export function imageInfo(key, domain) {
  85. var url = getImageUrl(key, domain) + '?imageInfo';
  86. return request(url, { method: 'GET' });
  87. }
  88. // invoke the exif api of Qiniu
  89. export function exif(key, domain) {
  90. var url = getImageUrl(key, domain) + '?exif';
  91. return request(url, { method: 'GET' });
  92. }
  93. export function pipeline(arr, key, domain) {
  94. var isArray = Object.prototype.toString.call(arr) === '[object Array]';
  95. var option;
  96. var errOp = false;
  97. var imageUrl = '';
  98. if (isArray) {
  99. for (var i = 0, len = arr.length; i < len; i++) {
  100. option = arr[i];
  101. if (!option.fop) {
  102. throw "fop can't be empty in pipeline";
  103. }
  104. switch (option.fop) {
  105. case 'watermark':
  106. imageUrl += watermark(option) + '|';
  107. break;
  108. case 'imageView2':
  109. imageUrl += imageView2(option) + '|';
  110. break;
  111. case 'imageMogr2':
  112. imageUrl += imageMogr2(option) + '|';
  113. break;
  114. default:
  115. errOp = true;
  116. break;
  117. }
  118. if (errOp) {
  119. throw 'fop is wrong in pipeline';
  120. }
  121. }
  122. if (key && domain) {
  123. imageUrl = getImageUrl(key, domain) + '?' + imageUrl;
  124. var length_1 = imageUrl.length;
  125. if (imageUrl.slice(length_1 - 1) === '|') {
  126. imageUrl = imageUrl.slice(0, length_1 - 1);
  127. }
  128. }
  129. return imageUrl;
  130. }
  131. throw "pipeline's first param should be array";
  132. }
  133. //# sourceMappingURL=index.js.map