thumbnail-generator.mdx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ---
  2. sidebar_position: 2
  3. slug: /thumbnail-generator
  4. ---
  5. import Tabs from '@theme/Tabs';
  6. import TabItem from '@theme/TabItem';
  7. import UppyCdnExample from '/src/components/UppyCdnExample';
  8. # Thumbnail generator
  9. `@uppy/thumbnail-generator` generates proportional thumbnails (file previews)
  10. for images that are added to Uppy.
  11. ## When should I use this?
  12. This plugin is included by default with the [Dashboard](/docs/dashboard) plugin,
  13. and can also be useful to display image previews in a custom UI.
  14. :::note
  15. Thumbnails are only generated for _local_ files. Remote files through
  16. [Companion](/docs/companion) generally won’t have thumbnails because they are
  17. downloaded on the server. Some providers, such as Google Drive, have baked in
  18. thumbnails into their API responses.
  19. :::
  20. ## Install
  21. <Tabs>
  22. <TabItem value="npm" label="NPM" default>
  23. ```shell
  24. npm install @uppy/thumbnail-generator
  25. ```
  26. </TabItem>
  27. <TabItem value="yarn" label="Yarn">
  28. ```shell
  29. yarn add @uppy/thumbnail-generator
  30. ```
  31. </TabItem>
  32. <TabItem value="cdn" label="CDN">
  33. <UppyCdnExample>
  34. {`
  35. import { Uppy, ThumbnailGenerator } from "{{UPPY_JS_URL}}"
  36. const uppy = new Uppy()
  37. uppy.use(ThumbnailGenerator)
  38. `}
  39. </UppyCdnExample>
  40. </TabItem>
  41. </Tabs>
  42. ## Use
  43. If you use the [Dashboard](/docs/dashboard) then it’s already installed. If you
  44. want to use it programmatically for your own UI:
  45. ```js showLineNumbers
  46. import Uppy from '@uppy/core';
  47. import ThumbnailGenerator from '@uppy/thumbnail-generator';
  48. const uppy = new Uppy();
  49. uppy.use(ThumbnailGenerator);
  50. uppy.on('thumbnail:generated', (file, preview) => doSomething(file, preview));
  51. ```
  52. ## API
  53. ### Options
  54. #### `id`
  55. A unique identifier for this plugin (`string`, default: `'ThumbnailGenerator'`).
  56. #### `locale`
  57. ```js
  58. export default {
  59. strings: {
  60. generatingThumbnails: 'Generating thumbnails...',
  61. },
  62. };
  63. ```
  64. #### `thumbnailWidth`
  65. Width of the resulting thumbnail (`number`, default: `200`).
  66. Thumbnails are always proportional and not cropped. If width is provided, height
  67. is calculated automatically to match ratio. If both width and height are given,
  68. only width is taken into account.
  69. #### `thumbnailHeight`
  70. Height of the resulting thumbnail (`number`, default: `200`).
  71. Thumbnails are always proportional and not cropped. If height is provided, width
  72. is calculated automatically to match ratio. If both width and height are given,
  73. only width is taken into account.
  74. :::note
  75. Produce a 300px height thumbnail with calculated width to match ratio:
  76. ```js
  77. uppy.use(ThumbnailGenerator, { thumbnailHeight: 300 });
  78. ```
  79. Produce a 300px width thumbnail with calculated height to match ratio (and
  80. ignore the given height):
  81. ```js
  82. uppy.use(ThumbnailGenerator, { thumbnailWidth: 300, thumbnailHeight: 300 });
  83. ```
  84. See issue [#979](https://github.com/transloadit/uppy/issues/979) and
  85. [#1096](https://github.com/transloadit/uppy/pull/1096) for details on this
  86. feature.
  87. :::
  88. #### `thumbnailType`
  89. MIME type of the resulting thumbnail (`string`, default: `'image/jpeg'`).
  90. This is useful if you want to support transparency in your thumbnails by
  91. switching to `image/png`.
  92. #### `waitForThumbnailsBeforeUpload`
  93. Whether to wait for all thumbnails to be ready before starting the upload
  94. (`boolean`, default: `false`).
  95. If set to `true`, Thumbnail Generator will invoke Uppy’s internal processing
  96. stage and wait for `thumbnail:all-generated` event, before proceeding to the
  97. uploading stage. This is useful because Thumbnail Generator also adds EXIF data
  98. to images, and if we wait until it’s done processing, this data will be
  99. available on the server after the upload.
  100. ### Events
  101. :::info
  102. You can use [`on`](/docs/uppy#onevent-action) and
  103. [`once`](/docs/uppy#onceevent-action) to listen to these events.
  104. :::
  105. #### `thumbnail:generated`
  106. Emitted with `file` and `preview` local url as arguments:
  107. ```js
  108. uppy.on('thumbnail:generated', (file, preview) => {
  109. const img = document.createElement('img');
  110. img.src = preview;
  111. img.width = 100;
  112. document.body.appendChild(img);
  113. });
  114. ```