index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import JPEG from 'jpeg-js';
  2. import { throwError, isNodePattern } from '@jimp/utils';
  3. const MIME_TYPE = 'image/jpeg';
  4. export default () => ({
  5. mime: { [MIME_TYPE]: ['jpeg', 'jpg', 'jpe'] },
  6. constants: {
  7. MIME_JPEG: MIME_TYPE
  8. },
  9. decoders: {
  10. [MIME_TYPE]: JPEG.decode
  11. },
  12. encoders: {
  13. [MIME_TYPE]: image => JPEG.encode(image.bitmap, image._quality).data
  14. },
  15. class: {
  16. // The quality to be used when saving JPEG images
  17. _quality: 100,
  18. /**
  19. * Sets the quality of the image when saving as JPEG format (default is 100)
  20. * @param {number} n The quality to use 0-100
  21. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  22. * @returns {Jimp} this for chaining of methods
  23. */
  24. quality(n, cb) {
  25. if (typeof n !== 'number') {
  26. return throwError.call(this, 'n must be a number', cb);
  27. }
  28. if (n < 0 || n > 100) {
  29. return throwError.call(this, 'n must be a number 0 - 100', cb);
  30. }
  31. this._quality = Math.round(n);
  32. if (isNodePattern(cb)) {
  33. cb.call(this, null, this);
  34. }
  35. return this;
  36. }
  37. }
  38. });