Le repo des sources pour le site web des JM2L
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

316 строки
12 KiB

  1. /*
  2. * jQuery File Upload Image Preview & Resize Plugin 1.7.2
  3. * https://github.com/blueimp/jQuery-File-Upload
  4. *
  5. * Copyright 2013, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * http://www.opensource.org/licenses/MIT
  10. */
  11. /* jshint nomen:false */
  12. /* global define, window, Blob */
  13. (function (factory) {
  14. 'use strict';
  15. if (typeof define === 'function' && define.amd) {
  16. // Register as an anonymous AMD module:
  17. define([
  18. 'jquery',
  19. 'load-image',
  20. 'load-image-meta',
  21. 'load-image-exif',
  22. 'load-image-ios',
  23. 'canvas-to-blob',
  24. './jquery.fileupload-process'
  25. ], factory);
  26. } else {
  27. // Browser globals:
  28. factory(
  29. window.jQuery,
  30. window.loadImage
  31. );
  32. }
  33. }(function ($, loadImage) {
  34. 'use strict';
  35. // Prepend to the default processQueue:
  36. $.blueimp.fileupload.prototype.options.processQueue.unshift(
  37. {
  38. action: 'loadImageMetaData',
  39. disableImageHead: '@',
  40. disableExif: '@',
  41. disableExifThumbnail: '@',
  42. disableExifSub: '@',
  43. disableExifGps: '@',
  44. disabled: '@disableImageMetaDataLoad'
  45. },
  46. {
  47. action: 'loadImage',
  48. // Use the action as prefix for the "@" options:
  49. prefix: true,
  50. fileTypes: '@',
  51. maxFileSize: '@',
  52. noRevoke: '@',
  53. disabled: '@disableImageLoad'
  54. },
  55. {
  56. action: 'resizeImage',
  57. // Use "image" as prefix for the "@" options:
  58. prefix: 'image',
  59. maxWidth: '@',
  60. maxHeight: '@',
  61. minWidth: '@',
  62. minHeight: '@',
  63. crop: '@',
  64. orientation: '@',
  65. forceResize: '@',
  66. disabled: '@disableImageResize'
  67. },
  68. {
  69. action: 'saveImage',
  70. quality: '@imageQuality',
  71. type: '@imageType',
  72. disabled: '@disableImageResize'
  73. },
  74. {
  75. action: 'saveImageMetaData',
  76. disabled: '@disableImageMetaDataSave'
  77. },
  78. {
  79. action: 'resizeImage',
  80. // Use "preview" as prefix for the "@" options:
  81. prefix: 'preview',
  82. maxWidth: '@',
  83. maxHeight: '@',
  84. minWidth: '@',
  85. minHeight: '@',
  86. crop: '@',
  87. orientation: '@',
  88. thumbnail: '@',
  89. canvas: '@',
  90. disabled: '@disableImagePreview'
  91. },
  92. {
  93. action: 'setImage',
  94. name: '@imagePreviewName',
  95. disabled: '@disableImagePreview'
  96. },
  97. {
  98. action: 'deleteImageReferences',
  99. disabled: '@disableImageReferencesDeletion'
  100. }
  101. );
  102. // The File Upload Resize plugin extends the fileupload widget
  103. // with image resize functionality:
  104. $.widget('blueimp.fileupload', $.blueimp.fileupload, {
  105. options: {
  106. // The regular expression for the types of images to load:
  107. // matched against the file type:
  108. loadImageFileTypes: /^image\/(gif|jpeg|png|svg\+xml)$/,
  109. // The maximum file size of images to load:
  110. loadImageMaxFileSize: 10000000, // 10MB
  111. // The maximum width of resized images:
  112. imageMaxWidth: 1920,
  113. // The maximum height of resized images:
  114. imageMaxHeight: 1080,
  115. // Defines the image orientation (1-8) or takes the orientation
  116. // value from Exif data if set to true:
  117. imageOrientation: false,
  118. // Define if resized images should be cropped or only scaled:
  119. imageCrop: false,
  120. // Disable the resize image functionality by default:
  121. disableImageResize: true,
  122. // The maximum width of the preview images:
  123. previewMaxWidth: 80,
  124. // The maximum height of the preview images:
  125. previewMaxHeight: 80,
  126. // Defines the preview orientation (1-8) or takes the orientation
  127. // value from Exif data if set to true:
  128. previewOrientation: true,
  129. // Create the preview using the Exif data thumbnail:
  130. previewThumbnail: true,
  131. // Define if preview images should be cropped or only scaled:
  132. previewCrop: false,
  133. // Define if preview images should be resized as canvas elements:
  134. previewCanvas: true
  135. },
  136. processActions: {
  137. // Loads the image given via data.files and data.index
  138. // as img element, if the browser supports the File API.
  139. // Accepts the options fileTypes (regular expression)
  140. // and maxFileSize (integer) to limit the files to load:
  141. loadImage: function (data, options) {
  142. if (options.disabled) {
  143. return data;
  144. }
  145. var that = this,
  146. file = data.files[data.index],
  147. dfd = $.Deferred();
  148. if (($.type(options.maxFileSize) === 'number' &&
  149. file.size > options.maxFileSize) ||
  150. (options.fileTypes &&
  151. !options.fileTypes.test(file.type)) ||
  152. !loadImage(
  153. file,
  154. function (img) {
  155. if (img.src) {
  156. data.img = img;
  157. }
  158. dfd.resolveWith(that, [data]);
  159. },
  160. options
  161. )) {
  162. return data;
  163. }
  164. return dfd.promise();
  165. },
  166. // Resizes the image given as data.canvas or data.img
  167. // and updates data.canvas or data.img with the resized image.
  168. // Also stores the resized image as preview property.
  169. // Accepts the options maxWidth, maxHeight, minWidth,
  170. // minHeight, canvas and crop:
  171. resizeImage: function (data, options) {
  172. if (options.disabled || !(data.canvas || data.img)) {
  173. return data;
  174. }
  175. options = $.extend({canvas: true}, options);
  176. var that = this,
  177. dfd = $.Deferred(),
  178. img = (options.canvas && data.canvas) || data.img,
  179. resolve = function (newImg) {
  180. if (newImg && (newImg.width !== img.width ||
  181. newImg.height !== img.height ||
  182. options.forceResize)) {
  183. data[newImg.getContext ? 'canvas' : 'img'] = newImg;
  184. }
  185. data.preview = newImg;
  186. dfd.resolveWith(that, [data]);
  187. },
  188. thumbnail;
  189. if (data.exif) {
  190. if (options.orientation === true) {
  191. options.orientation = data.exif.get('Orientation');
  192. }
  193. if (options.thumbnail) {
  194. thumbnail = data.exif.get('Thumbnail');
  195. if (thumbnail) {
  196. loadImage(thumbnail, resolve, options);
  197. return dfd.promise();
  198. }
  199. }
  200. // Prevent orienting the same image twice:
  201. if (data.orientation) {
  202. delete options.orientation;
  203. } else {
  204. data.orientation = options.orientation;
  205. }
  206. }
  207. if (img) {
  208. resolve(loadImage.scale(img, options));
  209. return dfd.promise();
  210. }
  211. return data;
  212. },
  213. // Saves the processed image given as data.canvas
  214. // inplace at data.index of data.files:
  215. saveImage: function (data, options) {
  216. if (!data.canvas || options.disabled) {
  217. return data;
  218. }
  219. var that = this,
  220. file = data.files[data.index],
  221. dfd = $.Deferred();
  222. if (data.canvas.toBlob) {
  223. data.canvas.toBlob(
  224. function (blob) {
  225. if (!blob.name) {
  226. if (file.type === blob.type) {
  227. blob.name = file.name;
  228. } else if (file.name) {
  229. blob.name = file.name.replace(
  230. /\..+$/,
  231. '.' + blob.type.substr(6)
  232. );
  233. }
  234. }
  235. // Don't restore invalid meta data:
  236. if (file.type !== blob.type) {
  237. delete data.imageHead;
  238. }
  239. // Store the created blob at the position
  240. // of the original file in the files list:
  241. data.files[data.index] = blob;
  242. dfd.resolveWith(that, [data]);
  243. },
  244. options.type || file.type,
  245. options.quality
  246. );
  247. } else {
  248. return data;
  249. }
  250. return dfd.promise();
  251. },
  252. loadImageMetaData: function (data, options) {
  253. if (options.disabled) {
  254. return data;
  255. }
  256. var that = this,
  257. dfd = $.Deferred();
  258. loadImage.parseMetaData(data.files[data.index], function (result) {
  259. $.extend(data, result);
  260. dfd.resolveWith(that, [data]);
  261. }, options);
  262. return dfd.promise();
  263. },
  264. saveImageMetaData: function (data, options) {
  265. if (!(data.imageHead && data.canvas &&
  266. data.canvas.toBlob && !options.disabled)) {
  267. return data;
  268. }
  269. var file = data.files[data.index],
  270. blob = new Blob([
  271. data.imageHead,
  272. // Resized images always have a head size of 20 bytes,
  273. // including the JPEG marker and a minimal JFIF header:
  274. this._blobSlice.call(file, 20)
  275. ], {type: file.type});
  276. blob.name = file.name;
  277. data.files[data.index] = blob;
  278. return data;
  279. },
  280. // Sets the resized version of the image as a property of the
  281. // file object, must be called after "saveImage":
  282. setImage: function (data, options) {
  283. if (data.preview && !options.disabled) {
  284. data.files[data.index][options.name || 'preview'] = data.preview;
  285. }
  286. return data;
  287. },
  288. deleteImageReferences: function (data, options) {
  289. if (!options.disabled) {
  290. delete data.img;
  291. delete data.canvas;
  292. delete data.preview;
  293. delete data.imageHead;
  294. }
  295. return data;
  296. }
  297. }
  298. });
  299. }));