image.js 5.0 KB

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