compressor.mdx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ---
  2. sidebar_position: 12
  3. ---
  4. import Tabs from '@theme/Tabs';
  5. import TabItem from '@theme/TabItem';
  6. import UppyCdnExample from '/src/components/UppyCdnExample';
  7. # Compressor
  8. The `@uppy/compressor` plugin optimizes images (JPEG, PNG, and any other format
  9. supported by the client’s browser) before upload, saving up to 60% in size
  10. (roughly 18 MB for 10 images). It uses [Compressor.js][] library under the hood.
  11. ## When should I use it?
  12. When your users are likely to upload images, potentially on mobile devices, and
  13. saving data and faster uploads are important.
  14. ## Install
  15. <Tabs>
  16. <TabItem value="npm" label="NPM" default>
  17. ```shell
  18. npm install @uppy/compressor
  19. ```
  20. </TabItem>
  21. <TabItem value="yarn" label="Yarn">
  22. ```shell
  23. yarn add @uppy/compressor
  24. ```
  25. </TabItem>
  26. <TabItem value="cdn" label="CDN">
  27. <UppyCdnExample>
  28. {`
  29. import { Uppy, Compressor } from "{{UPPY_JS_URL}}"
  30. const uppy = new Uppy()
  31. uppy.use(Compressor, {
  32. // Options
  33. })
  34. `}
  35. </UppyCdnExample>
  36. </TabItem>
  37. </Tabs>
  38. ## Use
  39. ```js {7} showLineNumbers
  40. import Uppy from '@uppy/core';
  41. import Dashboard from '@uppy/dashboard';
  42. import Compressor from '@uppy/compressor';
  43. new Uppy()
  44. .use(Dashboard, {inline:true, target: '#dashboard')
  45. .use(Compressor);
  46. ```
  47. No action is needed from the user — Uppy will automatically optimize images,
  48. show an [Informer](/docs/informer) message with saved bytes, and then begin the
  49. upload as usual.
  50. ## API
  51. ### Options
  52. :::tip
  53. You can also pass any of the [Compressor.js options][] here as well.
  54. :::
  55. #### `id`
  56. A unique identifier for this plugin (`string`, default: `'Compressor'`).
  57. #### `quality`
  58. The quality of the output image passed to [Compressor.js][] (`number`, default:
  59. `0.6`).
  60. It must be a number between `0` and `1`. Be careful to use `1` as it may make
  61. the size of the output image become larger. In most cases, going with the
  62. default value is best.
  63. :::note
  64. This option is only available for `image/jpeg` and `image/webp` images.
  65. :::
  66. #### `limit`
  67. Number of images that will be compressed in parallel (`number`, default: `10`).
  68. You likely don’t need to change this, unless you are experiencing performance
  69. issues.
  70. #### `locale`
  71. ```js
  72. export default {
  73. strings: {
  74. // Shown in the Status Bar
  75. compressingImages: 'Compressing images...',
  76. compressedX: 'Saved %{size} by compressing images',
  77. },
  78. };
  79. ```
  80. ## Events
  81. #### `compressor:complete`
  82. The event is emitted when all files are compressed. You can use it for side
  83. effects or custom UI notifications.
  84. [compressor.js]: https://github.com/fengyuanchen/compressorjs
  85. [compressor.js options]: https://github.com/fengyuanchen/compressorjs#options