image.js 5.1 KB

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